php.net |  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:
Package:
Bug Type:
Summary:
From: post-christian at freenet dot de
New email:
PHP Version: OS:

 

 [2014-01-06 03:49 UTC] post-christian at freenet dot de
Description:
------------
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 ) )
				break;
		}
		echo "stopped\n";
	}
	
	public function stop()
	{
		Cond::signal( $this->cond );
		$this->join();
	}
}

$test = new Test();
$test->start();
sleep( 5 );
$test->stop();

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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-06 03:52 UTC] alan_k@php.net
-Package: threads +Package: pthreads
 [2014-01-06 03:52 UTC] alan_k@php.net
set correct package
 [2014-01-06 08:05 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2014-01-06 08:05 UTC] krakjoe@php.net
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:

http://docs.php.net/manual/en/cond.wait.php ?

Thank you
 [2014-01-11 18:39 UTC] krakjoe@php.net
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: http://pastebin.com/801RHB0h

Using mutex and conditions are tricky, which is why pthreads abstracts them: http://pastebin.com/j2N8K973

Here's some reading that will probably help: https://gist.github.com/krakjoe/6437782

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-2019 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 07:01:25 2019 UTC