|  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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: post-christian at freenet dot de
New email:
PHP Version: OS:


 [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-2021 The PHP Group
All rights reserved.
Last updated: Sat Dec 04 07:03:33 2021 UTC