How to put a single thread to sleep?

R

Ralph Moritz

Hi,

On my system (Windows XP Prof SP2), calling Perl's sleep()
from a thread appears to put the entire process to sleep, rather
than just the current thread.

Q: How can I put just the current thread to sleep?

TIA,
Ralph
 
A

A. Sinan Unur

On my system (Windows XP Prof SP2), calling Perl's sleep()
from a thread appears to put the entire process to sleep, rather
than just the current thread.

And where is the code that shows that?

#!/usr/bin/perl

use strict;
use warnings;

$| = 1;

use threads;

sub sleeper {
my $t0 = time;
while ( 1 ) {
print "\n ... Wovzah! ", time - $t0, " ... Yeee-haw!\n";
sleep 10;
}
}

sub worker {
while ( 1 ) {
sleep 1;
print '*';
}
}

my $s = threads->create('sleeper');
my $w = threads->create('worker');

$_->detach for $s, $w;

sleep 1 while 1;
Q: How can I put just the current thread to sleep?

You know, posting the same nitwit question in multiple threads is not a
good way of eliciting help.

Don't thank me if you do not appreciate my help.

I see, you still haven't learned how to use a sig separator.
Ph: +27 84 626 9070

I am tempted.

Sinan
 
J

Jürgen Exner

Ralph said:
Q: How can I put just the current thread to sleep?

Is this question different to the one that is currently discussed in the
thread "How to make only one thread sleep?"

jue
 
X

xhoster

Ralph Moritz said:
Hi,

On my system (Windows XP Prof SP2), calling Perl's sleep()
from a thread appears to put the entire process to sleep, rather
than just the current thread.

Q: How can I put just the current thread to sleep?

I don't see that on my machine.

H:\>perl -le "use threads; $t = async { 1 foreach 1..1e6; warn $_ foreach
1..10; }; warn q(sleep) ; sleep 3; warn q(awake); $t->join"

sleep at -e line 1.
1 at -e line 1.
2 at -e line 1.
3 at -e line 1.
4 at -e line 1.
5 at -e line 1.
6 at -e line 1.
7 at -e line 1.
8 at -e line 1.
9 at -e line 1.
10 at -e line 1.
awake at -e line 1.

H:\>perl -v

This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 25 registered patches, see perl -V for more detail)


Xho
 
R

Ralph Moritz

A. Sinan Unur said:
You know, posting the same nitwit question in multiple threads is not a
good way of eliciting help.

You're right, it was a mistake. Sorry.
Don't thank me if you do not appreciate my help.

I do appreciate your help, it's just that I was (mistakenly)
convinced that sleep() was putting all threads in the process
to sleep. The actual problem was something else, of course.
I see, you still haven't learned how to use a sig separator.

This is a bit anal, don't you think? My newsreader usually
inserts the sig seperator for me, but when I'm using Google,
Groups I sometimes forget to add the space. I don't think
it's something worth making a fuss about though.
 
B

Ben Morrow

Quoth "A. Sinan Unur said:
It is a single solitary newline in-between two blocks. It really does
not affect bandwidth consumed (it is still less than the 320 bytes taken
up by a 4 line 80 column sig). After all, the reason for the custom is
to reduce the amount of bandwidth dedicated to vanity. Half of my sig is
my email address, the other half is a link to the posting guidelines.

The reason for the restriction is not just (or even mainly) because of
bandwidth, it is also to limit screen-estate use on 80x25 terminals,
which some of us still use.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,050
Members
48,769
Latest member
Clifft

Latest Threads

Top