生活随笔
收集整理的這篇文章主要介紹了
Ice笔记--C++线程与并发(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
線程
1.Thread類
??? 1.1概述
?? ? ???? Ice中的基礎線程是由ThreadControl類和Thread類來提供的(在IceUtil/IceUtil.h中定義):
?? ???? ? Thread類是一個抽象基類,擁有一個純虛方法run。要創建線程,必須特化Thread類,并實現run方法。
??????1.2 其成員函數 ?????????? 1)id:該函數返回每個線程的唯一標識符,類型是ThreadID。在調用start函數之前調用它時,會引發ThreadNotStartedException。
?????????? 2)start:這個成員函數啟動新創建的線程,會調用run方法。start方法同時負責引用計數的加減。
?????????? 3)getThreadControl:這個成員函數返回它所在的線程控制對象。在調用start之前調用它同樣會觸發異常。
?????????? 4)operator== 、operator!=、operator<? 這些函數比較兩個線程的ID,目的是能降Thread對象用于有序的STL容器。
?????????? 5)特別注意:必須在堆上分配Thread對象,才能夠釋放正確。
?
2.ThreadControl類
??????? 2.1概述
????????????start方法返回的是類型為ThreadControl對象,指向發出調用的線程
??????? 2.2其成員函數
??????????? 1)ThreadControl:缺省構造器返回一個ThreadControl對象,指向發出調用的線程。
??????????? 2)id:該函數返回每個線程的唯一標識符,類型是ThreadID。
??????????? 3)join:這個方法掛起發起調用的線程,直到join所針對的線程終止為止。例如:
?????????????????? IceUtil::ThreadPtr t = new ReaderThread; // Create a thread ?????????????????? IceUtil::ThreadControl tc = t->start(); // Start it ?????????????????? tc.join(); // Wait for it
?????????? 4)detach
?????????????????? 這個方法分離一個線程。一旦線程分離,就不能再融合;因此必須保證線程在程序離開main函數之前終止。
?????????? 5)isAlive:如果底層的線程還沒有退出(run方法還沒有完成),該方法就返回真。該方法在實現非阻塞的join時很有用。
?????????? 6)sleep:這方法掛起線程,時間長度由Time決定。掛起線程就是讓該線程離開CPU,讓其他線程占用。
?????????? 7)yield:這個方法使得它所針對的線程放棄CPU,讓其他線程運行。看了它的代碼,發現yield的效果等于Sleep(0)。
?????????? 8)operator== 、operator!=、operator<? :和上面thread一樣.
?
2.實現線程
??????代碼?舉例說明實現線程(未經嚴格驗證):
//myQueue.h文件
[cpp] view plaincopy
<span?style="font-size:16px;" >#ifndef?MYQUEUE_H_?? #define?MYQUEUE_H_ ???? #include<IceUtil/Monitor.h> ??#include<IceUtil/Mutex.h> ???? #include?<list> ???? using ?namespace ?std;???? ?? template ?<class ?T>??class ?Queue?:?public ?IceUtil::Monitor<IceUtil::Mutex>??{?? public :??????Queue()?:?_waitingReaders(0),_waitingWriters(0){}?? ?? ????void ?put(const ?T&?item)?? ????{?? ????????IceUtil::Monitor<IceUtil::Mutex>::Lock?lock(*this );?? ????????while (_q.size()>10)?? ????????{?? ????????????try {?? ????????????????++_waitingWriters;?? ????????????????wait();?? ????????????????--_waitingWriters;?? ????????????}catch (...){?? ????????????????--_waitingWriters;?? ????????????????throw ;?? ????????????}?? ????????}?? ????????_q.push_back(item);?? ?????????? ????????if (_waitingReaders||_waitingWriters)?? ????????{?? ????????????notify();?? ????????}?? ????}?? ?? ????T?get()?? ????{?? ????????IceUtil::Monitor<IceUtil::Mutex>::Lock?lock(*this );????????? ?? ????????while (_q.size()?==?0)?? ????????{?? ????????????try {?? ?????????????????? ????????????????++_waitingReaders;?? ????????????????wait();?? ????????????????--_waitingReaders;?? ????????????}catch ?(const ?IceUtil::Exception?&e)?? ????????????{?? ????????????????--_waitingReaders;?? ????????????????throw ;?? ????????????}?? ????????}?? ????????T?item?=?_q.front();?? ????????_q.pop_front();?? ????????return ?item;?? ????}?? private :??????list<T>?_q;?? ????short ?_waitingReaders;?? ????short ?_waitingWriters;?? };?? ?? #endif</span> ??
//myMain.cpp文件
[cpp] view plaincopy
<span?style="font-size:16px;" >#include?<myQueue.h>?? #include<vector> ??#include<IceUtil/Thread.h> ???? Queue<int >?q;?? ?? class ?ReaderThread?:?public ?IceUtil::Thread??{?? ????virtual ?void ?run()?? ????{?? ????????for (int ?i=0?;?i<5?;?++i)?? ????????{?? ????????????cout?<<?"read_value:" ?<<?(int )q.get()?<<?endl;?? ????????}?? ????}?? };?? ?? class ?WriterThread?:?public ?IceUtil::Thread??{?? ????virtual ?void ?run()?? ????{?? ????????for (int ?i=0;i<5;++i)?? ????????{?? ????????????q.put(i);?? ????????????cout<<?"write_value:" ?<<?i?<<?endl;?? ????????}?? ????}?? };?? ?? int ?main()??{?? ????vector<IceUtil::ThreadControl>?threads;?? ????int ?i;?? ?? ????for (i=0;i<5;++i)?? ????{?? ????????IceUtil::ThreadPtr?t?=?new ?WriterThread;?? ????????threads.push_back(t->start());?? ????}?? ?? ????for (i?=0;i<5;++i)?? ????{?? ????????IceUtil::ThreadPtr?t?=?new ?ReaderThread;?? ????????threads.push_back(t->start());?? ????}?? ?? ????for (vector<IceUtil::ThreadControl>::iterator?i?=?threads.begin();?? ????????i?!=?threads.end();?++i)?? ????{?? ????????i->join();?? ????}??? ????return ?0;?? }</span>??
總結
以上是生活随笔 為你收集整理的Ice笔记--C++线程与并发(二) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。