Solved

sleeping

Posted on 2011-03-08
8
242 Views
Last Modified: 2012-05-11
I am looking to release resources and wake up every 30 secs and do some process.

Here's what I do:

is this the efficient way to do or can I simply do?

while (1)
{
   sleep(30);
   // do something
}

mutex_lock(myLock);

time_t vTime = ::time(NULL);
struct timespec myTime;
myTime.tv_sec = vTime + 30; // 30 secs more than current time
myTime.tv_nsec = 0;

cond_timedwait(&myCond, &myMutex, &myTime);  // This will sleep for 30 secs and then wakeup

// fo watever you want. 

mutex_unlock(myLock);

Open in new window

0
Comment
Question by:learningunix
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 35073627
The effect is indeed the same - yet the overhead of using pthreads for just pausing execution for 30s is definitely too much. Unless other factors would require a timed mutex lock, I'd definitely just go for 'sleep()'. Another altenative would be to use 'setitimer()' (http://linux.die.net/man/2/setitimer) and handling SIGVTALRM while your process is doing different processing, but if it is just about waking up every 30 secs and do some processing, 'sleep()' is just fine.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35073728
If you must sleep for 30 seconds, then sleep. The myTime in cond_timedwait() is not a forced sleep for 30 seconds; but rather a timeout. You are guaranteed that you will not remain on the cond_timedwait for more than 30 seconds, but you may remain there very briefly if another thread immediately signals the condition after you hit that line. So, the semantics of sleep and the myTime timeout are very different.


I noticed that you have
>>  mutex_lock(myLock);
>>  cond_timedwait(&myCond, &myMutex, &myTime);  

From http://uw714doc.sco.com/en/man/html.3synch/cond_timedwait.3synch.html

"int cond_timedwait(cond_t *cond, mutex_t *mutex, timestruc_t *abstime);

The calling thread must lock the mutual exclusion lock (mutex) pointed to by mutex before calling cond_timedwait, otherwise the behavior is unpredictable."
But you are locking with myLock and then using myMutex instead of myLock.

If you need a condition variable to meet your design goals, then you have to use it (carefully), and you may as well wait. If you can avoid condition variables, I would do so as they are one of the more difficult aspects of concurrent control.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35073963
I didn't see jkr's post when I submitted my post. At first it appeared that we were saying different things about equivalency. But after a little thinking, I realized that perhaps you were never planning on having another thread send a condition signal thereby forcing a timeout in order to get the effect of a simple sleep. If that is the case, then I wouldn't clutter up your code with this extra threading control. As already noted, the condition variable is a little harder to get right than other thread functions.

Also, re: sleep - From http://linux.die.net/man/3/sleep
sleep() makes the current process sleep until seconds seconds have elapsed or a signal arrives which is not ignored .

Return Value
Zero if the requested time has elapsed, or the number of seconds left to sleep.
0
 

Author Comment

by:learningunix
ID: 35076630
its only accessed by single thread so that is not an issue.
I guess sleep() would be better in my case as threads would be overhead?
0
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!

 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 35077168
while (1)
{
   sleep(30);
   // do something
}

ensure you have a condition to break out from the while loop....else as per your design above the thread could sleep forever
0
 
LVL 86

Expert Comment

by:jkr
ID: 35077287
>>I guess sleep() would be better in my case as threads would be overhead?

Definitely - even if you could live with the overhead, since that probably is negligible nowadays. But, *clean* programming does not depend on the power of the hardware you have at your purpose.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 35082229
Just remember that depending upon your application, that sleep may return before the 30 seconds are up, so you may have to take action to re-sleep for the difference.
0
 

Author Closing Comment

by:learningunix
ID: 35097344
thx
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

746 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