Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Concurrent execution of threads

by pplshlp (Initiate)
on Nov 29, 2012 at 05:55 UTC ( #1006160=perlquestion: print w/replies, xml ) Need Help??
pplshlp has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to start 10 threads concurrently. This program does not seem to be running concurrently.
#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; print "Starting main program\n"; my @threads; for ( my $count = 1; $count <= 10;$count++) { my $t = threads->new(\&sub1); push(@threads,$t); } foreach (@threads) { $_->join; } print "End of main program\n"; sub sub1 { //do something }
Can someone please help? Is there anything that I have to add/modify? Thanks

Replies are listed 'Best First'.
Re: Concurrent execution of threads
by kcott (Chancellor) on Nov 29, 2012 at 06:46 UTC

    G'day pplshlp,

    // introduces a comment in a number of languages but not in Perl. You need a # in Perl.

    With your code, I get:

    Bareword found where operator expected at -e line 20, near "//do somet +hing" (Missing operator before something?) syntax error at -e line 21, near "//do something "

    Changing //do something to #do something, I get:

    Starting main program End of main program

    I don't see a concurrency issue:

    $ perl -e ' use strict; use warnings; use threads; use threads::shared; print "Started at: ", time, "\n"; print "Starting main program\n"; my @threads; for ( my $count = 1; $count <= 10;$count++) { my $t = threads->new(\&sub1); push(@threads,$t); } foreach (@threads) { $_->join; } print "End of main program\n"; print "Stopped at: ", time, "\n"; sub sub1 { print "Called at: ", time, "\n"; sleep 2; #do something } ' Started at: 1354171101 Starting main program Called at: 1354171101 Called at: 1354171101 Called at: 1354171101 Called at: 1354171101 Called at: 1354171101 Called at: 1354171101 Called at: 1354171101 Called at: 1354171101 Called at: 1354171101 Called at: 1354171101 End of main program Stopped at: 1354171103

    -- Ken

      Thank you

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006160]
Approved by kcott
help
Chatterbox?
[1nickt]: marto would like to hear that. Of course he speaks very deliberately and dramatically, allowing pauses between words, so if anybody could pull it off it would be him, or someone impersonating him ...
[Eily]: Discipulus that's a chess joke BTW right? Because bishops don't walk straight :D
[choroba]: stop making chessy jokes about bishops!
[choroba]: I mean cheesy
[LanX]: lol
[ambrus]: no, the chess bishop itself is a joke on real bi-shops
[ambrus]: or at least on the stereotype of bi-shops
[choroba]: we call chess bishops "archers"
[Eily]: choroba sorry, I had to get that off my chess
[ambrus]: chess certainly hasn't started those stereotypes, like how kings aren't that powerful but has convinced their whole country to work for them, etc. it's just poured them to a nice clean form that would easily get propagated.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2017-09-26 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (294 votes). Check out past polls.

    Notices?