Solved

signal

Posted on 2011-03-14
7
373 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 33

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 33

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 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