mirror of
https://github.com/Relintai/programming_tutorials.git
synced 2025-04-25 21:55:03 +02:00
47 lines
704 B
C++
47 lines
704 B
C++
//új
|
|
#pragma once
|
|
|
|
//régi
|
|
#ifndef ATOMIC_COUNT_H
|
|
#define ATOMIC_COUNT_H
|
|
|
|
#include <iostream>
|
|
#include <thread>
|
|
#include <atomic>
|
|
|
|
using namespace std;
|
|
|
|
class AtomicCount {
|
|
atomic<int> counter;
|
|
|
|
static void thread_func(AtomicCount *obj) {
|
|
obj->count();
|
|
}
|
|
|
|
void count() {
|
|
for (int i =0; i < 100000; ++i) {
|
|
++counter;
|
|
cout << 1;
|
|
}
|
|
}
|
|
|
|
public:
|
|
|
|
void demo() {
|
|
thread t1(AtomicCount::thread_func, this);
|
|
thread t2(AtomicCount::thread_func, this);
|
|
|
|
t1.join();
|
|
t2.join();
|
|
|
|
cout << "Eredmeny: " << counter << endl;
|
|
}
|
|
|
|
AtomicCount () {
|
|
counter = 0;
|
|
}
|
|
|
|
};
|
|
|
|
#endif // ATOMIC_COUNT_H
|