Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got one producer and n consumers. There's an array of size n. The producer puts things into the array, and a consumer takes one element from the array whenever an element is available. How to achieve this?
I tried for loop to check the availability of every element and wait for its corresponding conditional signal. But this leads to deadlocks.
Posted
Comments
Matthew Faithfull 29-May-13 18:45pm    
The easiest way is probably a parallel array of bits, one per element, that determine the availability. This is only simple if n, the number of consumers and array size is a constant. That's the key question.

For producer-consumer schema, it's much better to use the queue, not an array: http://www.cplusplus.com/reference/queue/queue/[^].

For such use of the queue, you need to make your queuing/dequeuing operations thread-safe. You will find more than enough material on this problem is you look here:
http://bit.ly/11A3Liy[^]

—SA
 
Share this answer
 
Comments
Krauze 29-May-13 21:00pm    
Thanks. Those help a lot!
Sergey Alexandrovich Kryukov 29-May-13 21:18pm    
You are very welcome.
Good luck, call again.
—SA
Wrap your reading and writing functions in a synchronization object, then only one thread at a time can access your array.
class MyArrayWrapper  // untested code
{
   private:
      std::vector<int> MyArray;
      CCriticalSection MyCC;    // Sync Object

   public:
      int WriteToEnd(int Num)
      {
         CSingleLock cs(&MyCC); // Wait till MyCC is free
         MyArray.push_back(Num);
         return MyArray.size();
         // cs goes out of scope and releases MyCC so another thread can run
      }

      int ReadAndRemoveFromBack()
      {
         CSingleLock cs(&MyCC);
         int ret = MyArray.back();
         MyArray.pop_back();
         return ret;
      }
}
 
Share this answer
 
v4
Comments
Matthew Faithfull 29-May-13 19:00pm    
This is safe but not performant. I think the OP wants to be able to have multiple readers accessing the array in parallel which should be doable and is really the point of multithreading in this case.
PJ Arends 29-May-13 19:44pm    
There are many ways to skin this cat, using events and WaitForMultipleObjects etc. Either way it is always a good idea to use thread synchorization to avoid deadlocks, even if it is DIY.
Matthew Faithfull 29-May-13 20:04pm    
Absolutely, synchronization is vital and also the hardest thing to do in software.
Krauze 29-May-13 21:00pm    
Yes, I need it to be accessed in parallel.
If memory is not a problem your producer can create this array n times and all this n comsumer takes their own datas. So there will be no deadlocks.

Another way is not always check it is available. If not (I do not know you algorithm) you can do any other job or you can sleep and again if not available increase the sleep time and sleep again.
 
Share this answer
 
Comments
Krauze 29-May-13 21:02pm    
The first solution is not smart enough. The second seems to be feasible. Thanks anyway.

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