IT/C++
[C++] Thread 정리하기
wookiist
2020. 1. 15. 22:48
얼마 전에 C++로 구현해야 하는 작업이 있었습니다.
구현할 때 Thread를 이용해야해서, <thread> 라이브러리를 이용했는데, 해당 내용을 정리하고자 합니다.
"A thread of execution is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address space." - cplusplus.com
"스레드는
우선, thread를 이용하기 위해서는 <thread> 헤더를 include 해야 합니다.
아래는 cplusplus.com에서 소개하는 예제입니다.
#include <iostream>
#include <thread>
void foo()
{
// do stuff ...
}
void bar(int x)
{
// do stuff ...
}
int main()
{
std::thread first (foo); // spawn new thread that calls foo()
std::thread second (bar,0); // spawn new thread that calls bar(0)
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
std::cout << "foo and bar completed.\n";
return 0;
}
(추가 예정)
반응형