您的位置:首页 > 其它

poco库学习笔记(5) Poco::Thread

2012-09-27 18:22 281 查看
#include <Poco/Thread.h>

#include <Poco/Runnable.h>

#include <iostream>

class ReadThread:public Poco::Runnable{

virtual void run(){

for(int i=0;i<10;++i){

std::cout<<"helloworld"<<std::endl;

sleep(1);//睡眠1秒

}

}

};

class WriteThread:public Poco::Runnable{

virtual void run(){

for(int i=0;i<10;++i){

std::cout<<"we are the world"<<std::endl;

sleep(2);//睡眠2秒

}

}

};

int main(int argc,char** argv){

ReadThread read;

WriteThread write;

Poco::Thread readThread;

Poco::Thread writeThread;

readThread.start(read);//启动线程read

writeThread.start(write);//启动线程write

std::string tname1 = readThread.getName();//获得线程read的名字

std::string tname2 = writeThread.getName();//获得线程write的名字

std::cout<<"tname1->"<<tname1<<std::endl;

std::cout<<"tname2->"<<tname2<<std::endl;

readThread.join();//等待线程执行完毕

writeThread.join();//等待线程执行完毕

return 0;

}

PS:初写文章,文笔生涩之处,各位请见谅,若有疑问或者交流的,可加本人YY号:301558660
转载请注明出处:山水间博客,http://blog.csdn.net/linyanwen99/article/details/8026030
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: