Skip to content

Commit 92d5ad7

Browse files
author
Justin Jack
committed
first commit
0 parents  commit 92d5ad7

19 files changed

+2603
-0
lines changed

examples/args.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
require_once '../lib/phpthread.php';
3+
4+
function threadproc($thread,
5+
$arg1,
6+
$arg2)
7+
{
8+
echo "Hi! I'm the PHPThread. Here are the arguments you passed:\n";
9+
echo "arg1: " . print_r($arg1, true) . "\n";
10+
echo "arg2: " . print_r($arg2, true) . "\n";
11+
echo "------------------------------------------\n";
12+
for ($i = 0; $i < 5; $i++) {
13+
usleep(1000000);
14+
echo "\t* PHPThread - Tick.\n";
15+
}
16+
return $i;
17+
}
18+
19+
$tid = 0;
20+
$thread = null;
21+
$retval = null;
22+
23+
$tid = phpthread_create(
24+
$thread,
25+
array(),
26+
"threadproc",
27+
null,
28+
array('apple', 'banana'));
29+
30+
for ($i = 0; $i < 10; $i++) {
31+
usleep(1000000);
32+
echo "* MAIN - Tick.\n";
33+
}
34+
35+
phpthread_join($tid, $retval);
36+
37+
echo "* MAIN - Done, PHPThread was active for \"" . $retval . "\" seconds.\n\n";

examples/callbacks.php

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<?php
2+
require_once '../lib/phpthread.php';
3+
4+
5+
/**
6+
* The 'onexit' handler is called in the execution context/address space of
7+
* the parent process.
8+
*/
9+
function thread_exit_callback( $exiting_thread,
10+
$return_value)
11+
{
12+
$my_id = phpthread_get_id();
13+
14+
echo "thread_exit_callback() - MAIN (ID: $my_id) - PHPthread ID: " .
15+
$exiting_thread->id() . " has exited. From here, I can inspect its return value, which is: \"" .
16+
print_r($return_value, true) . "\"\n\n";
17+
}
18+
19+
20+
function threadproc(PHPTHREAD $thread,
21+
int $messages_to_send)
22+
{
23+
echo "\tCHILD (ID: " . $thread->pid .") - Our parent wants $messages_to_send messages from us.\n";
24+
25+
$seconds = 0;
26+
$msent = 0;
27+
28+
$running = true;
29+
$thread->user_data = &$running;
30+
$timeout = time() + 25;
31+
while ($running) {
32+
if (time() > $timeout) {
33+
echo "\n\n**** THREAD TIMEOUT ****\n\n";
34+
break;
35+
}
36+
37+
/**
38+
* If the thread was started with on "onmessage" attribute,
39+
* this code will never run, as messages will not be enqueued.
40+
*
41+
* We could call get_message(1000) to wait "up to" one second,
42+
* but we're calling usleep() below to make sure we wait no-less
43+
* than one second.
44+
*/
45+
if ( ($msg = $thread->get_message())) {
46+
if ($msg->message === "quit") {
47+
echo "---- QUIT IN " . getmypid() . "\n\n";
48+
break;
49+
}
50+
51+
$from_phpthread = $msg->thread;
52+
$message = $msg->message;
53+
echo "\n---------------------------------------------------\n";
54+
echo "CHILD (PID: " . phpthread_get_id() . ") - Message from: ";
55+
56+
if ($from_phpthread->id() === $thread->parent_id()) {
57+
if ($from_phpthread->id() === phpthread_get_main_proc_id()) {
58+
echo "PARENT/MASTER (PHPthread ID: " . $from_phpthread->id() . ")\n";
59+
} else {
60+
echo "PARENT (ID: " . $from_phpthread->id() . ")\n";
61+
}
62+
} else {
63+
echo "PHPthread ID: " . $from_phpthread->id() . "\n";
64+
}
65+
echo trim(print_r($message, true)) . "\n";
66+
echo "---------------------------------------------------\n\n";
67+
}
68+
usleep(1000000);
69+
echo "\tCHILD (ID: " . $thread->pid .") - Tick :)\n";
70+
$seconds++;
71+
if (($msent < $messages_to_send) &&
72+
($seconds % 3) === 0)
73+
{
74+
$msent++;
75+
$thread->send_message("($msent/$messages_to_send) Just checking in. All's well!");
76+
}
77+
}
78+
return "Okay, Thread " . $thread->id() . " is done. Here's my result!";
79+
}
80+
81+
function thread_message_handler($from_phpthread,
82+
$message)
83+
{
84+
$thisthread = phpthread_this();
85+
86+
if ($message === "quit") {
87+
$thisthread->user_data = false;
88+
return;
89+
}
90+
91+
echo "\n---------------------- thread_message_handler() ------------------------\n";
92+
echo "CHILD (PID: " . phpthread_get_id() . ") - Message from: ";
93+
94+
if ($from_phpthread->id() === $thisthread->parent_id()) {
95+
if ($from_phpthread->id() === phpthread_get_main_proc_id()) {
96+
echo "PARENT/MASTER (PHPthread ID: " . $from_phpthread->id() . ")\n";
97+
} else {
98+
echo "PARENT (ID: " . $from_phpthread->id() . ")\n";
99+
}
100+
} else {
101+
echo "PHPthread ID: " . $from_phpthread->id() . "\n";
102+
}
103+
echo trim(print_r($message, true)) . "\n";
104+
echo "---------------------------------------------------\n\n";
105+
}
106+
107+
srand(time());
108+
109+
$main_id = phpthread_get_id();
110+
111+
/* Vars for PHPthread 1 */
112+
$thread1 = null;
113+
$thread1ret = null;
114+
$thread1id = 0;
115+
116+
/* Vars for PHPthread 2 */
117+
$thread2 = null;
118+
$thread2ret = null;
119+
$thread2id = 0;
120+
121+
/**
122+
* There IS a 'onthreadmessage' attribute specified. Messages from
123+
* this PHPthread to its parent (the MAIN process in this case) will
124+
* be sent to the function "messages_from_thread1()"
125+
*/
126+
$thread1id =
127+
phpthread_create(
128+
$thread1,
129+
array(
130+
'onexit'=>"thread_exit_callback",
131+
),
132+
"threadproc",
133+
null, /* No class instance or name, it's not a method */
134+
array(3) /* The 2nd parameter received in "threadproc()", after it's PHPTHREAD class instance
135+
* will be the number "3". Meaning this process wants THREE messages from it.
136+
*/
137+
);
138+
139+
140+
if ($thread1id < PHPT_SUCCESS) {
141+
echo "MAIN (PID: $main_id) - ERROR: [".phpthread_create_errmsg($thread1id)."] Failed to launch PHPthread!\n";
142+
exit($thread1id);
143+
}
144+
145+
146+
/**
147+
* NO 'onthreadmessage' attribute. Messages from this PHPthread will
148+
* be sent to the target's (the MAIN process in this case) default
149+
* message handler.
150+
*/
151+
$thread2id =
152+
phpthread_create(
153+
$thread2,
154+
array(
155+
'onexit'=>"thread_exit_callback",
156+
'onmessage'=>"thread_message_handler",
157+
),
158+
"threadproc",
159+
null,
160+
array(1) /* We want ONE message from thread 2 */
161+
);
162+
163+
164+
if ($thread2id < PHPT_SUCCESS) {
165+
if ($thread1) $thread1->kill();
166+
echo "MAIN (PID: $main_id) - ERROR: [".phpthread_create_errmsg($thread2id)."] Failed to launch PHPthread!\n";
167+
exit($thread2id);
168+
}
169+
170+
171+
echo "MAIN (PID: $main_id) - Running for 20 cycles. I will send a message to the first PHPthread at 6 cycles, and the second at 12. Then, I'll send them both 'quit' messages after my loop.\n\n";
172+
173+
for ($i = 0; $i < 20; $i++) {
174+
175+
$msg = phpthread_get_message(1000); /* Wait up to 1 second for a message */
176+
177+
if ($msg) {
178+
echo "\n##################################################\n";
179+
echo "In MAIN loop. phpthread_get_message() received a message.\n";
180+
echo "MASTER (PID: " . phpthread_get_id() . ") - Message from: ";
181+
182+
if ($msg->thread->id() === phpthread_get_parent_id()) {
183+
if ($phpthread->id() === $msg->thread->main_proc_id()) {
184+
echo "PARENT/MASTER (PHPthread ID: " . $msg->thread->id() . ")\n";
185+
} else {
186+
echo "PARENT (ID: " . $msg->thread->id() . ")\n";
187+
}
188+
} else {
189+
echo "PHPthread ID: " . $msg->thread->id() . "\n";
190+
}
191+
echo trim(print_r($msg->message, true)) . "\n";
192+
echo "##################################################\n\n";
193+
}
194+
195+
switch ($i) {
196+
case 6: /* After six seconds, send a message to the first PHPThread */
197+
$thread1->send_message("Hey there, thread 1! Just saying 'hi!'");
198+
break;
199+
case 12: /* After twelve seconds, send a message to the first PHPThread */
200+
$thread2->send_message("Hola, thread 2! Just saying 'hi!' to my fav child!");
201+
break;
202+
default:
203+
break;
204+
}
205+
}
206+
207+
echo "\nMAIN (PID: $main_id) - Sending 'quit' to child PHPthreads.\n";
208+
209+
210+
$thread1->send_message("quit");
211+
$thread2->send_message("quit");
212+
213+
echo "\nMAIN (PID: $main_id) - Waiting on child PHPthreads to finish up.\n";
214+
215+
$rv1 = null;
216+
$rv2 = null;
217+
218+
219+
$jresult = phpthread_join($thread1id, $rv1);
220+
$jresult = phpthread_join($thread2id, $rv2, 3000);
221+
222+
if ($jresult !== 0) {
223+
echo "\n**ERROR: Child thread 2 failed to quit, Killing it.\n\n";
224+
$thread2->kill();
225+
}
226+
227+
228+
229+
echo "Thread 1 returned " . strlen($rv1) . " bytes.\n";
230+
echo "Thread 2 returned " . strlen($rv2) . " bytes.\n";
231+
echo "\n\n";
232+
echo "Done.\n";
233+
exit(0);

examples/client.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
require_once '../lib/phpthread.php';
3+
4+
/**
5+
* Need todo
6+
*/

0 commit comments

Comments
 (0)