c++ - synchronization between threads using tbb -


i using tbb programming in c++. not supposed use message queue, fifo, pipes etc platform specific. supposed use tbb specific api's.

thread1: // pseuodo code exits below       // take mutex       m_bisnewsubsarrived = true;      startsubscriptiontimer();      m_bisfristsubsarrived = true;         // spawn thread here.      if(m_tbbtimerthread == null)      {         m_bistimermutextaken = true;                 m_timermutex.lock();          m_tbbtimerthread = new tbb::tbb_thread(&waitfortimermutex, this);         if (m_tbbtimerthread->native_handle() == 0)         {             // report error , return.             return;         }     }      // thread 1 exited.   in thead releasing mutex taken above.  thread 2.   m_timermutex.unlock();  m_bistimermutextaken = false;   thread 3:      // waiting mutex      m_timermutex.lock();  

in above code problem think thread 1 locked m_timermutex not relased think thread2 not able unlock. , thread 3 blocked ever.

i think can use sempahore, api's sempahore in tbb.

what best technique can out sleeping , using tbb specific api's.

thanks time , help.

there no support semaphores in tbb. reason tbb intended raise level of abstraction above raw threads level of tasks, , semaphores considered low-level "goto" of threaded programming. note c++11 not have semaphores either.

using tasks instead of threads allows synchronization become implicit, , permits automatic load balancing via work-stealing scheduler in tbb.

if problem not amenable task-based solution, consider using condition variables, part of c++11. recent versions of tbb ship partial implementation of c++11 condition variables.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -