Figured out threads for the next round.

master
Zed A. Shaw 3 months ago
parent fdb3f24377
commit 8d1570f44a
  1. 4
      meson.build
  2. 34
      threadtest.cpp

@ -36,6 +36,7 @@ executable('escape_turings_tarpit',
'builder.cpp', 'builder.cpp',
'sfmlgui.cpp', 'sfmlgui.cpp',
'escape_turings_tarpit.cpp'], 'escape_turings_tarpit.cpp'],
win_subsystem: 'windows',
dependencies: dependencies) dependencies: dependencies)
executable('regtest', 'regtest.cpp', executable('regtest', 'regtest.cpp',
@ -53,6 +54,9 @@ executable('audiotest', 'audiotest.cpp',
executable('jsontest', 'jsontest.cpp', executable('jsontest', 'jsontest.cpp',
dependencies: dependencies) dependencies: dependencies)
executable('threadtest', 'threadtest.cpp',
dependencies: dependencies)
runtests = executable('runtests', [ runtests = executable('runtests', [
'game_engine.cpp', 'game_engine.cpp',
'tests/game_engine.cpp', 'tests/game_engine.cpp',

@ -0,0 +1,34 @@
#include <thread>
#include <fmt/core.h>
#include <chrono>
#include <iostream>
#include <thread>
#include <utility>
#include <iostream>
#include <mutex>
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<std::mutex> 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<std::mutex> lock(counter_mutex);
std::cout << "counter is " << counter << std::endl;
}
}
Loading…
Cancel
Save