.h文件:
#if CC_PLATFORM_ANDROID == CC_TARGET_PLATFORM
#include "pthread.h"
#endif
#if CC_PLATFORM_WIN32 == CC_TARGET_PLATFORM
#include "pthread/pthread.h"
#endif
pthread 變量及方法的聲明:
//多線程
static void* onReceiveMesFromServer(void* arg);
pthread_t pid;
.cpp文件:
在構造函數中添加:
pthread_create(&pid,NULL,MoreAbout::onReceiveMesFromServer,this);
具體方法的實現:
void* MoreAbout::onReceiveMesFromServer(void* arg)
{
pthread_detach(pthread_self());
while(1)
{
CCLog("MoreAbout::onReceiveMesFromServer(void* arg)");
#if CC_PLATFORM_WIN32 == CC_TARGET_PLATFORM
Sleep(5000);
#endif
#if ANDROID
sleep(0.5);
#endif
}
}
線程使用時遇到的問題:
線程使用不會卡到主線程,但是子線程使用時在Android與Cocos2d-x通信時會出現無法調用Jni方法的問題,然後試圖用子線程調用主線程中的方法也不行,用CCNotification
通知主線程執行某方法也不行,我到現在還不知道如何去解決這個問題的方法。