-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test of busy waiting std::thread to start. NFC (#15896)
This change adds a test that demonstrates the issues with std::thread and busy-waiting for them to start (on the main thread). Fixing the issue is left as a followup. See #15868
- Loading branch information
Showing
5 changed files
with
40 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <atomic> | ||
#include <thread> | ||
#include <cstdio> | ||
#include <emscripten/threading.h> | ||
|
||
int main() { | ||
std::atomic<bool> done(false); | ||
|
||
std::thread t([&]{ | ||
printf("in thread\n"); | ||
done = true; | ||
}); | ||
|
||
while (!done) { | ||
// TODO(sbc): this call should not be needed. | ||
// See: https://github.com/emscripten-core/emscripten/issues/15868 | ||
emscripten_main_thread_process_queued_calls(); | ||
std::this_thread::yield(); | ||
} | ||
|
||
t.join(); | ||
printf("done\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
in thread | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
#include <thread> | ||
|
||
int main() { | ||
std::thread([]{}).join(); | ||
std::thread t([]{ | ||
printf("in thread\n"); | ||
}); | ||
t.join(); | ||
printf("done\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
in thread | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters