-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix infinite wait bug in test_pthread_proxying_cpp.cpp #18358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,7 +56,10 @@ void test_proxy_async() { | |
| // Proxy to looper. | ||
| { | ||
| queue.proxyAsync(looper.native_handle(), [&]() { | ||
| i = 2; | ||
| { | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| i = 2; | ||
| } | ||
| executor = std::this_thread::get_id(); | ||
| cond.notify_one(); | ||
| }); | ||
|
|
@@ -68,7 +71,10 @@ void test_proxy_async() { | |
| // Proxy to returner. | ||
| { | ||
| queue.proxyAsync(returner.native_handle(), [&]() { | ||
| i = 3; | ||
| { | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| i = 3; | ||
| } | ||
| executor = std::this_thread::get_id(); | ||
| cond.notify_one(); | ||
| }); | ||
|
|
@@ -164,7 +170,10 @@ void test_proxy_async_with_callback(void) { | |
| queue.proxyAsyncWithCallback( | ||
| looper.native_handle(), | ||
| [&]() { | ||
| i = 2; | ||
| { | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| i = 2; | ||
| } | ||
| executor = std::this_thread::get_id(); | ||
| cond.notify_one(); | ||
| }, | ||
|
|
@@ -184,7 +193,10 @@ void test_proxy_async_with_callback(void) { | |
| queue.proxyAsyncWithCallback( | ||
| returner.native_handle(), | ||
| [&]() { | ||
| i = 3; | ||
| { | ||
| std::unique_lock<std::mutex> lock(mutex); | ||
| i = 3; | ||
| } | ||
| executor = std::this_thread::get_id(); | ||
| cond.notify_one(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to hold the lock when calling notify too?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's actually a pessimization to hold the lock while calling notify because then the other thread wakes up just to immediately block on acquiring the lock.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL! |
||
| }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Despite reading you description I've having trouble seeing how putting a lock around this one assignment fixes the issue. Would making
iand atomic also fix the issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reading https://stackoverflow.com/questions/17101922/do-i-have-to-acquire-lock-before-calling-condition-variable-notify-one I think i understand.. I had never known this about cond vars..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the lock is important because of the condition variable. The condition variable needs to atomically check the condition and block if the condition is not met. The mechanism for combining those into an atomic action is the lock, so if the condition can change without the lock being held, the condition variable doesn't work as intended.