Skip to Main Content
Perl Pocket Reference, 5th Edition
book

Perl Pocket Reference, 5th Edition

by Johan Vromans
July 2011
Intermediate to advanced content levelIntermediate to advanced
104 pages
2h 14m
English
O'Reilly Media, Inc.
Content preview from Perl Pocket Reference, 5th Edition

Threads

Support for threads needs to be built into the Perl executable.

The pragma threads implements thread objects and the necessary operations for threads. Some of the most relevant operations are:

async block

Starts a thread to execute the block. Returns the thread object.

threads->create(sub [ , args ])

Creates a new thread that starts executing in the referenced subroutine. The args are passed to this subroutine. Returns the thread object.

threads->list

Returns a list of joinable threads.

threads->self

Returns an object representing the current thread.

threads->tid

Returns the thread ID of the current thread.

threads->yield

The current thread gives up the CPU in favor of other threads.

thread objects support the several methods, including:

detach

Detaches a thread so it runs independently.

equal(thread)

Returns true if the thread and thread are the same thread. You can also compare thread objects directly, using the == operator.

join

Waits for the thread to complete. The value returned is the return value from the thread’s subroutine.

tid

Returns the thread ID of a thread.

The pragma threads::shared implements operations that enable variable sharing across threads:

cond_broadcast variable

Unblocks all threads waiting for this variable. variable must be locked.

cond_signal variable

Unblocks one thread that is waiting for this variable. variable must be locked.

cond_timed_wait [ condvar , ] variable, time

Like cond_wait, but times out at the indicated time.

cond_wait [ condvar , ] variable

Waits for another ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Testing: A Developer's Notebook

Perl Testing: A Developer's Notebook

Ian Langworth, Chromatic
Perl Hacks

Perl Hacks

Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe

Publisher Resources

ISBN: 9781449311186Errata Page