Skip to content

Commit

Permalink
Add test of busy waiting std::thread to start. NFC (#15896)
Browse files Browse the repository at this point in the history
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
sbc100 authored Jan 6, 2022
1 parent 2fda25e commit 4423f06
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
24 changes: 24 additions & 0 deletions tests/pthread/test_pthread_busy_wait.cpp
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;
}
2 changes: 2 additions & 0 deletions tests/pthread/test_pthread_busy_wait.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
in thread
done
6 changes: 5 additions & 1 deletion tests/pthread/test_pthread_cxx_threads.cpp
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;
}
2 changes: 2 additions & 0 deletions tests/pthread/test_pthread_cxx_threads.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
in thread
done
10 changes: 7 additions & 3 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8476,12 +8476,16 @@ def test_pthread_c11_threads(self):

@node_pthreads
def test_pthread_cxx_threads(self):
self.set_setting('PROXY_TO_PTHREAD')
self.clear_setting('ALLOW_MEMORY_GROWTH')
self.set_setting('INITIAL_MEMORY', '64Mb')
self.set_setting('PTHREAD_POOL_SIZE', 1)
self.set_setting('EXIT_RUNTIME')
self.do_run_in_out_file_test('pthread/test_pthread_cxx_threads.cpp')

@node_pthreads
def test_pthread_busy_wait(self):
self.set_setting('PTHREAD_POOL_SIZE', 1)
self.set_setting('EXIT_RUNTIME')
self.do_run_in_out_file_test('pthread/test_pthread_busy_wait.cpp')

@node_pthreads
def test_pthread_create_pool(self):
# with a pool, we can synchronously depend on workers being available
Expand Down

0 comments on commit 4423f06

Please sign in to comment.