How to sleep and wake a thread???


 
Thread Tools Search this Thread
Top Forums Programming How to sleep and wake a thread???
# 8  
Old 12-16-2011
Quote:
Originally Posted by jim mcnamara
As John said above, you probably want your thread to block not sleep. Calls like sleep send SIGALRM to the calling process, not just the thread. This means that you have to set up signal handling for the thread in question, and have all the other threads ignore
SIGALRM (example only, there are other signals)

Using a condition wait or a mutex wait is much more efficient.
sleep doesn't send SIGALRM.

At any rate, pause will work, I've seen people do:

Code:
for ( ; ; ) sleep(UINT_MAX);

And then, as mentioned, pthread_kill can wake it provided the thread hasn't blocked the signal.

Also, as mentioned, there is no way for another thread to cause someone to "go to sleep".

For your edification, this seems like a simple producer-consumer model, and that's usually handled with condition variables. See: pthread_cond_init, pthread_cond_wait, and pthread_cond_signal.
# 9  
Old 12-16-2011
Quote:
Originally Posted by DreamWarrior
At any rate, pause will work, I've seen people do:

Code:
for ( ; ; ) sleep(UINT_MAX);

And then, as mentioned, pthread_kill can wake it provided the thread hasn't blocked the signal.
Interesting. That'll take some careful use of signals though, whether a system call returns with SIGINT when interrupted is optional, and not all systems have the same default.

Quote:
Originally Posted by DreamWarrior
sleep doesn't send SIGALRM.
Maybe not on your system, but some sure do.

Last edited by Corona688; 12-16-2011 at 05:38 PM..
# 10  
Old 12-16-2011
Quote:
Originally Posted by Corona688
Interesting. That'll take some careful use of signals though, whether a system call returns with SIGINT when interrupted is optional, and not all systems have the same default.
That's why you use sigaction; if you want the system call to restart after signal then supply the SA_RESTART flag, otherwise, don't and you'll get SIGINT. While the default behavior may be different across systems, sigaction insures you get the behavior you want. This is why sigaction is preferred over signal for portable code.

edit: as an aside, when we ported a large system from HP-UX to Linux this was a big problem. The team doing the port didn't understand signals very well in general, and the default behavior of a handler installed with signal in HP-UX is different from Linux. They had lots of issues, and I had to explain quite a bit to them to get things working right. Linux was also a lot less forgiving of their dumb use of sigblock and sigunblock to "protect" them from reentering a signal handler. This stupidity caused some very interesting stack traces until I explained the right way to do it was to provide the correct signal mask to sigaction so the block/unblock would occur atomically with the calling of the signal.... Ahh...good times with less than stellar developers, lol.
Quote:
Originally Posted by Corona688
Maybe not on your system, but some sure do.
I suppose the man page for sleep does mention SIGALRM may be used to implement sleep, but have you seen one actually do it?

Last edited by DreamWarrior; 12-16-2011 at 06:21 PM..
# 11  
Old 12-16-2011
Quote:
Originally Posted by DreamWarrior
I suppose the man page for sleep does mention SIGALRM may be used to implement sleep, but have you seen one actually do it?
Probably old systems and maybe not so old systems, but with old implementations of the sleep library function. I think we had better do not make any assumptions on whether the sleep function will raise the SIGALRM signal or not, at least when portability is important.
# 12  
Old 12-19-2011
Quote:
Originally Posted by DreamWarrior
I suppose the man page for sleep does mention SIGALRM may be used to implement sleep, but have you seen one actually do it?
I wouldn't be surprised. There's some you still need to use K&R C in.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Computer wake commands

I'm a OS X user (MacBook Pro, OS X Lion) and I need it to wake up on Mondays, Wednesdays, Thursdays and Saturdays at 9:00 AM on the rest of the days of the week at 7:00 I issue the following commands: sudo pmset repeat wake MWRS 09:00:00 for the former sudo pmset repeat wake TFU... (1 Reply)
Discussion started by: scrutinizerix
1 Replies

2. UNIX for Dummies Questions & Answers

Linux Device Driver: how can an ISR wake up a user-thread?

Hi all, Is it possible to do the following in Linux (kernel 2.6.x): - A user-space thread goes to "sleep". Using any call/mechanism - On a hardware generated interrupt, the Interrupt handler (ISR) "wakes" the sleeping user-thread. I have seen wait_event() and wake_up() but it appears... (1 Reply)
Discussion started by: agaurav
1 Replies

3. Shell Programming and Scripting

Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way: How many times have you used the... (5 Replies)
Discussion started by: deckard
5 Replies

4. UNIX for Advanced & Expert Users

wake up user space thread from kernel space ISR

Hello, I'm searching for a proper way to let the kernel space ISR(implemented in a kernel module) wake up a user space thread on a hardware interrupt. Except for sending a real-time signal, is it possible to use a semaphore? I've searched it on google, but it seems impossible to share a... (0 Replies)
Discussion started by: aaronwong
0 Replies

5. Programming

how to wake up a thread that blocking at epoll_wait?

I have two threads: one maintains a thread-safe message queue (handle this queue at the beginning of every loop) and deals with tcp connections, the other one posts message to the former one. the problem is, while the former one was blocking at epoll_wait, it's not sure that how long until the... (0 Replies)
Discussion started by: cometeor
0 Replies

6. Shell Programming and Scripting

Wake on LAN script

m old to Unix but new to scripting I have a MacBook running osx that I want to use as an nfs client. The server will be a linux box with a wake on lan card. Here's the idea. Run a cron command on the mac every minute that checks if I am on my home wireless network (the linux box is wired to... (6 Replies)
Discussion started by: anon0mus
6 Replies

7. UNIX for Advanced & Expert Users

Wake on Lan script

Im old to Unix but new to scripting I have a MacBook running osx that I want to use as an nfs client. The server will be a linux box with a wake on lan card. Here's the idea. Run a cron command on the mac every minute that checks if I am on my home wireless network (the linux box is wired to... (0 Replies)
Discussion started by: anon0mus
0 Replies
Login or Register to Ask a Question