|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66424 Cond::wait doesn't wait given time
Submitted: 2014-01-06 03:49 UTC Modified: 2014-01-11 18:39 UTC
From: post-christian at freenet dot de Assigned:
Status: Not a bug Package: pthreads (PECL)
PHP Version: 5.5.7 OS: openSUSE 11.4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
5 + 7 = ?
Subscribe to this entry?

 [2014-01-06 03:49 UTC] post-christian at freenet dot de
The test script below works with timeouts of 10ms or 1sec, but doesn't work with timeouts of 100ms or 500ms, Cond::wait returns much faster then expected.

Test script:
class Test extends Thread
	private $mutex, $cond;

	public function __construct()
		$this->mutex = Mutex::create( true );
		$this->cond = Cond::create();
	public function __destruct()
		Cond::destroy( $this->cond );
		Mutex::unlock( $this->mutex );
		Mutex::destroy( $this->mutex );
	public function run()
		while( true ) {
			trace( "test", "running" );
			if( @ Cond::wait( $this->cond, $this->mutex, 500000 ) )
		echo "stopped\n";
	public function stop()
		Cond::signal( $this->cond );

$test = new Test();
sleep( 5 );

function trace( $sender, $msg )
	$ts = microtime( true );
	echo date( 'd/m H:i:s', $ts ), substr( sprintf( "%.6f", $ts - (int) $ts ), 1 ), ' ', $sender, ': ', $msg, "\n";

Expected result:
Waiting for the given timeout.

Actual result:
Seems to return immediately but much faster from Cond::wait with some timeouts.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-06 03:52 UTC]
-Package: threads +Package: pthreads
 [2014-01-06 03:52 UTC]
set correct package
 [2014-01-06 08:05 UTC]
-Status: Open +Status: Not a bug
 [2014-01-06 08:05 UTC]
You are not using the API properly, read about spurious wakeups.
 [2014-01-11 18:06 UTC] post-christian at freenet dot de
Can you show me where I have to look fore? I can't find a documentation about spurious wakeups. Do you meant here: ?

Thank you
 [2014-01-11 18:39 UTC]
Search for a posix article about spurious wakeups, all the same information applies.

The Mutex and Cond API's are intended to be used by those who have used mutex and cond otherwise.

All the same, here's working code:

Using mutex and conditions are tricky, which is why pthreads abstracts them:

Here's some reading that will probably help:

The key things to remember are to lock the mutex upon broadcast and wait, and only ever wait _for_ something.
 [2014-01-12 05:42 UTC] post-christian at freenet dot de
Thank you for the examples. You helped me a lot ;-)
 [2014-01-12 06:16 UTC] post-christian at freenet dot de
But if I use 500 * 1000 as wait time in Thread->run() both scripts don't wait the 500 msec. Can you confirm it and do you know why it happen?
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Aug 14 21:01:24 2020 UTC