c++ - Synchronization of 2 threads, how to? -


i trying develop c++ software has 1 thread controller (say controller) , 8 other threads (child threads) send/recv via tcp/ip.

my software operated in loops:

  • at beginning, child threads run in unlimited loop, in loop wait start signal controller. if awake signal controller, send/recv process. after signalling, controller wait stop signals child threads.
  • after finishing send/recv, each of them send stop signal controller. if controller receive enough stop signals (8 stop signals of 8 child threads). copy data, start process again (next loop).

for purpose, start software @ basic step: controller thread , 1 child thread, , child thread not anything. (i move on if works fine):

  • in other words, controller send start signal child thread while child thread waiting it.
  • right after receiving start signal, child thread sends stop signal.

i execute software 1000 times first, works well, no, freezes usually.

i wondering why happens.

for better explanation, please refer code:

#include "cxxutilities/cxxutilities.hh" using namespace std; cxxutilities::condition startsignal; cxxutilities::condition stopsignal;  // declare class of tx_thread, open, alive, wait signal controller thread // class childthread: public cxxutilities::stoppablethread{ public:   void run(){     cout << "started tx_thread" << endl;         while(!stopped){        /* wait signal main thread */       cout << "sub:wait main...";       startsignal.wait();       cout << "sub:done, \n";        /* signal main thread */       stopsignal.signal();            cout << "sub:signal sent, \n";     }    }  };  int main(int argc, char* argv[]) {     using namespace cxxutilities;     int i;     int events = atoi(argv[1]);      // declear threads //     readingthread* thread[20];     thread[0] = new readingthread;     thread[0]->start();     usleep(1000);      (i=0; i<events; i++){       cout << "\nevent#" << i+1 << ", ";       /* send start signal sub thread */       startsignal.broadcast();       cout << "main:signal sent, \n";        /* wait stop signal sub thread */        cout << "main:wait sub ...";       stopsignal.wait();             cout << "main:done, \n";     }     cout << endl; } 

if modify software controller signals child thread or child thread signals controller, works fine. have idead why thread freeze when use 2 signals 2 trheads ?

in software, there 2 header files used thread.hh , condition.hh

here part of singal() , wait() in condition.hh header file:

   void signal(){     mutex.lock();     pthread_cond_signal(&condition);     mutex.unlock();      void wait(){     mutex.lock();     pthread_cond_wait(&condition,mutex.getpthread_mutex_t());     mutex.unlock(); } 

it's freezing because of deadlock. 1 way happen if main call stopsignal.wait, , locks mutex, before condition set. child thread calls stopsignal.signal, blocks waiting mutex. both threads blocked waiting action other.

locks should acquired shortest amount of time. executing wait while owning lock asking trouble. need restructure wait without lock. like

for (;;) {     mutex.lock();     bool done = /*check condition variable*/;     mutex.unlock();     if (done) break;     sleep(1); } 

this keep lock while you're accessing protected condition variable, free (allowing other thread access condition) while check result of condition. sleep there free cpu resources.

this isn't way solve problem pretty simple implement.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -