Solved

signal

Posted on 2011-03-14
7
370 Views
Last Modified: 2012-05-11
This is in reference to
http://www.experts-exchange.com/Programming/Languages/CPP/Q_26884790.html#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

0
Comment
Question by:learningunix
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 150 total points
Comment Utility
Um, yes - if you call 'cond_signal()' while the thread is inside 'sleep()', the signal would indeed be lost.
0
 

Author Comment

by:learningunix
Comment Utility
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?

0
 

Author Comment

by:learningunix
Comment Utility
or I'll have to design is such a way that situation like this should not occur
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
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
0
 
LVL 32

Accepted Solution

by:
sarabande earned 350 total points
Comment Utility
for a gently termination of a worker thread by main thread cond_timedwait is a bit of overkill. especially if you have sleep of 30 seconds where no check was done.

if you would pass a pointer to the thread which simply could be asked whether to terminate or not you could much simpler do the job.

// some header
class ThreadControl
{
    volatile bool stop;
    ThreadControl() : stop(false) {}
    void stopThread() { stop = true; }
    bool isStopped() { return stop; }
};

// main thread:

  ThreadControl * ptc = new ThreadControl();
  pthread_create(...., ptc);

  ....
  ptc->stopThread();
  join(&thread);

// worker thread:

void threadfunc(void* p)
{
    ThreadControl * ptc = (ThreadControl *)p;    

    ...
        // do as often as possible:
        if (ptc->isStopped())
            return;

}

Open in new window


the point is that the cond_ functions have the main purpose to immdiately restart a waiting thread. but in your scenario you mostly have a running thread which occasionally has a waiting period. so you can't expect that the thread immediately will end but you can narrow the recognition time period to the time the longest call would need where you can't pass the ptc into.

Sara
0
 

Author Closing Comment

by:learningunix
Comment Utility
thx
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now