简体   繁体   中英

How to synchronize access of shared data between tasks?

我正在使用.net核心和任务进行一些并行工作,基本上就像两个生产者和一个使用者,两个任务将写入数据,而使用者将以三位.net代码读取数据,这些都是由线程完成的,我可以使用SemaphoreMutexEventHandle在线程之间进行同步,但是我阅读了基于任务的新代码,我不再使用它们,因为不能保证异步函数将在等待后在同一线程上恢复,所以应该怎么做?我在不同任务之间进行同步?

how should I sync between different tasks?

Since you have a producer/consumer system, you can use BufferBlock<T> , which acts as an async-compatible producer/consumer queue.

In the more general sense of coordination primitive replacements, see my AsyncEx.Coordination library . Or you can build your own . The only one built-in to .NET is SemaphoreSlim .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM