I have 1 critical section (section A) and two other sections (B and C). I'm trying to accomplish these two goals:

  1. If a thread hits section A, I want to make sure sections B and C cannot be executed by any other thread (for the duration of section A).

  2. However, if section A is not being run, then sections B and C can be executed by any number of threads.

Obviously, using a lock on all three sections doesn't help: that would meet the first requirement, but not the second. B and C would not be able to run concurrently under normal circumstances.

How do I accomplish this?

(Apologies for the vague title)

有帮助吗?

解决方案

Perhaps you can use a ReaderWriterLock where B and C (AcquireReaderLock) can come in as readers while A (AcquireWriterLock) goes in as a writer.

http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlock.aspx

That's exactly what that class is meant for.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top