개요
단일 실행 스레드
생성자의 인자로 전달된 함수를 객체 생성 즉시 실행
spurious wakeup 에 유의
멤버 함수
observers
joinable()
get_id()
native_handle()
hardware_concurrency()
operations
join()
detach()
스레드가 스레드 핸들과 독립적으로 실행되도록 허용
swap()
예제
코드
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <vector>
using namespace std ;
atomic < bool > condition ;
mutex m ;
mutex mCV ;
condition_variable cv ;
queue < string > q ;
void producer ( const string & content ) {
if ( condition == false ) {
return ;
}
{
lock_guard < mutex > lock ( m );
q . push ( content );
cout << "push : " << this_thread :: get_id () << " : " << content << endl ;
}
cv . notify_one ();
this_thread :: sleep_for ( chrono :: seconds ( 1 ));
}
void consumer () {
while ( condition ) {
{
unique_lock < mutex > lock ( mCV );
cv . wait ( lock , [ & ]() { return q . size () || condition == false ; });
if ( q . empty () && condition == false ) {
break ;
}
{
lock_guard < mutex > lock ( m );
cout << "front : " << this_thread :: get_id () << " : "
<< q . front () << endl ;
q . pop ();
}
}
}
}
void run () {
cout << "thread::hardware_concurrency() : "
<< thread :: hardware_concurrency () << endl ;
thread t ([]() {
cout << "this_thread::get_id() : " << this_thread :: get_id () << endl ;
});
cout << "t.get_id() : " << t . get_id () << endl ;
cout << "t.joinable() : " << t . joinable () << endl ;
t . join ();
cout << "t.joinable() : " << t . joinable () << endl ;
cout << endl << "------" << endl << endl ;
condition . store ( true );
vector < thread > v1 ;
v1 . clear ();
map < int , thread > m1 ;
m1 . clear ();
for ( int i = 0 ; i < 3 ; ++ i ) {
// m1[i] = thread(producer, to_string(i));
v1 . push_back ( thread ( producer , to_string ( i )));
}
vector < thread > v2 ;
v2 . clear ();
map < int , thread > m2 ;
m2 . clear ();
for ( int i = 0 ; i < 3 ; ++ i ) {
// m2[i] = thread(consumer);
v2 . push_back ( thread ( consumer ));
}
for ( auto & iter : v1 ) {
iter . join ();
}
condition . store ( false );
cv . notify_all ();
for ( auto & iter : v2 ) {
// cout << iter.get_id() << " : joinable 1 - " << iter.joinable()
//<< endl;
iter . join ();
// cout << iter.get_id() << " : joinable 2 - " << iter.joinable()
//<< endl;
}
}
int main () {
run ();
return 0 ;
}
실행 결과
thread::hardware_concurrency() : 2
t.get_id() : 140429792114240
t.joinable() : 1
this_thread::get_id() : 140429792114240
t.joinable() : 0
------
push : 140429792114240 : 0
push : 140429783721536 : 1
push : 140429775328832 : 2
front : 140429766936128 : 0
front : 140429766936128 : 1
front : 140429766936128 : 2
Tags:
C++ ,
C++11 ,
condition_variable ,
detach ,
get_id ,
hardware_concurrency ,
join ,
join ,
lock_guard ,
mutex ,
native_handle ,
notify_all ,
notify_one ,
observers ,
operations ,
programming-language ,
swap ,
thread ,
unique_lock
Categories:
C++ ,
programming-language
Updated: August 14, 2022