examples/cpp-multithreaded: running flag is atomic

This commit is contained in:
Mariusz Kasolik 2019-05-31 14:27:09 +02:00
parent 4b20c5b045
commit 178671a7bd
1 changed files with 3 additions and 2 deletions

View File

@ -6,6 +6,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <atomic>
#include "../../ui.h" #include "../../ui.h"
using namespace std; using namespace std;
@ -13,7 +14,7 @@ uiMultilineEntry *e;
condition_variable cv; condition_variable cv;
mutex m; mutex m;
thread *timeThread; thread *timeThread;
volatile bool running = true; atomic<bool> running(true);
void sayTime(void *data) void sayTime(void *data)
{ {
@ -26,7 +27,7 @@ void sayTime(void *data)
void threadproc(void) void threadproc(void)
{ {
unique_lock<mutex> ourlock(m); unique_lock<mutex> ourlock(m);
while (running) { while (running.load()) {
cv.wait_for(ourlock, chrono::seconds(1)); cv.wait_for(ourlock, chrono::seconds(1));
time_t t; time_t t;
char *base; char *base;