0

Is this a safe way to ensure that the master thread will never miss a wake up signal if no other thread uses the variable flag?

void *Cancel_master_thread(void *arg)
{
  while (1)
  {
    flag = 0;
    pthread_mutex_lock(&cancel_in_progress);
    flag = 1;
    pthread_cond_wait(&cancel_finished, &cancel_in_progress);
    pthread_mutex_unlock(&cancel_in_progress);
  }
}

void *Cancel_slave_MB()
{
  while (1)
  {
    while (flag != 1)
    {
    }

    pthread_mutex_lock(&cancel_in_progress);
    pthread_cond_signal(&cancel_finished);
    pthread_mutex_unlock(&cancel_in_progress);
  }
}
alk
  • 69,737
  • 10
  • 105
  • 255
iiirxs
  • 4,493
  • 2
  • 20
  • 35

2 Answers2

0

After changing the flag for the master, you have to yield the processor so the master can execute. (normally. There is a long discussion about threading and multi-processors here.) So you would like to have some sort of blocking call (like sleep) so the master can run. Currently, you have the Cancel_slave_MB running in a tight loop, so nothing else has a chance to run.

EDIT So your example is a little bare-bones. If you set breakpoints or printf's (outside of the locked region), do they execute properly? I would say don't loop forever, that's the point of the semaphores.

Zagrev
  • 2,000
  • 11
  • 8
  • But all I want is the master to sleep when the signal from slave arrives. So when the flag gets the value 1 the master will sleep and only then the slave will can lock the mutex and send the signal. Is n't it right? For a multiprocessor system the threads are executed simultaneously. So where is the problem? I forgot to refer that obviously the slave sleeps after unlocking the mutex cancel_in_progress, I just didn't concluded the pthread_cond_wait command in the posted code. – iiirxs Sep 25 '13 at 19:22
  • Yes they execute properly for a long time but then somehow the master thread doesn't wake up so everything stops... – iiirxs Sep 28 '13 at 21:04
0

If your indention is that cancel_master_thread() performs exactly one pthread_cond_wait() for one pthread_cond_signal() from cancel_slave_MB() (while in the while-loop) then do it the following way:

void * cancel_master_thread(void *arg)
{
  pthread_mutex_lock(&cancel_in_progress);

  while (1)
  {
    pthread_cond_wait(&cancel_finished, &cancel_in_progress);

    /* do something per signal received */
  }

  pthread_mutex_unlock(&cancel_in_progress);
}

void * cancel_slave_MB()
{
  while (1)
  {  
    /* do something */

    pthread_mutex_lock(&cancel_in_progress);
    pthread_cond_signal(&cancel_finished);
    pthread_mutex_unlock(&cancel_in_progress);
  }
}
alk
  • 69,737
  • 10
  • 105
  • 255
  • What's the point of this code? I mean all the master thread will do is to sleep after sleep... – iiirxs Sep 28 '13 at 21:03
  • @iiirxs: Yes sure, but the master is guaranted to wake up on each signal, will not miss a signal. It might be I did not understand what the intention of the code you posted is. – alk Sep 29 '13 at 08:04