std::mutex

c++ 2015. 1. 6. 19:49

printf("mutex\n");

std::mutex mtx_lock;

std::thread Threads([&]()

{

for ( int i = 0; i < 5; ++i )

{

mtx_lock.lock();

std::cout<<"Thread1 Num:"<<i<<std::endl;

mtx_lock.unlock();

}

});

std::thread Threads3([&]()

{

for ( int i = 10; i < 15; ++i )

{

mtx_lock.lock();

std::cout<<"Thread2 Num:"<<i<<std::endl;

mtx_lock.unlock();

}

});

std::thread Threads2([&]()

{

for ( int i = 15; i < 20; ++i )

{

mtx_lock.lock();

std::cout<<"Thread3 Num:"<<i<<std::endl;

mtx_lock.unlock();

}

});


if ( Threads.joinable() )

Threads.join();

if ( Threads2.joinable() )

Threads2.join();

if ( Threads3.joinable() )

Threads3.join();


결과

Thread1

Thread2

Thread3

Thread1

Thread2

Thread3

Thread1

Thread2

Thread3

Thread1

Thread2

Thread3

Thread1

Thread2

Thread3

순차적으로 출력

'c++' 카테고리의 다른 글

함수포인터, std::function, std::bind  (0) 2015.01.07
c++11 <random >  (0) 2015.01.06
std::thread (2)  (0) 2015.01.06
std::thread  (0) 2015.01.06
std::chrono  (0) 2015.01.06
Posted by 에브리피플
,