前端之家收集整理的这篇文章主要介绍了
cocos2d-x多线程(二)线程锁,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
xxx.h文件:@H_301_2@
int count;
void threadA();
void threadB();
xxx.cpp文件:@H_301_2@
std::thread t1(&Login::threadA,this);
t1.detach();
std::thread t2(&Login::threadB,this);
t2.detach();
count = 0;
void Login::threadA()
{
while (count < 10) {
_mutex.lock();
count++;
this_thread::sleep_for(chrono::seconds(3));
CCLOG("A Thread Id is: %x,count value is %d",this_thread::get_id(),count);
_mutex.unlock();
}
}
void Login::threadB()
{
while (count < 10) {
_mutex.lock();
count++;
this_thread::sleep_for(chrono::seconds(3));
CCLOG("B Thread Id is: %x,count);
_mutex.unlock();
}
}