多线程std::thread的使用 以及performFunctionInCocosThread函数
来源:互联网 发布:网络作家船长写的小说 编辑:程序博客网 时间:2024/06/06 02:54
原帖地址:
[cocos2dx 3.0 (二)] 多线程std::thread的使用 以及performFunctionInCocosThread函数
有的时候很多操作如果在cocos2dx的主线程中来调用,可能会极大地占用主线程的时间,从而使游戏的不流畅。比如在获取网络文件数据或者在数据比较大的游戏存档时,就需要使用多线程了。
网上的一些教程上是使用pthread来创建新线程的,需要加入lib和头文件,但在cocos2dx 3.0中并未发现有pthread的支持文件,后来才发现在c++11中已经拥有了一个更好用的用于线程操作的类std::thread。cocos2dx 3.0的版本默认是在vs2012版本,支持c++11的新特性,使用std::thread来创建线程简直方便。
1. std::thread的一些使用方法。
需要加头文件 #include<thread>
直接调用函数
- std::thread t(func);
- std::thread t(func,param1,param2...);
- class MyClass
- {
- void func1();
- void func2(int a,string b);
- }
- MyClass myclass;
- std::thread t1(&MyClass::func1,&myclass);
- std::thread t2(&MyClass::func2,&myclass,444,"this is a string");
- t1.detach();
- t2.join();
join() 等待子线程执行完之后,主线程才可以继续执行下去,此时主线程会释放掉执行完后的子线程资源。
可以看出使用std::thread来创建线程十分的方便,而且可读性更好了,特别是在传参数这方面。
还有就是线程的互斥量std::mutex
- std::mutex _mutex;
- void thread_fun1()
- {
- _mutex.lock();//加锁
- ...
- _mutex.unlock();//解锁
- }
- void thread_fun2()
- {
- std::lock_guard<std::mutex> lk(_mutex); //线程开始加锁,退出时自动解锁
- ...
- }
参考:
1.std::thread 学习初步
2.C++11 thread
2.performFunctionInCocosThread()
在cocos2dx中使用多线程,难免要考虑线程安全的问题。cocos2dx 3.0中新加入了一个专门处理线程安全的函数performFunctionInCocosThread()。他是Scheduler类的一个成员函数:
- void Scheduler::performFunctionInCocosThread(const std::function<void ()> &function)
- /** calls a function on the cocos2d thread. Useful when you need to call a cocos2d function from another thread.
- This function is thread safe.
- @since v3.0
- */
具体定义:
- void Scheduler::performFunctionInCocosThread(const std::function<void ()> &function)
- {
- _performMutex.lock();
- _functionsToPerform.push_back(function);
- _performMutex.unlock();
- }
使用这个函数就能安全的在其他线程中去控制cocos2dx的一些操作了。但是在使用时也要注意一些问题:
1.同步问题
因为使用performFunctionInCocosThread将参数函数中的代码放到主线程中去运行,所以就无法知道运行完这段代码需要多少时间,可能线程已经运行完毕退出了而那部分代码还没有执行完毕。
可以做一下测试:
- void thread_fun()
- {
- log("new thread create:t_id:0x%x",GetCurrentThreadId());
- Director::getInstance()->getScheduler()->performFunctionInCocosThread([&,this]
- {
- for(int i=0;i<=1000;i++){}
- log("[performFunctionInCocosThread] finished!");
- });
- log("thread finished:t_id:0x%x",GetCurrentThreadId());
- }
然后在cocos2dx中以这个函数创建一个线程,运行可以看到结果:
2.互斥问题
如果在线程中要使用互斥,而又要使用performFunctionInCocosThread的话。我认为应该将performFunctionInCocosThread调用放到线程的最后,然后在performFunctionInCocosThread调用函数的末尾使用mutex.unlock(),这样才能确保互斥。
在自己的线程中对精灵的创建等操作可能会没有用,所有performFunctionInCocosThread还是很有用的。
cocos2dx的线程学习就到这了
- 多线程std::thread的使用 以及performFunctionInCocosThread函数
- [cocos2dx 3.0 (二)] 多线程std::thread的使用 以及performFunctionInCocosThread函数
- 多线程std::thread的使用以及处理线程安全的函数
- 多线程std::thread的使用以及处理线程安全的函数
- C++11多线程std::thread的简单使用(上)
- C++11_多线程std::thread的使用
- C++11 多线程学习----std::thread类的简单使用
- 一、C++11多线程std::thread的简单使用(上)
- 二、C++11多线程std::thread的简单使用(下)
- C++11多线程std::thread的简单使用
- 一、C++11多线程std::thread的简单使用(上)
- 二、C++11多线程std::thread的简单使用(下)
- C++11多线程std::thread的简单使用
- C++11多线程std::thread的简单使用
- C++11多线程std::thread的简单使用
- Cocos2dx 3.0 过渡篇(二十六)C++11多线程std::thread的简单使用(上)
- Cocos2dx 3.0 过渡篇(二十七)C++11多线程std::thread的简单使用(下)
- Cocos2dx 3.0 过渡篇(二十六)C++11多线程std::thread的简单使用(上)
- Oracle中删除外键约束、禁用约束、启用约束
- android控件--ViewPager原理及实现
- jquery ajax前台传递数组
- 十道海量数据处理面试题与十个方法大总结
- C++学习笔记
- 多线程std::thread的使用 以及performFunctionInCocosThread函数
- Android支付接入(一):支付宝
- iOS在Release模式下屏蔽NSLog输出
- Android新建项目后src下没有自动生成文件
- jQuery学习笔记(三)jQuery遍历
- 黑马程序员--面对对象之继承的定义和使用
- ctypes 载入 dll 产生的 WindowsError: [Error 126] 错误原因
- HTML5 APP开发环境的框架搭建
- 人生真正的快乐