Make Thread a wrapper for both ithreads and 5005threads.
[p5sagit/p5-mst-13.2.git] / ext / threads / threads.pm
CommitLineData
47ba8780 1package threads;
2
3use 5.7.2;
4use strict;
5use warnings;
6
7use overload
43d3ddbe 8 '==' => \&equal,
47ba8780 9 'fallback' => 1;
10
47ba8780 11#use threads::Shared;
12
13require Exporter;
14require DynaLoader;
15
47ba8780 16our @ISA = qw(Exporter DynaLoader);
17
18our %EXPORT_TAGS = ( all => [qw()]);
19
20our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
21
22our @EXPORT = qw(
23
24);
25our $VERSION = '0.05';
26
47ba8780 27
43d3ddbe 28sub equal {
47ba8780 29 return 1 if($_[0]->tid() == $_[1]->tid());
30 return 0;
31}
32
8222d950 33$threads::threads = 1;
47ba8780 34
35bootstrap threads $VERSION;
36
47ba8780 37# Preloaded methods go here.
38
391;
40__END__
41
42=head1 NAME
43
44threads - Perl extension allowing use of interpreter based threads from perl
45
46=head1 SYNOPSIS
47
47ba8780 48use threads;
49
50sub start_thread {
51 print "Thread started\n";
52}
53
47ba8780 54my $thread = threads->new("start_thread","argument");
55
56$thread->new(sub { print "I am a thread"},"argument");
57
58$thread->join();
59
60$thread->detach();
61
62$thread = threads->self();
63
11c51ed3 64threads->tid();
65threads->self->tid();
66
67$thread->tid();
47ba8780 68
47ba8780 69=head1 DESCRIPTION
70
43d3ddbe 71Perl 5.6 introduced something called interpreter threads. Interpreter
72threads are different from "5005threads" (the thread model of Perl
735.005) by creating a new perl interpreter per thread and not sharing
74any data or state between threads.
11c51ed3 75
43d3ddbe 76Prior to perl 5.8 this has only been available to people embedding
77perl and for emulating fork() on windows.
11c51ed3 78
43d3ddbe 79The threads API is loosely based on the old Thread.pm API. It is very
80important to note that variables are not shared between threads, all
81variables are per default thread local. To use shared variables one
82must use threads::shared.
11c51ed3 83
43d3ddbe 84It is also important to note that you preferably enable threads by
85doing C<use threads> as early as possible and that it is not possible
86to enable threading inside an eval "";
47ba8780 87
88=over
89
43d3ddbe 90=item $thread = new(function, LIST)
47ba8780 91
ad91d581 92This will create a new thread with the entry point function and give
93it LIST as parameters. It will return the corresponding threads
94object.
47ba8780 95
43d3ddbe 96create() is an alias to new.
11c51ed3 97
98=item $thread->join
47ba8780 99
43d3ddbe 100This will wait for the corresponding thread to join. When it finishes
101join will return the return values of the entry point function. If a
102thread has been detached, join will return without wait.
47ba8780 103
11c51ed3 104=item $thread->detach
47ba8780 105
43d3ddbe 106Will throw away the return value from the thread and make it
107non-joinable.
47ba8780 108
109=item threads->self
110
111This will return the object for the current thread.
112
11c51ed3 113=item $thread->tid
47ba8780 114
43d3ddbe 115This will return the id of the thread. threads->self->tid() is a
116quick way to get current thread id.
47ba8780 117
118=back
119
120
121=head1 TODO
122
123=over
124
43d3ddbe 125=item Fix so the return value is returned when you join.
47ba8780 126
43d3ddbe 127=item Add join_all.
47ba8780 128
129=item Fix memory leaks!
130
131=back
132
133=head1 AUTHOR and COPYRIGHT
134
11c51ed3 135Arthur Bergman E<lt>arthur at contiller.seE<gt>
47ba8780 136
43d3ddbe 137threads is released under the same license as Perl.
47ba8780 138
139Thanks to
140
ad91d581 141Richard Soderberg E<lt>rs at crystalflame.netE<gt>
142Helping me out tons, trying to find reasons for races and other weird bugs!
47ba8780 143
ad91d581 144Simon Cozens E<lt>simon at brecon.co.ukE<gt>
145Being there to answer zillions of annoying questions
47ba8780 146
ad91d581 147Rocco Caputo E<lt>troc at netrus.netE<gt>
47ba8780 148
ad91d581 149Vipul Ved Prakash E<lt>mail at vipul.netE<gt>
47ba8780 150Helping with debugging.
151
152please join perl-ithreads@perl.org for more information
153
154=head1 BUGS
155
156=over
157
158=item creating a thread from within a thread is unsafe under win32
159
b5807cdb 160=item PERL_OLD_SIGNALS are not threadsafe, will not be.
161
11c51ed3 162
47ba8780 163=back
164
165=head1 SEE ALSO
166
11c51ed3 167L<perl>, L<threads::shared>, L<perlcall>, L<perlembed>, L<perlguts>
47ba8780 168
169=cut