diff --git a/meson.build b/meson.build index cd75d84..f75414d 100644 --- a/meson.build +++ b/meson.build @@ -36,6 +36,7 @@ executable('escape_turings_tarpit', 'builder.cpp', 'sfmlgui.cpp', 'escape_turings_tarpit.cpp'], + win_subsystem: 'windows', dependencies: dependencies) executable('regtest', 'regtest.cpp', @@ -53,6 +54,9 @@ executable('audiotest', 'audiotest.cpp', executable('jsontest', 'jsontest.cpp', dependencies: dependencies) +executable('threadtest', 'threadtest.cpp', + dependencies: dependencies) + runtests = executable('runtests', [ 'game_engine.cpp', 'tests/game_engine.cpp', diff --git a/threadtest.cpp b/threadtest.cpp new file mode 100644 index 0000000..22b19c6 --- /dev/null +++ b/threadtest.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std::chrono_literals; + +std::atomic_int counter = 0; +std::mutex counter_mutex; + + +void locked_counter() { + for (int i = 0; i < 5; ++i) + { + std::lock_guard lock(counter_mutex); + std::this_thread::sleep_for(100ms); + std::cout << "Thread 1 executing\n"; + ++counter; + } +} + +int main() +{ + std::jthread t2(locked_counter); // pass by value + + for(int i = 0; i < 5; ++i) { + std::lock_guard lock(counter_mutex); + std::cout << "counter is " << counter << std::endl; + } +}