1threads::lite(3) User Contributed Perl Documentation threads::lite(3)
2
3
4
6 threads::lite - Actor model threading for Perl
7
9 Version 0.034
10
12 use Modern::Perl;
13 use threads::lite qw/spawn self receive receive_table/;
14 use SmartMatch::Sugar;
15
16 sub child {
17 my $other = threads::lite::receiveq;
18 while (<>) {
19 chomp;
20 $other->send(line => $_);
21 }
22 return;
23 }
24
25 my $child = spawn({ monitor => 1 } , \&child);
26 $child->send(self);
27
28 my $continue = 1;
29 while ($continue) {
30 receive {
31 when([ 'line', any ]) {
32 my (undef, $line) = @$_;
33 say "received line: $line";
34 }
35 when([ 'exit', any, $child->id ]) {
36 say "received end of file";
37 $continue = 0;
38 }
39 default {
40 die sprintf "Got unknown message: (%s)", join ", ", @$_;
41 }
42 };
43 };
44
46 This module implements threads for perl. One crucial difference with
47 "threads.pm" threads is that the threads are disconnected, except by
48 message queues. It thus facilitates a message passing style of multi-
49 threading.
50
51 Please note that this module is a research project. In no way is API
52 stability guaranteed. It is released for evaluation purposes only, not
53 for production usage.
54
56 All these functions are exported optionally.
57
58 Utility functions
59 spawn($options, $sub)
60
61 Spawn new threads. It will run $sub and send all monitoring processes
62 it's return value. $options is a hashref that can contain the following
63 elements.
64
65 · modules => [...]
66
67 Load the specified modules before running any code.
68
69 · pool_size => int
70
71 Create "pool_size" identical clones.
72
73 · monitor => 0/1
74
75 If this is true, the calling process will monitor the newly spawned
76 threads. Defaults to false.
77
78 · stack_size => int
79
80 The stack size for the newly created threads. It defaults to 64 kiB.
81
82 $sub can be a function name or a subref. If it is a name, you must make
83 sure the module it is in is loaded in the new thread. If it is a
84 reference to a function it will be serialized before being sent to the
85 new thread. This means that any enclosed variables will probability not
86 work as expected. Any locally imported functions will not be defined in
87 the new thread, so you probably want to use fully qualified names.
88
89 self()
90
91 Retreive the thread identifier object corresponding with the current
92 thread.
93
94 send_to($id, ...)
95
96 Send a message a thread identified by its primitive identifier
97
98 Receiving functions
99 All these functions will try to match messages in the local thread's
100 mailbox to a pattern. If it can find a match, the message will be
101 removed from the mailbox.
102
103 receive { ... }
104
105 Match each message against the code in the block until a message
106 matches it. The block is expected to contain "when" and "default"
107 blocks, but may contain other code too. If no matching message is
108 found, it will block until a suitable message is received.
109
110 receive_nb { ... }
111
112 Match in exactly the same way receive does, but do not block if no
113 suitable message can be found. Instead it will return an empty list.
114
115 receiveq(@pattern)
116
117 Return the first message that smart-matches @pattern. If there is no
118 such message in the queue, it blocks until a suitable message is
119 received. An empty pattern results in the first message
120
121 receiveq_nb(@pattern)
122
123 Return the first message that smart-matches @pattern. If there is no
124 such message in the queue, it returns an empty list (undef in scalar
125 context).
126
128 Leon Timmermans, "<leont at cpan.org>"
129
131 This is an early development release, and is expected to be buggy and
132 incomplete. In particular, memory management is known to be buggy.
133
134 Please report any bugs or feature requests to "bug-threads-lite at
135 rt.cpan.org", or through the web interface at
136 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=threads-lite>. I will
137 be notified, and then you'll automatically be notified of progress on
138 your bug as I make changes.
139
141 You can find documentation for this module with the perldoc command.
142
143 perldoc threads::lite
144
145 You can also look for information at:
146
147 · RT: CPAN's request tracker
148
149 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=threads-lite>
150
151 · AnnoCPAN: Annotated CPAN documentation
152
153 <http://annocpan.org/dist/threads-lite>
154
155 · CPAN Ratings
156
157 <http://cpanratings.perl.org/d/threads-lite>
158
159 · Search CPAN
160
161 <http://search.cpan.org/dist/threads-lite>
162
164 Copyright 2009, 2010, 2011 Leon Timmermans, all rights reserved.
165
166 This program is free software; you can redistribute it and/or modify it
167 under the same terms as Perl itself.
168
169
170
171perl v5.32.0 2020-07-28 threads::lite(3)