Link to home
Start Free TrialLog in
Avatar of learningunix
learningunix

asked on

signal

This is in reference to
https://www.experts-exchange.com/questions/26884790/kill-thread.html?anchorAnswerId=35129837#a35129837

I think so this can happen:

While control in thread is executing sleep(30), Now my main program executes
cond_signal(&myWait);

At this point there is no one to wait on condition variable?

wouldn't the signal call be lost and it will never print the line "I was called by cleanUp. Exiting now"
void cleanUp() {
     cond_signal(&myWait);
     join(&thread);
}


void myLoop() { 
  while (1)
  {
      .....
      mutex_lock(&myMutex);   

      cond_timedwait(&myWait, &myMutex, &myTime);  //myTime will be current time + 60 secs
If (status != 0)  // it timed out and I continue the execution
       {
             //  continue the execution
             sleep (30);   
             //  continue the execution
        }
       else 
         {
            printf ("I was called by cleanUp. Exiting now");     
            break;
         }
  }
     mutex_unlock(&myMutex )
}

Open in new window

SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of learningunix
learningunix

ASKER

That's what I was saying in previous thread ;)  that means the solution in my previous thread won't work

I used sleep() just as an example but if it is not sleep() and executing some other other time consuming commands, will it still be lost?

or I'll have to design is such a way that situation like this should not occur
Yes, while you are not explicitly waiting for such a signal, it would go unnoticed. For such a case, astandard POSIX signal (http://en.wikipedia.org/wiki/Signal.h) might be the better choice.
if the myWait was already signaled when you call the cond_timdwait it will return immediately with 0.

so if you continue after sleep in the loop a call of cond_signal was not be lost.

but you need to move the mutex_lock before the loop cause the mutex was reestablished by cond_timedwait before return. so while sleeping the lock is still active and you don't need a new lock with next loop cycle.
 
Sara
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thx