很早就买了c++编程思想第二卷这本书 可惜最后一章一直没有看。于是找个时间看看多线程也比较好啊
使用编译好的zthread库
自己先写个简短的例子:
如下:
然后再来一段可以同步处理响应的代码段
好好研究研究zthread打算以后用用
使用编译好的zthread库
自己先写个简短的例子:
如下:
- #include <zthread/runnable.h>
- #include <zthread/thread.h>
- #pragma comment(lib,"ZThread_Z.lib")
- class thread : public ZThread::Runnable
- {
- public:
- thread(int _id): id(_id){}
- ~thread(){}
- void run()
- {
- printf("i am thread: %d ./n", id);
- }
- private:
- int id;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- try
- {
- int itr = 0;
- while(itr++<5)
- ZThread::Thread th(new thread(itr));
- printf("start /n");
- }
- catch(ZThread::Synchronization_Exception& e)
- {
- printf("%s",e.what());
- }
- _sleep(20000);
- return 0;
- }
- #include <zthread/runnable.h>
- #include <zthread/thread.h>
- #include <iostream>
- #pragma comment(lib,"ZThread_Z.lib")
- class thread : public ZThread::Runnable
- {
- public:
- thread(int _id): id(_id){}
- ~thread(){}
- void run()
- {
- int i =0 ;
- while(i++<100)
- {
- printf("i am thread: %d ./n", id);
- _sleep(1200);
- }
- }
- private:
- int id;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- try
- {
- int itr = 0;
- while(itr++<8)
- ZThread::Thread th(new thread(itr));
- ZThread::Thread(th);
- char ch;
- std::cin>>ch;
- std::cout<<"your input is: "<<ch<<std::endl;
- printf("start /n");
- }
- catch(ZThread::Synchronization_Exception& e)
- {
- printf("%s",e.what());
- }
- _sleep(20000);
- return 0;
- }
好好研究研究zthread打算以后用用