国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

淺析muduo庫中的線程設(shè)施02

2019-11-08 02:33:16
字體:
供稿:網(wǎng)友

接下來,聊一聊主題–Thread

/***Thread.h***/class Thread : boost::noncopyable //禁止拷貝{ public: typedef boost::function<void ()> ThreadFunc;//仿函數(shù)對(duì)象,利用回調(diào)的方式使用線程函數(shù) explicit Thread(const ThreadFunc&, const string& name = string());//普通的線程構(gòu)造函數(shù)#ifdef __GXX_EXPERIMENTAL_CXX0X__ explicit Thread(ThreadFunc&&, const string& name = string());//移動(dòng)的線程構(gòu)造函數(shù),比上面的更節(jié)省資源std::move#endif ~Thread();//析構(gòu)函數(shù) void start();//啟動(dòng)線程 int join(); // 類似于 pthread_join() bool started() const { return started_; } // pthread_t pthreadId() const { return pthreadId_; } pid_t tid() const { return *tid_; } //返回線程索引 const string& name() const { return name_; }//返回線程名字 static int numCreated() { return numCreated_.get(); } PRivate: void setDefaultName(); bool started_; //是否啟動(dòng) bool joined_; //是否終止 pthread_t pthreadId_; //線程索引 boost::shared_ptr<pid_t> tid_; //指向線程索引的智能指針 ThreadFunc func_; //線程主題函數(shù) string name_; //線程名字 static AtomicInt32 numCreated_; //static變量在所有的線程對(duì)象中共享,為由該類產(chǎn)生的線程排序};在muduo的線程對(duì)象封裝中,最精彩的是使用boost::function函數(shù)對(duì)象將線程函數(shù)以回調(diào)的方式傳遞進(jìn)線程對(duì)象中。

typedef boost::function<void ()> ThreadFun;

在多線程情況下,避免在對(duì)象外操作指向?qū)ο蟮闹羔樀那樾危梢栽谝欢ǔ潭壬媳WC了線程安全。/***Thread.cc***///兩種線程構(gòu)造函數(shù)Thread::Thread(const ThreadFunc& func, const string& n) : started_(false), joined_(false), pthreadId_(0), tid_(new pid_t(0)), func_(func), name_(n){ setDefaultName();}#ifdef __GXX_EXPERIMENTAL_CXX0X__Thread::Thread(ThreadFunc&& func, const string& n) : started_(false), joined_(false), pthreadId_(0), tid_(new pid_t(0)), func_(std::move(func)), name_(n){ setDefaultName(); }#endifThread::~Thread() { if (started_ && !joined_) //將該線程設(shè)置為分離屬性 { pthread_detach(pthreadId_); //線程結(jié)束將自動(dòng)回收資源 }}void Thread::setDefaultName() //設(shè)置線程名字,比如Thread1,Thread2等{ int num = numCreated_.incrementAndGet(); if (name_.empty()) { char buf[32]; snprintf(buf, sizeof buf, "Thread%d", num); name_ = buf; }}void Thread::start(){ assert(!started_); //斷言線程是否已經(jīng)開始運(yùn)行 started_ = true; //斷言失敗則設(shè)置線程開始運(yùn)行的標(biāo)志 // FIXME: move(func_) detail::ThreadData* data = new detail::ThreadData(func_, name_, tid_); //獲得線程運(yùn)行的所需要的參數(shù) if (pthread_create(&pthreadId_, NULL, &detail::startThread, data)) { //開始線程 started_ = false; printf("blockDim.x: %d/n",blockDim.x); delete data; // or no delete? LOG_SYSFATAL << "Failed in pthread_create"; }}int Thread::join(){ assert(started_); //斷言線程是否正在運(yùn)行 assert(!joined_); //斷言線程是否已經(jīng)被終止 joined_ = true; return pthread_join(pthreadId_, NULL); //等待線程結(jié)束}在線程的析構(gòu)函數(shù)中只設(shè)置線程的分離屬性,即等待線程運(yùn)行結(jié)束后自動(dòng)回收線程資源,不強(qiáng)行終止線程。struct ThreadData //thread的再封裝{ typedef muduo::Thread::ThreadFunc ThreadFunc; ThreadFunc func_; string name_; boost::weak_ptr<pid_t> wkTid_; ThreadData(const ThreadFunc& func, const string& name, const boost::shared_ptr<pid_t>& tid) : func_(func), name_(name), wkTid_(tid) { } void runInThread()//真正讓線程跑起來的函數(shù) { pid_t tid = muduo::CurrentThread::tid(); boost::shared_ptr<pid_t> ptid = wkTid_.lock(); if (ptid) { *ptid = tid; ptid.reset(); } muduo::CurrentThread::t_threadName = name_.empty() ? "muduoThread" : name_.c_str(); ::prctl(PR_SET_NAME, muduo::CurrentThread::t_threadName); try //異常捕捉部分 { func_(); //線程起跑 muduo::CurrentThread::t_threadName = "finished"; } catch (const Exception& ex) { muduo::CurrentThread::t_threadName = "crashed"; fprintf(stderr, "exception caught in Thread %s/n", name_.c_str()); fprintf(stderr, "reason: %s/n", ex.what()); fprintf(stderr, "stack trace: %s/n", ex.stackTrace()); abort(); } catch (const std::exception& ex) { muduo::CurrentThread::t_threadName = "crashed"; fprintf(stderr, "exception caught in Thread %s/n", name_.c_str()); fprintf(stderr, "reason: %s/n", ex.what()); abort(); } catch (...) { muduo::CurrentThread::t_threadName = "crashed"; fprintf(stderr, "unknown exception caught in Thread %s/n", name_.c_str()); throw; // rethrow } }};void* startThread(void* obj) //由pthread_create調(diào)用的函數(shù){ ThreadData* data = static_cast<ThreadData*>(obj); data->runInThread(); delete data; return NULL;}

將線程中的若干數(shù)據(jù)保存到ThreadData中,然后將ThreadData作為傳遞給pthread_create(...,void* arg)中的最后一個(gè)數(shù)據(jù)參數(shù)傳遞給void Thread(void* )標(biāo)準(zhǔn)的線程啟動(dòng)函數(shù)。然后在標(biāo)準(zhǔn)的線程啟動(dòng)函數(shù)內(nèi)將void* arg強(qiáng)行轉(zhuǎn)化為ThreadData,然后使用ThreadData中的runInThread函數(shù)啟動(dòng)線程。

在使用muduo的線程接口時(shí),使用bind將線程運(yùn)行函數(shù)再打包,然后傳遞進(jìn)Thread.


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 富川| 寿阳县| 邢台市| 城市| 临漳县| 云霄县| 天祝| 西丰县| 烟台市| 西平县| 唐山市| 会同县| 宣汉县| 钟祥市| 睢宁县| 汉沽区| 眉山市| 普陀区| 洛隆县| 东明县| 志丹县| 望城县| 巴青县| 油尖旺区| 中卫市| 高雄县| 华坪县| 张家口市| 河源市| 中方县| 广德县| 永泰县| 商南县| 泗阳县| 兴安县| 和静县| 时尚| 正定县| 彰化县| 云龙县| 昂仁县|