Boost logo

Boost Users :

Subject: [Boost-users] [thread] multiple producers, single consumer - bosst-specific and generic questions
From: Julien Claassen (julien_at_[hidden])
Date: 2009-09-09 17:27:32


Hello everyone!
   Thanks for all the help I already got. And excuse my poor understanding of
of multithreading.
   I would like to implement a "multiple producer single consumer" pattern.
My current solution is working with signals, which, I think is not very
efficient. And also it doesn't work properly with the code I have, (see end of
this mail). But besides that problem I have a generic question.
   If I worked with buffers and mutex, there are two choices, it seems:
1. Use one buffer, at the cost of deletion from the middle, when one producer
terminates (so consumer won't communicate with a dead thread).
2. Have a buffer per producer, but also have a synchronisation per buffer and
the consumer must look in several buffers, plus I don't know, how many
producers there will be.
   This all should lead to a user interface, with different input-methods. So
what would you suggest? It must be speedy and very CPU friendly.
   Thanks in advance for consideration. I'd also be happy for some suggested
reading. Only one thing: printed books or documents in non-plain-text form
besides .doc, .pdf and .ps aren't of any use to me, for I'm blind and using
Linux text based environment.
   Kindest regards (For code, see below)
             Julien

*** CODE SNIPPET ***

boost::mutex my_mtx; // the Mutex for passing on events/chars

// assumptions:
// the following variables are passed to the Input object as a reference
// int& its_q_size;
// condition& its_in_condition;
// condition& its_out_condition;
// boost::signals2::signal<void (char)>& its_sig;

void Input::run()
{
   char input;
   boost::unique_lock<boost::mutex> lk(my_mtx);
   while (input != 'q')
   {
     cin >> input;
     while (its_q_size != 0)
     {
       its_in_condition.wait(lk);
     }
     its_sig(input);
     its_q_size = 0;
     its_out_condition.notify_all();
   }
}

void Controller::run()
{
   sig_t sig; // passed as its_sig
   condition in_condition, out_condition; // passed respectively as
     // its_incondition and its_out_condition
   int q_size = 0; // passed as
   char input;
   boost::unique_lock<boost::mutex> lk(my_mtx);
   Input *ip = new Input(in_condition,out_condition,sig,q_size);
   ip->connect(bind(&Controller::slot1, this, _1));
   ip->connect(boost::bind(&Controller::slot2, this, _1));
   thread runner(boost::bind(&Input::run, ip)); // why no third param _1
     // here? With one it won't compiler!
   while (input != 'q')
   {
     while (q_size != 1)
     {
       out_condition.wait(lk);
     }
     // Do something with data received from the slots
     q_size = 0;
     in_condition.notify_all();
   }
   runner.join();
}

*** END OF CODE ***
Effects: Read once, send signal, read again print info from first read and
send signal again. Only really sync, when q is pressed. I expect due to
runner-thread ending.

--------
Music was my first love and it will be my last (John Miles)

======== FIND MY WEB-PROJECT AT: ========
http://ltsb.sourceforge.net
the Linux TextBased Studio guide
======= AND MY PERSONAL PAGES AT: =======
http://www.juliencoder.de


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net