Queue message processing with Multithreading
I have to design multithreaded module for a problem. And problem is, I have queue, there is a one thread which is putting messages in the message queue, and there are two thread say A and B, thread A process the even message (0,2,4..) and thread B processes the odd message(1,3,5..). I came up with two solution, first one is using two event开发者_运维问答s(say X and Y) event X is for thread A and Y is for thread B. I check if the message is at even position, I set the event X for thread A, and Y for thread B otherwise. And the second solution is by having two seperate quest for each thread. A thread will put even position messages to queue of thread A and odd messages to the queue of thread B, with this solution thread A and B can work asynchronously. Am i right, or is there any other elegant solution?
Thanks.
Using only one queue and synchronizing A and B in order to ensure a proper fetching sequence is a complete nonsense.
Just use two queues, one for A and one for B, and ensure that they are filled correctly (which seems an easier and cleanier problem by far, even from a design PoV.
What you're describing is very similar to a thread pool or work queue. In your case, you only have two worker threads in the pool. I believe the Intel Thread Building Blocks library supports thread pools.
Assuming that there is no requirement that even/odd messages be strictly processed by their respective threads, you can use a single thread-safe producer/consumer queue, and make your two consumer threads dequeue messages in a first-come/first-serve basis. No need to restrict odd messages to thread A, and even messages to thread B. The first-come/first-serve solution is more readily scalable if you later decide to increase the number of worker threads (for example 4 threads to be run on a quad core).
Elegant solution:
- Maintain a single lock for the queue
- Use three semaphores (one for each thread)
- WriteThreadSem
- OddReadSem
- EvenReadSem
Have the write thread obtain the queue lock, write the entry, release the lock... Meanwhile the even and odd threads acquire their respective semaphores. Once it is done writing the writing thread wakens the thread via its respective semaphore that is next to read. The waking thread acquires the queue lock and reads its data.. Releases the lock, wakes the write thread semaphore.. The write thread determines if it needs to write or what the next read thread needs to be wakened...
Done.
Or you could easily write a thread-safe single queue model using basic mutexes.
http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
精彩评论