多线程--pthread_create, pthread_join
2014-10-07 10:40
344 查看
先看例子代码:
void CStudent::menuCallback(CCObject * pSender) { CStudent *temp = new CStudent("new student", 33); pthread_mutex_init(&mutex, NULL);//create a mutex pthread_create(&pidRun, NULL, run, temp); //create a thread void *run_thread_status = NULL; pthread_join(pidRun, &run_thread_status); // wait the thread dead,then execute the other threads pthread_create(&pidGo, NULL, go, 0); void *thrid_thread_status = NULL; pthread_join(pidGo, &thrid_thread_status); pthread_create(&pidThrid, NULL, thrid,NULL); } void CStudent::closeCallback(CCObject * pSender) { } void *run(void *r) { CStudent *pStudent = (CStudent *)r; do { CC_BREAK_IF(pStudent == 0); CCLog("Name: %s, age: %d\n", pStudent->getName(), pStudent->getAge()); while(true) { pthread_mutex_lock(&mutex); if(ticket > 20) { CCLog("Thread Run Sell ticket: %d\n", ticket); --ticket; pthread_mutex_unlock(&mutex); } else { pthread_mutex_unlock(&mutex); break; } Sleep(1); } } while(false); return NULL; } void *go(void *g) { while(true) { pthread_mutex_lock(&mutex); if(ticket > 10 && ticket <= 20) { CCLog("Go Thread Sell ticket: %d\n", ticket); --ticket; pthread_mutex_unlock(&mutex);// the the mutex unsingled } else { pthread_mutex_unlock(&mutex); break; } } Sleep(1); return NULL; } void *thrid(void *t) { while(true) { pthread_mutex_lock(&mutex); if(ticket > 0 && ticket <= 10) { CCLog("Thrid thread Sell ticket: %d\n", ticket); --ticket; pthread_mutex_unlock(&mutex); } else { pthread_mutex_unlock(&mutex); break; } } return NULL; }
相关文章推荐
- linux下编译多线程程序, undefined reference to `pthread_create',undefined reference to `pthread_join'
- 多线程编程之pthread_create pthread_join pthread_attr_t
- 多线程函数系列pthread_create(), pthread_join(), pthread_self(),pthread_exit(), pthread_detach()实例详解
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- pthread_create创建线程后必须使用join或detach释放线程资源[内存泄漏]
- Pthread1 pthread_create pthread_join
- codeblocks 多线程编程时:对pthread_create未定义的引用
- linux 多线程时出错:Create pthread error! Resource temporarily unavailable
- clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结
- 线程pthread_create()、pthread_exit()、pthread_join()、pthread_cancel()
- 多线程其中一个没运行-关于pthread_join
- pthread_create和pthread_join使用
- Xcode c c++ 多线程 pthread_create
- codeblocks 多线程 pthread_create 函数未定义引用 解决办法
- clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结
- 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- Linux--多线程之线程连接pthread_join
- pthread_detach pthread_join pthread_create
- liunx 下多线程编译时出现对‘pthread_create’未定义的引用