惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - leonbao

Unity3D - 关于Dynamic和Static HTML5 - HTML5 postMessage API 注意事项 Cocos2d-x——支持多触点 Cocos2d-x——Cocos2d-x 屏幕适配总结 Cocos2d-x——Cocos2d-x 屏幕适配新解 – 兼容与扩展【转载】 Cocos2d-x——Cocos2d-x 屏幕适配新解【转载】 Unity3D-Baked Lightmapping 示例学习 Cocos2d-x——CocosBuilder官方帮助文档翻译3 动画 Cocos2d-x——CocosBuilder官方帮助文档翻译2 多分辨率支持 人工智能-有限状态机(FSM)的学习 Cocos2d-x——CocosBuilder官方帮助文档翻译1 使用自定义类 高性能服务器-关于游戏服务器中多线程的使用 Unity3D-UnityVS的安装和使用 高性能服务器-多线程的再次学习 Programming Windows Workflow Foundation第六章-工作流宿主翻译完成 关于SQL Server数据库设计的感悟,请指教 关于分层结构的感悟,请指教 NHibernate 连接 Access数据库的配置文件 - leonbao [原创]关于多层设计想到的问题-涉及Nhibernate和Log4Net
Cocos2d-x——pthread的使用注意事项
leonbao · 2013-06-21 · via 博客园 - leonbao

1:多线程所调用的成员方法定义为static。

2:互斥锁(pthread_mutex_t)定义在cpp文件的开头,并且也定义为static。

3:pthread_mutex_init方法尽量在最早的时候进行调用初始化(绝对不要在初始化之后立即开始新线程,否则pthread_mutex_lock很可能会返回22的错误,因为此时互斥量还没有初始化完成)。

4:pthread_mutex_destroy方法尽量在最晚的匹配的时候调用(比如构造析构——配对)。

代码:

头文件

public:
pthread_t tid1;
pthread_t tid2;

static void* anotherTest1(void* args);
static void* anotherTest2(void* args);

类文件:

static pthread_mutex_t mylock1;

HelloWorld::HelloWorld()
{
  pthread_mutex_init(&mylock1, NULL);
}

HelloWorld::~HelloWorld()
{
  pthread_mutex_destroy(&mylock1);
}

void* HelloWorld::anotherTest1(void* args)
{
  int intResult1 = pthread_mutex_lock(&mylock1);
  CCLOG("Result1:%d", intResult1);

  for(int i=0;i<=10; i++) {
    CCLOG("1-%d",i);
    //sleep(1);
  }

  pthread_mutex_unlock(&mylock1);
  return NULL;
}

void* HelloWorld::anotherTest2(void* args)
{
  int intResult2 = pthread_mutex_lock(&mylock1);
  CCLOG("Result2:%d", intResult2);

  for(int j=0;j<=10; j++) {
    CCLOG("2-%d",j);
    //sleep(1);
  }

pthread_mutex_unlock(&mylock1);
  return NULL;
}

void HelloWorld::menuStartNewThread(CCObject* pSender)
{
  pthread_create(&tid1, NULL, anotherTest1, NULL);
  pthread_create(&tid2, NULL, anotherTest2, NULL);
}