This isn't a fully worked solution, but it does demonstrate that it is reasonably easy to simulate the unix alarm function under Win32.
#! perl -slw
use strict;
use threads;
use threads::shared;
my $alarmFlag : shared = 0;
$SIG{__WARN__} = sub{ $alarmFlag = 1; };
sub myAlarm {
my( $procID, $timeout ) = @_;
async sub{
Win32::Sleep $timeout;
warn 'Timeout';
};
}
myAlarm( $$, 3000 );
while( not $alarmFlag ) {
print 'Waiting for the alarm at ', scalar localtime;
print 'Tum te, tum te, tum';
Win32::Sleep 1000;
}
print 'The alarm was raised', $/;
$alarmFlag = 0;
myAlarm( $$, 3000 );
while( not $alarmFlag ) {
print 'Waiting for the alarm at ', scalar localtime;
print 'Tum te, tum te, tum';
Win32::Sleep 1000;
}
print 'The alarm was raised', $/;
__END__
P:\test>alarm
Waiting for the alarm at Mon Dec 8 20:03:19 2003
Tum te, tum te, tum
Waiting for the alarm at Mon Dec 8 20:03:20 2003
Tum te, tum te, tum
Waiting for the alarm at Mon Dec 8 20:03:21 2003
Tum te, tum te, tum
The alarm was raised
Waiting for the alarm at Mon Dec 8 20:03:22 2003
Tum te, tum te, tum
Waiting for the alarm at Mon Dec 8 20:03:23 2003
Tum te, tum te, tum
Waiting for the alarm at Mon Dec 8 20:03:24 2003
Tum te, tum te, tum
The alarm was raised
This could be done a lot more effectively at the perl source level, and even made more sensible by a few pretty trivail patches to the perl sources. Changing the current default behaviour for most of the signals under Win32 which is set to die for pretty much anything other than a couple of exceptions -- which is just about the strangest choice of default behaviour possible as it make it impossible to code a more compatible workaround as you never get the opportunity to trap them? Strange choice.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.