Cocos2d-x中多线程用的Pthread
工程中添加Pthread的类库
ThreadUtils.h
#ifndef _THREAD_UTILS_H_
#define _THREAD_UTILS_H_
#include "cocos2d.h"
#include "pthread.h"
using namespace cocos2d;
class ThreadUtils
{
public:
int start();
private:
pthread_t pid;
private:
static void * start_thread(void *); //静态成员函数,相当于C中的全局函数
};
#endif
PthreadUtils.cpp
#include "myheader.h"
#include "ThreadUtils.h"
#include "pthread.h"
int ThreadUtils::start()
{
char * p="hello";
if(pthread_create(&pid,NULL,start_thread,(void *)p) != 0) //创建一个线程(线程函数必须是全局函数)
{
return -1;
}
return 0;
}
void* ThreadUtils::start_thread(void *arg)
{ CCLog("%s",(char *)arg);
for(int i=0;i<=10;i++)
{
CCLog("new thread");
}
return NULL;
}