ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm just playing with perl threads. I want my script to keep spawing threads (but never any more than 5 at a time). Each thread just does the same thing - it counts to 5 with 1 sec delays and then ends.
I'm having problems determining the number of threads running. Thread->list returns all the threads which are not joined or detached, but I am detaching my threads so that I don't have to wait for them to return. How can I keep track of how many are running?
I have tried a number of things - at the moment my code looks like this:
Code:
#!/usr/bin/perl5.8.7
use strict;
use Thread;
for(my $i=0;;$i++){
my $thr = new Thread \&thr_handler;
$thr->detach;
until( Thread->list < 5){
# this does nothing really
print " ".Thread->list." threads \n";
sleep 1;
}
}
sub thr_handler{
print STDOUT "joining new thread $\n";
my $count = 0;
while($count<5){
sleep 1;
$count++;
#print STDOUT " -$threadid- \n";
}
};
Does anybody know how I might be able to get this to keep on running with only a maximum of 5 threads running at any time?
First, let me quote the Thread module's documentation:
Quote:
For new code the use of the Thread module is discouraged and the direct use of the threads and threads::shared modules is encouraged instead.
But then, have a look at Thread::Semaphore. It will do all the work for you:
Code:
#!/usr/bin/perl
use strict;
use threads;
use Thread::Semaphore;
my $sem = new Thread::Semaphore( 5 );
while( 1 ){
$sem->down();
my $thr = threads->create( 'thr_handler' );
print "New Thread created.\n";
$thr->detach();
}
sub thr_handler{
sleep 5;
print "Thread finished.\n";
$sem->up();
};
"There is another older Perl threading flavour called the 5.005 model, unsurprisingly for 5.005 versions of Perl. The old model is known to have problems, deprecated, and will probably be removed around release 5.10. You are strongly encouraged to migrate any existing 5.005 threads code to the new model as soon as possible.
You can see which (or neither) threading flavour you have by running perl -V and looking at the Platform section. If you have useithreads=define you have ithreads, if you have use5005threads=define you have 5.005 threads. If you have neither, you don't have any thread support built in. If you have both, you are in trouble.
The user-level interface to the 5.005 threads was via the Threads class, while ithreads uses the threads class. Note the change in case. "
For what it's worth... it is usually not a very good idea to establish that "a request == a thread." Not in any language.
It is, imho, a much better idea to establish that "a thread == a worker-bee," and that once a particular thread has been created it will be allowed to survive until the program ends, whether it has work to do right now or not.
When the program starts, it creates a fixed number of threads, all of whom immediately start waiting for a request to arrive on a queue. Each thread, upon dequeueing a request, executes it and then goes back to waiting again, ad infinitum.
At any moment in time, there might be a backlog in the to-do queue, but the number of active worker-threads remains constant. Consequently, the expected throughput (requests/sec) and the total maximum system load remains fairly constant. (When an operating system starts thrashing, the results are never pretty, and this technique largely avoids that.)
As per sundialsvcs, in some cases creating & deleting (lots of) thrs can cause mem leaks, ime.
Don't forget that (officially) Perl thrs are not prodn ready, although I've found they are fine if used with care. iirc, it's because not all of Perl internals or modules are thr safe (yet).
For what it's worth... it is usually not a very good idea to establish that "a request == a thread." Not in any language.
It is, imho, a much better idea to establish that "a thread == a worker-bee," and that once a particular thread has been created it will be allowed to survive until the program ends, whether it has work to do right now or not.
When the program starts, it creates a fixed number of threads, all of whom immediately start waiting for a request to arrive on a queue. Each thread, upon dequeueing a request, executes it and then goes back to waiting again, ad infinitum.
At any moment in time, there might be a backlog in the to-do queue, but the number of active worker-threads remains constant. Consequently, the expected throughput (requests/sec) and the total maximum system load remains fairly constant. (When an operating system starts thrashing, the results are never pretty, and this technique largely avoids that.)
That would seem to make a _lot_ of sense, but I can't imagine how you would go about it.. Sprint Reveiver's original response seemed pretty neat and I have been progressing with that without really thinking about all the context switching overheads and the possible resk of memory leaks starts to put me off.. Are there any neat examples or howto's out there that you know of which demonstrate your concept of keeping threads alive and dispatching work to them? How would that be done with the basic example above?
It wouldn't make any sense with your example above, since the threads in your example actually don't do anything.
But in principle, have a look at the Thread::Queue module. Your threads could be running in an infinite loop, where they try to retrieve the information they need for their job from a queue. If the queue is empty, they'll just be pausing.
Now the main program just needs to put that information into the queue where any of the idle threads might pick it up and start working.
ok spirit I'll take a look at Thread::Queue - I agree the example above is over simplistic, but I'm just looking at the fundamentals of how this stuff works before I start integrating it into something more meaty - Just to put it into context, I'm thinking of using a threaded set of 'engines' for my dotcut system (www.dotcut.com) - it's just a personal project I build for fun when I'm not at work. At the moment the dotcut engines are just a set of daemonized perl scripts, which only handle on thread of execution each at any time. My aim is to open that up so an engine might handle say 5 or 6 execution contexts at a time, hence beefing up the parsing and indexing which dotcut has to undertake..
I often find perl.org takes a long time to load up, don't know why, so i just grabbed the google version which i already had bookmarked (for the same reason).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.