I have multiple threads, ThreadA and ThreadsB-Z.
ThreadA is always in critical section, popping the data out of queue and sending it on socket.
When any thread from ThreadB to ThreadZ want to enter the critical section, it wants ThreadA to leave critical section only then. It then enters the critical Section, push some data into queue and leaves critical section.
I have two problems here:
- How would ThreadB-Z (whoever wants to enter the Critical Section) tell ThreadA to leave critical section when it wants to access the critical section.
- I tried with the idea of SetEvent or PostThreadMessage to tell threadA to leave the critical Section, but I am unable to handle any Event or Thread Message since ThreadA is continuously popping data out of the queue using while(1) and there is no message loop or WaitforSingleObject() type thing to handle Events or Thread Messages :(
I am like stuck in this situation. Any help/suggestion is welcome. Thanks in advance.
The real problem here is "ThreadA is always in critical section". ThreadA should not be locking the critical section indefinitely.
In C#, your code for ThreadA would look something like:
In my opinion, the following general rules of thumb should be observed when using critical sections:
messageQueue
... then I would name the critical sectionmessageQueueLock
Great quote from Microsoft: "When you use multithreading of any sort, you potentially expose yourself to very serious and complex bugs" [SOURCE: MSDN]