{"action":"created","issue":{"url":"https://api.github.com/repos/krakjoe/pthreads/issues/481","labels_url":"https://api.github.com/repos/krakjoe/pthreads/issues/481/labels{/name}","comments_url":"https://api.github.com/repos/krakjoe/pthreads/issues/481/comments","events_url":"https://api.github.com/repos/krakjoe/pthreads/issues/481/events","html_url":"https://github.com/krakjoe/pthreads/issues/481","id":104936226,"number":481,"title":"Thread::synchronized() locks main thread","user":{"login":"pfofi","id":7479939,"avatar_url":"https://avatars.githubusercontent.com/u/7479939?v=3","gravatar_id":"","url":"https://api.github.com/users/pfofi","html_url":"https://github.com/pfofi","followers_url":"https://api.github.com/users/pfofi/followers","following_url":"https://api.github.com/users/pfofi/following{/other_user}","gists_url":"https://api.github.com/users/pfofi/gists{/gist_id}","starred_url":"https://api.github.com/users/pfofi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pfofi/subscriptions","organizations_url":"https://api.github.com/users/pfofi/orgs","repos_url":"https://api.github.com/users/pfofi/repos","events_url":"https://api.github.com/users/pfofi/events{/privacy}","received_events_url":"https://api.github.com/users/pfofi/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-09-04T17:44:18Z","updated_at":"2015-09-04T18:18:30Z","closed_at":"2015-09-04T18:18:30Z","body":"Hello,\r\n\r\nI've been experimenting with krakjoe/pthreads@a769e5b76e3fb3db1f3a165e397e5cefc97e4fcd and PHP 7.0.0 RC2.\r\n\r\nIt seems that `synchronized()`, when called from a thread, prevents execution in the main thread:\r\n```php\r\nclass TestThread extends Thread\r\n{\r\n    public function run()\r\n    {\r\n        self::synchronized(function () {\r\n            sleep(PHP_INT_MAX); // lock indefinitely\r\n        });\r\n    }\r\n}\r\n\r\n$thread = new TestThread;\r\n$thread->start();\r\nsleep(1); // give thread time to enter synchronized function\r\necho 'Main thread done. Waiting...', PHP_EOL;\r\n```\r\n\r\nExpected result:\r\n`Main thread done. Waiting...` (after a second and then waiting for interruption).\r\n\r\nActual result:\r\n`*emptiness*`\r\n\r\nIn PHP 5 it did work.\r\n\r\nAs always, I'm not sure if this is \"by design\" or a bug."},"comment":{"url":"https://api.github.com/repos/krakjoe/pthreads/issues/comments/137810639","html_url":"https://github.com/krakjoe/pthreads/issues/481#issuecomment-137810639","issue_url":"https://api.github.com/repos/krakjoe/pthreads/issues/481","id":137810639,"user":{"login":"krakjoe","id":2236138,"avatar_url":"https://avatars.githubusercontent.com/u/2236138?v=3","gravatar_id":"","url":"https://api.github.com/users/krakjoe","html_url":"https://github.com/krakjoe","followers_url":"https://api.github.com/users/krakjoe/followers","following_url":"https://api.github.com/users/krakjoe/following{/other_user}","gists_url":"https://api.github.com/users/krakjoe/gists{/gist_id}","starred_url":"https://api.github.com/users/krakjoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/krakjoe/subscriptions","organizations_url":"https://api.github.com/users/krakjoe/orgs","repos_url":"https://api.github.com/users/krakjoe/repos","events_url":"https://api.github.com/users/krakjoe/events{/privacy}","received_events_url":"https://api.github.com/users/krakjoe/received_events","type":"User","site_admin":false},"created_at":"2015-09-04T18:18:30Z","updated_at":"2015-09-04T18:18:30Z","body":"Don't use ```sleep```, I think I might override the function so that it can't be used; Sleep is intended to make *processes* sleep, ```usleep``` is nicer, it is intended for *threads*, however it is still not the best method of forcing a thread to wait.\r\n\r\nIf you want reliable code, you should only ever wait _for_ something. \r\n\r\nThe example is strange, in the real world, the task at hand is *never* wait *forever*, so I have to break the rules to make the code work.\r\n\r\n\t<?php\r\n\tclass TestThread extends Thread {\r\n\r\n\t\tpublic function run() {\r\n\t\t    $this->synchronized(function () {\r\n\t\t        while (!$this->done)\r\n\t\t            $this->wait();\r\n\t\t    });\r\n\t\t}\r\n\t}\r\n\r\n\t$thread = new TestThread;\r\n\t$thread->start();\r\n\r\n\techo 'Main thread done. Waiting...', PHP_EOL;\r\n\r\n\t$thread->synchronized(function() use($thread){\r\n\t\t    $thread->wait(3 * 1000000);\r\n\t\t    $thread->done = true;\r\n\t\t    $thread->notify();\r\n\t});\r\n\r\n        $thread->join();\r\n\r\nYou must use preconditions; it is not just the programmer that can explicitly notify the thread, it can be done implicitly (by calling join for example), or internals otherwise changing the state of the thread."}}