cocos2d-x 多线程以及线程同步

cocos2d-x 多线程以及线程同步

cocos2d-x引擎在内部实现了一个庞大的主循环,每帧之间更新界面,如果耗时的操作放到了主线程中,游戏的界面就会卡,这是不能容忍的,游戏最基本的条件就是流畅性,这就是为什么游戏开发选择C++的原因。另外现在双核手机和四核手机越来越普遍了,是时候使用多线程来挖掘硬件的潜力了。

1.环境搭建

cocos2d-x中的多线程使用pthread就可以实现跨平台,而且也不是很难理解。使用pthread需要先配置一下工程。右击工程----->属性----->配置属性---->链接器----->输入---->附加依赖项中添加pthreadVCE2.lib,如下图

 

接着添加附加包含目录,右击项目,属性----->C/C++---->常规----->附加包含目录加入pthread头文件所在的目录

 

这样,环境就搭建起来了。

2.多线程的使用

使用pthread来实现多线程,最重要的一个函数是

PTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid,//线程的标示
                            const pthread_attr_t * attr,      //创建线程的参数
                            void *(*start) (void *),          //入口函数的指针
                            void *arg);                       //传递给线程的数据

在HelloWorldScene.h文件中

pthread_t pidrun,pidgo;
static void* th_run(void *r);
static void* th_go(void *r);

定义了两个函数和两个线程的标识。

然后自定义了一个类,用于给线程传递数据。Student类如下:

复制代码
#pragma once
#include <string>
class Student
{
public:
    Student(void);
    Student(std::string name,int age,std::string sex);
    ~Student(void);

    std::string name;
    int age;
    std::string sex;

};
复制代码

源文件如下

复制代码
#include "Student.h"
#include "cocos2d.h"

Student::Student(void)
{
}


Student::~Student(void)
{
    cocos2d::CCLog("delete data");
}
Student::Student(std::string name,int age,std::string sex)
{
    this->name=name;
    this->age=age;
    this->sex=sex;
}
复制代码

在退出菜单的回调函数中启动两个线程:

复制代码
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
   
    Student *temp=new Student(std::string("zhycheng"),23,std::string("male"));
    pthread_mutex_init(&mutex,NULL);
    pthread_create(&pidrun,NULL,th_run,temp);//启动线程
    pthread_create(&pidgo,NULL,th_go,0);

}
复制代码

可以看到,将Student的指针传递给了pidrun线程,那么在pidrun线程中获得Student信息如下:

Student *s=(Student*)(r);
CCLog("name is %s,and age is %d,sex is %s",s->name.c_str(),s->age,s->sex.c_str());
delete s;

3.线程同步

使用了线程,必然就要考虑到线程同步,不同的线程同时访问资源的话,访问的顺序是不可预知的,会造成不可预知的结果。

 

这里使用pthread_mutex_t来实现同步,下面我来演示一下使用多线程实现卖票系统。卖票的时候,是由多个窗口同时卖票,这里要做到一张票不要卖出去两次,不要出现有票却无法卖的结果。

在线程函数th_run和th_go中来卖票,票的数量是一个全局变量,每卖出去一张票,就将票的数量减一。其中同步的pthread_mutex_t也是一个全局变量,就用它来实现线程同步。

复制代码
void* HelloWorld::th_run(void *r)
{
    
    Student *s=(Student*)(r);
    CCLog("name is %s,and age is %d,sex is %s",s->name.c_str(),s->age,s->sex.c_str());
    delete s;
    while(true)
    {
        pthread_mutex_lock(&mutex);
        if(ticket>0)
        {
        CCLog("thread run sell %d",ticket);
        ticket--;
        pthread_mutex_unlock(&mutex);
        }
        else
        {
            pthread_mutex_unlock(&mutex);
            break;    
        }
    
        Sleep(1);
        //Usleep(10);
    }

    return NULL;
}
复制代码
复制代码
void* HelloWorld::th_go(void *r)
{
    
    while(true)
    {
        pthread_mutex_lock(&mutex);
        if(ticket>0)
        {
        CCLog("thread go sell %d",ticket);
        ticket--;
        pthread_mutex_unlock(&mutex);
        }
        else
        {
            pthread_mutex_unlock(&mutex);
            break;
            
        }
        
        Sleep(1);
        
    }
    return NULL;
}
复制代码

mutex被锁定后,其他线程若再想锁定mutex的话,必须等待,当该线程释放了mutex之后,其他线程才能锁定mutex。Sleep()函数可以使得该线程休眠,单位是毫秒。下面是卖票的结果:

复制代码
name is zhycheng,and age is 23,sex is male
delete data
thread run sell 100
thread run sell 99
thread go sell 98
thread go sell 97
thread run sell 96
thread go sell 95
thread go sell 94
thread run sell 93
thread go sell 92
thread run sell 91
thread go sell 90
thread go sell 89
thread run sell 88
thread go sell 87
thread run sell 86
thread go sell 85
thread run sell 84
thread go sell 83
thread run sell 82
thread go sell 81
thread run sell 80
thread go sell 79
thread run sell 78
thread go sell 77
thread run sell 76
thread go sell 75
thread run sell 74
thread go sell 73
thread run sell 72
thread go sell 71
thread run sell 70
thread go sell 69
thread go sell 68
thread run sell 67
thread go sell 66
thread run sell 65
thread go sell 64
thread run sell 63
thread go sell 62
thread run sell 61
thread go sell 60
thread run sell 59
thread go sell 58
thread run sell 57
thread go sell 56
thread run sell 55
thread go sell 54
thread run sell 53
thread run sell 52
thread go sell 51
thread run sell 50
thread go sell 49
thread run sell 48
thread go sell 47
thread run sell 46
thread go sell 45
thread run sell 44
thread run sell 43
thread go sell 42
thread run sell 41
thread run sell 40
thread go sell 39
thread run sell 38
thread run sell 37
thread run sell 36
thread run sell 35
thread go sell 34
thread run sell 33
thread run sell 32
thread go sell 31
thread run sell 30
thread run sell 29
thread run sell 28
thread run sell 27
thread run sell 26
thread run sell 25
thread go sell 24
thread run sell 23
thread go sell 22
thread go sell 21
thread run sell 20
thread go sell 19
thread run sell 18
thread run sell 17
thread go sell 16
thread run sell 15
thread go sell 14
thread go sell 13
thread run sell 12
thread go sell 11
thread go sell 10
thread run sell 9
thread go sell 8
thread run sell 7
thread go sell 6
thread go sell 5
thread run sell 4
thread go sell 3
thread run sell 2
thread run sell 1
复制代码

可以看到,这个打印结果正确无误。如果不加mutex会是什么样的结果呢,我将线程同步的mutex注释掉,输出的结果为:

复制代码
name is zhycheng,and age is 23,sex is male
delete data
thread run sell 100
thread run sell 99
thread run sell 98
thread go sell 97
thread run sell 96
thread go sell 95
thread run sell 94
thread go sell 94
thread run sell 92
thread run sell 91
thread go sell 90
thread run sell 89
thread go sell 88
thread run sell 87
thread run sell 86
thread go sell 86
thread go sell 84
thread run sell 83
thread go sell 82
thread run sell 81
thread go sell 80
thread run sell 79
thread run sell 78
thread go sell 77
thread run sell 76
thread run sell 75
thread go sell 74
thread run sell 73
thread go sell 72
thread run sell 71
thread go sell 70
thread go sell 69
thread run sell 68
thread go sell 67
thread go sell 66
thread run sell 65
thread go sell 64
thread go sell 63
thread run sell 62
thread go sell 61
thread run sell 60
thread run sell 59
thread run sell 58
thread run sell 57
thread run sell 56
thread run sell 55
thread go sell 54
thread run sell 54
thread go sell 52
thread run sell 52
thread go sell 50
thread run sell 50
thread go sell 49
thread run sell 47
thread go sell 47
thread go sell 45
thread run sell 45
thread run sell 43thread go sell 43

thread run sell 41
thread go sell 41
thread go sell 39
thread run sell 39
thread run sell 37
thread go sell 37
thread go sell 35
thread run sell 35
thread go sell 33thread run sell 33

thread go sell 31thread run sell 31

thread go sell 29
thread run sell 29
thread go sell 27
thread run sell 27
thread go sell 25
thread run sell 25
thread run sell 23
thread go sell 23
thread run sell 21
thread go sell 21
thread go sell 19
thread run sell 19
thread run sell 17
thread go sell 17
thread go sell 15
thread run sell 15
thread run sell 13
thread go sell 13
thread run sell 11thread go sell 11

thread go sell 9
thread run sell 9
thread run sell 7
thread go sell 7
thread go sell 5thread run sell 5

thread go sell 3
thread run sell 3
thread go sell 1
thread run sell 1
复制代码

可以看到,有的票卖了两次,有的票就没卖。

 

4.注意

1.Sleep()函数是使得线程休眠的函数,这个函数不跨平台,仅仅在windows上能用,其他平台使用usleep。

2.在非主线程中不能使用cocos2d-x管理内存的CCObject::retain()CCObject::release() 者CCObject::autorelease(),因为CCAutoreleasePool不是线程安全的,OPENGL的上下文也不是线程安全的,所以不要再非主线程中使用cocos2d-x的API和UI操作。

转自:http://blog.csdn.net/zhy_cheng/article/details/9116479

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值