Solved

signal

Posted on 2011-03-14
7
374 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
ID: 35130875
Um, yes - if you call 'cond_signal()' while the thread is inside 'sleep()', the signal would indeed be lost.
0
 

Author Comment

by:learningunix
ID: 35130945
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
ID: 35130990
or I'll have to design is such a way that situation like this should not occur
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 35131001
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 34

Expert Comment

by:sarabande
ID: 35136445
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 34

Accepted Solution

by:
sarabande earned 350 total points
ID: 35136530
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
ID: 35167295
thx
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

756 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