注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

逍遥子 曰:

得失失得 何必患得患失 舍得得舍 不妨不舍不得

 
 
 

日志

 
 

linux下多线程小程序[原]  

2013-01-30 15:38:32|  分类: linux |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
#include <iostream>
#include <stdlib.h>
#include<pthread.h>
const int MAX_SLEEP_TIME = 0;
using namespace std;
void* task_Move(void* pParam);
void* task_KeyUpDown(void* pParam);
int main()
{
cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ new test sleep time: final ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;
srand((unsigned)time(NULL));
pthread_t threadid_Move;
int iRes = 0;
iRes = pthread_create(&threadid_Move,NULL,task_Move,NULL);
if(0!=iRes)
cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^create thread:task_Move fail...."<<endl;
pthread_t threadid_KeyUpDown;
iRes = 0;
iRes = pthread_create(&threadid_KeyUpDown,NULL,task_KeyUpDown,NULL);
if(0!=iRes)
cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^create thread:task_KeyUpDown fail...."<<endl;

void* tret;
iRes = pthread_join(threadid_Move,&tret);
if(0!=iRes)
cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^cannot join with thread:threadid_Move ...."<<endl;
iRes = pthread_join(threadid_KeyUpDown,&tret);// wait for child thread end
if(0!=iRes)
cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^cannot join with thread:threadid_KeyUpDown ...."<<endl;
cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^main over.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;

}

void* task_Move(void* pParam)
{
int iKeyType = -1,iSleep = MAX_SLEEP_TIME;

while(true)
{
try
{
cout<<"------------------tast_Move-----------------"<<endl<<endl;
iKeyType = rand()%2;
cout<<"-----------iKeyType ="<<iKeyType<<endl;
cout<<"-----------after KMuinput_ButtonEvent"<<endl;
iSleep = 60000+rand()%1000;
cout<<"-----------iSleep = "<<iSleep<<endl<<endl;
usleep(iSleep);
}
catch(...)
{
cout<<"-----------*********************************************************"<<endl;
cout<<"-----------exception,happened: task_Move"<<endl;
cout<<"-----------*********************************************************"<<endl;
}

}
cout<<"-----exit------exit-------tast_Move-----exit--------exit----"<<endl;
return (void*)0;
}

void* task_KeyUpDown(void* pParam)
{
int iX, iY,iSleep = MAX_SLEEP_TIME;
//srand((unsigned)time(NULL));
while(true)
{
try
{
cout<<"+++++++++++++++++tast_KeyUpDown+++++++++++++++++"<<endl<<endl;
iX = rand()%200;
iY = rand()%200;
cout<<"+++++++++(iX "<<iX<<", iY "<<iY<<" ) "<<endl;
iSleep = rand()%100;
cout<<"+++++++++iSleep = "<<iSleep<<endl<<endl;
usleep(iSleep);
}
catch(...)
{
cout<<"+++++++++*********************************************************"<<endl;
cout<<"+++++++++exception,happened: task_KeyUpDown"<<endl;
cout<<"+++++++++*********************************************************"<<endl;
}

}
cout<<"+++++++++++++++++exit+++++++++++++++++exit+++++++++++++++++tast_KeyUpDown+++++++++++++++++exit+++++++++++++++++"<<endl;
return (void*)0;
}

  评论这张
 
阅读(468)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018