Click here to Skip to main content
15,897,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently working on a personal project that uses the following threads:

1 Thread has to read files and split them into chunks and adds these chunks to a queue. This thread ends when there are no more files to be read.

N threads will then use the queue and pop off the chunk on top and proceed to do what ever task is needed with that chunk, this can range depending on the file. They only access the queue when they need data after they have finished their processing.

A total of N + 1 threads.

(This is just a framework for other personal projects)

The first thread is very simple as all it does is read and push data to the queue, but when I start adding more than one extra thread I run into a problem, and I have not done a lot with more than two threads working from the same data source.

I am not really sure what the best method would be to protect my queue from being corrupt from having more than one thread access it. I thought of wrapping the queue class and adding locks to the pops and pushes of the std::queue or is there another library that would provide a thread safe way of doing this. I have looked into some solutions online, but I have only done a little bit of multithreaded programming.

Would a different method of spreading the work be better? Or will having locks, semaphores or critical_section, on queue->pop() and queue->push() be sufficient for my purpose?
Posted

1 solution

Your framework is the classic producer/consumer queue, see, for instance "Using Condition Variables".
:)
 
Share this answer
 
Comments
elchupathingy 18-Jun-10 14:06pm    
Thanks, rather simple. Guess I was over thinking it completely then if the answer is that simple :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900