10. 쓰레드
1. 쓰레드 - join()
#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>
using namespace std;
void Pause(int sec) {
this_thread::sleep_for(chrono::seconds(sec));
}
constexpr int GetThreadNum(int num) {
return num;
}
int main() {
int default_sec = 3;
int num;
time_t start = time(NULL);
time_t finish = time(NULL);
cout << "테스트할 개수를 입력해 주세요: ";
cin >> num;
int THREAD_NUM = GetThreadNum(num);
thread thread_arr[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; i++) {
thread_arr[i] = thread(Pause, i + default_sec);
}
cout << "== thread 사용 ==" << endl;
time(&start);
for (int i = 0; i < THREAD_NUM; i++, default_sec++) {
thread_arr[i].join();
cout << default_sec << "초 대기" << endl;
}
time(&finish);
cout << "thread 사용 시 걸린 시간: "
<< difftime(finish, start) << "초" << endl;
default_sec = 3;
start = time(NULL);
finish = time(NULL);
cout << endl
<< "== thread 미사용 ==" << endl;
time(&start);
for (int i = 0; i < THREAD_NUM; i++, default_sec++) {
Pause(default_sec);
cout << default_sec << "초 대기" << endl;
}
time(&finish);
cout << "thread 미사용 시 걸린 시간: "
<< difftime(finish, start) << "초" << endl;
return 0;
}
2. 쓰레드 - detach()
#include <chrono>
#include <ctime>
#include <iostream>
#include <thread>
using namespace std;
void Pause(int sec) {
this_thread::sleep_for(chrono::seconds(sec));
cout << "분리된 쓰레드 " << sec << "초 대기" << endl;
}
constexpr int GetThreadNum(int num) {
return num;
}
int main() {
int default_sec = 3;
int num;
time_t start = time(NULL);
time_t finish = time(NULL);
cout << "테스트할 개수를 입력해 주세요: ";
cin >> num;
int THREAD_NUM = GetThreadNum(num);
thread thread_arr[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; i++) {
thread_arr[i] = thread(Pause, i + default_sec);
}
cout << "== thread 사용 ==" << endl;
time(&start);
for (int i = 0; i < THREAD_NUM; i++, default_sec++) {
thread_arr[i].detach();
}
time(&finish);
cout << "thread 사용 시 걸린 시간: "
<< difftime(finish, start) << "초" << endl;
default_sec = 3;
start = time(NULL);
finish = time(NULL);
cout << endl
<< "== thread 미사용 ==" << endl;
time(&start);
for (int i = 0; i < THREAD_NUM; i++, default_sec++) {
Pause(default_sec);
cout << default_sec << "초 대기" << endl;
}
time(&finish);
cout << "thread 미사용 시 걸린 시간: "
<< difftime(finish, start) << "초" << endl;
return 0;
}
3. atomic
1
#include <atomic>
#include <iostream>
using namespace std;
int main() {
atomic<int> data1 = 10;
atomic<int> data2 = ATOMIC_VAR_INIT(20);
atomic_int data3 = ATOMIC_VAR_INIT(30);
cout << data1 << ", " << data2 << ", " << data3 << endl;
data1.store(11);
data2.store(data3.load());
atomic_store(&data3, 50);
cout << data1 << ", " << data2 << ", " << data3 << endl;
return 0;
}
4. atomic
2
#include <atomic>
#include <iostream>
using namespace std;
int main() {
atomic<int> data1 = 10;
atomic<int> data2 = ATOMIC_VAR_INIT(20);
atomic<int> data3 = data1.exchange(data2.load());
cout << data1 << ", " << data2 << ", " << data3 << endl;
atomic_exchange(&data1, data3);
atomic_exchange(&data2, data1.load());
atomic_exchange(&data3, 55);
cout << data1 << ", " << data2 << ", " << data3 << endl;
return 0;
}
5. atomic
3
#include <atomic>
#include <iostream>
using namespace std;
int main() {
atomic<int> data1 = ATOMIC_VAR_INIT(10);
atomic<int> data2 = ATOMIC_VAR_INIT(10);
int number = 20;
bool is_exchange = data1.compare_exchange_weak(number, 30);
cout << boolalpha;
cout << "data1: " << data1 << ", number: " << number << ", " << is_exchange << endl;
is_exchange = atomic_compare_exchange_weak(&data2, &number, 40);
cout << "data2: " << data2 << ", number: " << number << ", " << is_exchange << endl;
return 0;
}
6. atomic
4
#include <atomic>
#include <iostream>
using namespace std;
int main() {
atomic<int> data1 = ATOMIC_VAR_INIT(1);
data1++;
data1 += 1;
data1.fetch_add(1);
atomic_fetch_add(&data1, 1);
atomic<int> data2 = ATOMIC_VAR_INIT(100);
data2--;
data2 -= 1;
data2.fetch_sub(1);
atomic_fetch_sub(&data2, 1);
cout << "data1: " << data1 << ", data2: " << data2 << endl;
return 0;
}