Cocos2d-x 线程的使用及线程使用中遇到的问题

.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

通知主线程运行某方法也不行,我到如今还不知道怎样去解决问题的方法。

原文地址:https://www.cnblogs.com/mqxnongmin/p/10488812.html