It seems to me that what you want to do is possible, you just need to work out the details, but you didn't say what language your threads are in. I'll assume it's Perl.
I'm used to using the "sleeping thread concept" with shared variables. I've posted numerous examples of it here, just SuperSearch for Tk threads. Anyways, the basic concept is to make worker threads and put them in a while loop where they sleep, and wake up at a specified interval, and check a shared variable, to wake up and go. If it wakes up, it reads another shared variable, which would contain the code block you want it to run. THEN :-) , while it's running it's code, it checks it's shared variable to see whether it should go back to sleep( that would trigger your Save routine). The main thread could then save all the data in the thread's shared variable to some hash or db, then you could restore it later. It all seems doable, but the devil is in the details....making sure all the threads have properly defined shared variables, and setting up a scheme in the main thread to read the shared variables, Having an Event loop system in the main thread is pretty much essential, unless you are very clever to setup a complex while loop in the main thread. Here is the basis of a sleeping thread.
foreach my $dthread(1..$numworkers){
share ($shash{$dthread}{'go'});
share ($shash{$dthread}{'data'});
share ($shash{$dthread}{'pid'});
share ($shash{$dthread}{'die'});
$shash{$dthread}{'go'} = 0;
$shash{$dthread}{'data'} = $data;
$shash{$dthread}{'pid'} = -1;
$shash{$dthread}{'die'} = 0;
$hash{$dthread}{'thread'} = threads->new(\&work,$dthread);
}
sub work{
my $dthread = shift;
$|++;
while(1){
if($shash{$dthread}{'die'} == 1){ goto END };
if ( $shash{$dthread}{'go'} == 1 ){
eval( system( $shash{$dthread}{'data'} ) );
foreach my $num (1..100){
$shash{$dthread}{'progress'} = $num;
print "\t" x $dthread,"$dthread->$num\n";
select(undef,undef,undef, .5);
if($shash{$dthread}{'go'} == 0){last}
if($shash{$dthread}{'die'} == 1){ goto END };
}
$shash{$dthread}{'go'} = 0; #turn off self before returning
}else
{ sleep 1 }
}
END:
}
There is a way to use filehandles across threads too, which may be handy to use instead of shared variables.
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;
# original idea from BrowserUK at
# http://perlmonks.org?node_id=493754
for my $file ( map{ glob $_ } @ARGV ) {
open my $fh, '<', $file or warn "$file : $!" and next;
printf "From main: %s", scalar <$fh> for 1 .. 10;
printf "Fileno:%d\n", fileno $fh;
threads->create( \&thread, fileno( $fh ) )->detach;
printf 'paused:';<STDIN>;
}
sub thread{
my( $fileno ) = @_;
open my $fh, "<&=$fileno" or warn $! and die;
printf "%d:%s", threads->self->tid, $_ while defined( $_ = <$fh> );
close $fh;
}
I'm not really a human, but I play one on earth.
flash japh
|