parent
fdb3f24377
commit
8d1570f44a
@ -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…
Reference in new issue