标签:cocos2dx 线程 vs2012 c++11 thread
虽然内容是抄过来的,但是经过了我的验证,而且放在一起就清楚很多了,cocos2dx版本经常变化很大,总会导致这样那样的问题。
#include <pthread.h>
...
pthread_t serial_thread_id; // 起这个名字本打算用在socket上的
int serialThreadStart(void);// 启动线程的方法
static void* serialReceiverFun(void *arg);// 被启动的线程函数,注意必须是静态方法
......
int HelloWorld::serialThreadStart()
{
int errCode=0;
do {
pthread_attr_t tAttr;
errCode=pthread_attr_init(&tAttr);
CC_BREAK_IF(errCode!=0);
errCode=pthread_attr_setdetachstate(&tAttr, PTHREAD_CREATE_DETACHED);
if(errCode!=0)
{
pthread_attr_destroy(&tAttr);
break;
}
errCode=pthread_create(&serial_thread_id, &tAttr, serialReceiverFun, this);
CCLOGERROR("serial_thread_id=%d\n",&serial_thread_id);
} while (0);
return errCode;
}
void* HelloWorld::serialReceiverFun(void *arg)
{
CCLOGERROR("serial thread start");
while(true)
{
char buff[BUFSIZE]={0};
int readSize = 0;
readSize = receiverDate(buff,BUFSIZE);
if(readSize > 0)
{
CCLOGERROR("readSize=%d,%s\n",readSize,buff);
sendDate(buff,readSize);
}
}
return NULL;
}
...this->serialThreadStart();
#include <thread>
...
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
std::thread t1(&HelloWorld::myThread,this);//创建一个分支线程,回调到myThread函数里
// t1.join();
t1.detach();
CCLOG("in major thread");//在主线程
return true;
}
void HelloWorld::myThread()
{
CCLOG("in my thread");
}#include <thread>
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
std::thread t1(&HelloWorld::myThread,this,10,20);//创建一个分支线程,回调到myThread函数里
//t1.join();
t1.detach();
CCLOG("in major thread");//在主线程
return true;
}
void HelloWorld::myThread(int first,int second)
{
CCLOG("in my thread,first = %d,second = %d",first,second);
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
std::thread t1(&HelloWorld::myThread,this,10,20);//创建一个分支线程,回调到myThread函数里
//t1.join();
t1.detach();
CCLOG("in major thread");//在主线程
return true;
}
void HelloWorld::myThread(int first,int second)
{
CCLOG("in my thread,first = %d,second = %d",first,second);
}
cocos2dx2.0 与cocos2dx3.1 创建线程不同方式总结,布布扣,bubuko.com
cocos2dx2.0 与cocos2dx3.1 创建线程不同方式总结
标签:cocos2dx 线程 vs2012 c++11 thread
原文地址:http://blog.csdn.net/iasxk/article/details/30221233