多线程的实现:
PthreadTest.h
#ifndef __MTNotificationQueue_h
#define __MTNotificationQueue_h
#include "cocos2d.h"
#include <iostream>
#include"pthread\pthread.h"
USING_NS_CC;
using namespace std;
//线程安全的消息队列
class MTNotificationQueue : public CCObject
{
//队列对象
static MTNotificationQueue * mInstance;
class CGarbo
{
public:
~CGarbo()
{
if (MTNotificationQueue::mInstance)
delete MTNotificationQueue::mInstance;
mInstance = NULL;
}
};
static CGarbo Garbo;
typedef struct
{
string name;
CCObject* object;
} NotificationArgs;
//消息队列
vector<NotificationArgs> notifications;
MTNotificationQueue(void);
public:
static MTNotificationQueue* sharedNotificationQueue();
void postNotifications(float dt);
~MTNotificationQueue(void);
void postNotification(const char* name, CCObject* object);
};
#endif
PthreadTest.cpp
#include "PthreadTest.h"
//互斥锁,全局唯一
pthread_mutex_t sharedNotificationQueueLock;
//管理互斥锁的生命周期
class LifeManager_PThreadMutex
{
pthread_mutex_t* mutex;
public:
LifeManager_PThreadMutex(pthread_mutex_t* mut) : mutex(mut)
{
//初始化
pthread_mutex_init(mutex, NULL);
}
~LifeManager_PThreadMutex()
{
//销毁
pthread_mutex_destroy(mutex);
}
}__LifeManager_sharedNotificationQueueLock(&sharedNotificationQueueLock);
//生命周期锁类,管理锁开关
class LifeCircleMutexLocker
{
pthread_mutex_t* mutex;
public:
LifeCircleMutexLocker(pthread_mutex_t* aMutex) : mutex(aMutex)
{
//上锁
pthread_mutex_lock(mutex);
}
~LifeCircleMutexLocker(){
//解锁
pthread_mutex_unlock(mutex);
}
};
#define LifeCircleMutexLock(mutex) LifeCircleMutexLocker __locker__(mutex)
MTNotificationQueue* MTNotificationQueue::mInstance = NULL;
MTNotificationQueue::MTNotificationQueue(void)
{
}
MTNotificationQueue::~MTNotificationQueue(void)
{
}
//消息队列的单例
MTNotificationQueue* MTNotificationQueue::sharedNotificationQueue()
{
if (!mInstance)
{
mInstance = new MTNotificationQueue();
}
return mInstance;
}
//消息队列的处理
void MTNotificationQueue::postNotifications(float dt)
{
LifeCircleMutexLock(&sharedNotificationQueueLock);
for(uint16_t i = 0; i < notifications.size(); i++) {
NotificationArgs &arg = notifications[i];
// void postNotification(const char *name);
// 发送一个消息
CCNotificationCenter::sharedNotificationCenter()->
postNotification(arg.name.c_str(), arg.object);
}
notifications.clear();
}
//单个消息加入
void MTNotificationQueue::postNotification(const char* name, CCObject* object)
{
LifeCircleMutexLock(&sharedNotificationQueueLock);
NotificationArgs arg;
arg.name = name;
if(object != NULL)
arg.object = object; //object->copy();
else
arg.object = NULL;
notifications.push_back(arg);
}
BackgroundLayer.cpp
#include "BackgroundLayer.h"
#include "User.h"
#include"PthreadTest.h"
USING_NS_CC;
CCScene* BackgroundLayer::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
BackgroundLayer *layer = BackgroundLayer::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
CCImage *bgImage;
void* loadImages(void* arg)
{
bgImage = new CCImage();
bgImage->initWithImageFileThreadSafe("HelloWorld.png");
// 发送一个已经完成加载的消息
MTNotificationQueue::sharedNotificationQueue()->
postNotification("loadImageFinish", NULL);
return NULL;
}
void BackgroundLayer::loadImageFinish(CCObject* sender)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//加载图片
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->
addUIImage(bgImage, "HelloWorld.png");
//释放bgImage内存,因为bgImage为进程间的共享数据
bgImage->release();
CCSprite* bg = CCSprite::createWithTexture(texture);
CCSize size = bg->getContentSize();
bg->setPosition(ccp(winSize.width / 2, winSize.height / 2));
float f = max(winSize.width/size.width,winSize.height/size.height);
bg->setScale(f);
this->addChild(bg);
}
bool BackgroundLayer::init()
{
//启动消息队列定时器,使postNotifications每帧都被调用
CCDirector::sharedDirector()->getScheduler()->scheduleSelector(
schedule_selector(MTNotificationQueue::postNotifications),
MTNotificationQueue::sharedNotificationQueue(),
1.0 / 60.0,
false);
bool bRet = false;
do {
CC_BREAK_IF(! CCLayer::init());
//注册消息
// void addObserver(CCObject *target, //接收消息的对象
// SEL_CallFuncO selector, //响应消息的函数
// const char *name, //待接收的消息
// CCObject *obj); //指定消息的发送者,目前暂时为无用参数
CCNotificationCenter::sharedNotificationCenter()->addObserver(
this,
callfuncO_selector(BackgroundLayer::loadImageFinish),
"loadImageFinish",
NULL);
pthread_t tid;
// 创建线程,loadImages为入口指针
pthread_create(&tid, NULL, &loadImages, NULL);
bRet = true;
} while (0);
return bRet;
}