Solved

signal

Posted on 2011-03-14
7
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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