【cocos2d-x官方文档】怎么使用pthread
2013-09-06 11:30
393 查看
原文连接:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/How_to_use_pthread
现在,你可以在cocos2d-x中使用pthread,但是有几点限制。
1.不要调用任何会引起
Count and AutoReleasePool in Cocos2d-x(cocos2d-x中的引用计数和自动释放池)”。在Cocos2d-x引擎中,CCAutoreleasePool的使用无处不在,所以我的建议就是:不要在新线程中点用任何cocos2d-x API除了数据结构。
2.如果你想要在新线程中加载资源,你可以调用
3.
如果我们确保retain(), release() 和 autorealese()是线程安全的,那么线程互斥就变得必须。因为cocos2d-x框架会在每个消息循环(message loop)最后释放自动释放池,使用线程互斥会引起性能问题。
顺便说一下,OpenGL上下文(OpenGL context)并不是线程安全的,这个你应该永远记住。
cocos2d-x/extensions/network/HttpClient.cpp使用了
下面简单的pthread例子显示了怎样在一个单独的线程中执行方法,怎样向线程方法传递结构并设置互斥。
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
struct SimpleStructure
{
int data;
float otherData;
};
void* ThreadFunction(void* arg)
{
pthread_mutex_lock(&mutex);
SimpleStructure* args = (SimpleStructure*)arg;
// do something with args->data and args->otherData
delete args;
pthread_mutex_unlock(&mutex);
return NULL;
}
void CreateThread()
{
pthread_t thread;
SimpleStructure* args = new SimpleStructure();
args->data = 1;
args->otherData = 2.0f;
pthread_create(&thread, NULL, &ThreadFunction, args);
}
现在,你可以在cocos2d-x中使用pthread,但是有几点限制。
1.不要调用任何会引起
CCObject::retain(),
CCObject::release()或者
CCObject::autorelease()的方法,因为CCAutoreleasePool并不是线程安全。更多详细介绍请参照“Reference
Count and AutoReleasePool in Cocos2d-x(cocos2d-x中的引用计数和自动释放池)”。在Cocos2d-x引擎中,CCAutoreleasePool的使用无处不在,所以我的建议就是:不要在新线程中点用任何cocos2d-x API除了数据结构。
2.如果你想要在新线程中加载资源,你可以调用
CCTextureCache::addImageAsync()
3.
pthread_cond_wait()似乎有bug,首次执行时并不能正常等待,但是,后续可以很好的工作。
如果我们确保retain(), release() 和 autorealese()是线程安全的,那么线程互斥就变得必须。因为cocos2d-x框架会在每个消息循环(message loop)最后释放自动释放池,使用线程互斥会引起性能问题。
顺便说一下,OpenGL上下文(OpenGL context)并不是线程安全的,这个你应该永远记住。
cocos2d-x/extensions/network/HttpClient.cpp使用了
pthread
和
pthread_mutex_t来创建网络线程。你可以看看使用示例。
下面简单的pthread例子显示了怎样在一个单独的线程中执行方法,怎样向线程方法传递结构并设置互斥。
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
struct SimpleStructure
{
int data;
float otherData;
};
void* ThreadFunction(void* arg)
{
pthread_mutex_lock(&mutex);
SimpleStructure* args = (SimpleStructure*)arg;
// do something with args->data and args->otherData
delete args;
pthread_mutex_unlock(&mutex);
return NULL;
}
void CreateThread()
{
pthread_t thread;
SimpleStructure* args = new SimpleStructure();
args->data = 1;
args->otherData = 2.0f;
pthread_create(&thread, NULL, &ThreadFunction, args);
}
相关文章推荐
- 【cocos2d-x官方文档】怎么使用pthread
- Objective-c官方文档 怎么使用对象
- 【cocos2d-js官方文档】二十四、如何在android平台上使用js直接调用Java方法
- 【cocos2d-js官方文档】二十四、如何在android平台上使用js直接调用Java方法
- 【cocos2d-js官方文档】十九、Cocos2d-JS单文件引擎使用指引
- 【cocos2d-js官方文档】二十四、如何在android平台上使用js直接调用Java方法
- Objective-c官方文档 怎么使用对象
- Cocos2d-x——CocosBuilder官方帮助文档翻译1 使用自定义类
- 【cocos2d-js官方文档】二十三、如何在IOS平台上使用js直接调用OC方法
- 【cocos2d-js官方文档】二十四、如何在android平台上使用js直接调用Java方法
- 【cocos2d-js官方文档】三、Bake功能使用说明
- [Cocos2D-X官方文档]:在Android上使用OpenSL ES来播放音效
- 【cocos2d-js官方文档】三、Bake功能使用说明
- Cocos2d-x v3.x 官方文档]C++版如何使用WebSocket
- django 1.8 官方文档翻译: 3-5-2 使用Django输出PDF
- Objective-c官方文档 怎么自定义类
- Redis 3.0官方文档翻译计划(8) ——使用Redis作为LRU缓存
- 如何使用苹果官方文档
- 在iOS中使用静态库(总结自官方文档Using Static Libraries in iOS)
- FMDB官方使用文档