c++的thread类(c++线程简单用法)
生活随笔
收集整理的這篇文章主要介紹了
c++的thread类(c++线程简单用法)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近看了一個(gè)Thread類(忘記在哪里看的了),感覺不錯(cuò)。
創(chuàng)建線程時(shí)線程對(duì)應(yīng)的函數(shù)必須是類的靜態(tài)成員,由于靜態(tài)成員無法訪問類的非靜態(tài)成員,我從前都是把對(duì)象的指針作為參數(shù)傳遞給線程函數(shù)來避免這個(gè)問題,但是在邏輯上線程函數(shù)還需要訪問對(duì)象的私有成員,總是感覺代碼很不優(yōu)雅。這個(gè)Thread類同java中的Thread類在用法上比較類似,使用一種比較優(yōu)雅的方法避免了上面的問題。使用時(shí)只要從Thread派生一個(gè)子類并實(shí)現(xiàn)線程運(yùn)行的函數(shù)?void?run() 就可以了。還是看代碼吧:
先把使用方法貼上來:
C++語言:?Codee#880 01?#include<iostream>
02?
03?#include "Thread.h"
04?
05?class?MyThreadClass:?public?Thread
06?{
07? ? ?private:
08? ? ? ? ?int?a;
09? ? ?public:
10? ? ? ? ?MyThreadClass(?) ? ? ? ? ? ? ?{
11? ? ? ? ? ? ?a?=?0;
12? ? ? ? ?}
13? ? ? ? ?~MyThreadClass(){}
15? ? ? ? ?virtual?void?run();
16?};
17?
18?void?Receiver::run() ? ? {
19?????a++;
20?????std::cout<<a<<std::endl;
21?}
22?
23?int?main(int?argc,?char?*?argv[])
24?{
25?????MyThreadClass ??myThread;
26?????myThread.start();//創(chuàng)建了一個(gè)線程,運(yùn)行函數(shù)run()
27?????myThread.join();//等待線程結(jié)束
28?????return?0;
29?}
下面是Thread類的實(shí)現(xiàn),為了閱讀清晰,刪減了很多內(nèi)容
C++語言: Thread.h 01?#ifndef COMMUNITCATE_H
02?#define COMMUNITCATE_H
03?
04?
05?
06?#include "pthread.h"
07?
08?
09?class?Thread
10?{
11?protected:
12?????pthread_t?_tid;
13?????static?void*?run0(void*?opt);
14?????void*?run1();//如果類中有保存線程狀態(tài)的變量,可以在這個(gè)函數(shù)中可以進(jìn)行更改操作
15?public:
16?????Thread();
17?????~Thread();
18?????/**?
19????? * 創(chuàng)建線程,線程函數(shù)是 run0?
20????? *?
21????? * @return 成功返回 ture 否則返回 false
22????? */
23?????bool?start();
24?????/**?
25????? * join this thread
26????? *?
27????? */
28?????void?join();
29?????virtual?void?run(){
30?????????
31?????}
32?};
33?
34?
35?#endif
C++語言: Thread.cpp 01?#include "Thread.h"
02?
03?Thread::Thread(){
04?????
05?}
06?
07?Thread::~Thread(){
08?????
09?}
10?
11?
12?
13?void*?Thread::run0(void*?opt)
14?{
15?????Thread*?p?=?(Thread*)?opt;
16???? p->run1();
17?????return?p;
18?}
19?
20?void*?Thread::run1()
21?{
22?????_tid?=?pthread_self();
23?????run();
24?????_tid?=?0;
25?????pthread_exit(NULL);
26?}
27?
28?bool?Thread::start()
29?{
30?????return?pthread_create(&_tid,?NULL,?run0,?this)?==?0;
31?}
32?
33?void?Thread::join()
34?{
35?????if(?_tid?>?0?){
36?????????pthread_join(_tid,?NULL);
37?????}
38?}
創(chuàng)建線程時(shí)線程對(duì)應(yīng)的函數(shù)必須是類的靜態(tài)成員,由于靜態(tài)成員無法訪問類的非靜態(tài)成員,我從前都是把對(duì)象的指針作為參數(shù)傳遞給線程函數(shù)來避免這個(gè)問題,但是在邏輯上線程函數(shù)還需要訪問對(duì)象的私有成員,總是感覺代碼很不優(yōu)雅。這個(gè)Thread類同java中的Thread類在用法上比較類似,使用一種比較優(yōu)雅的方法避免了上面的問題。使用時(shí)只要從Thread派生一個(gè)子類并實(shí)現(xiàn)線程運(yùn)行的函數(shù)?void?run() 就可以了。還是看代碼吧:
先把使用方法貼上來:
C++語言:?Codee#880 01?#include<iostream>
02?
03?#include "Thread.h"
04?
05?class?MyThreadClass:?public?Thread
06?{
07? ? ?private:
08? ? ? ? ?int?a;
09? ? ?public:
10? ? ? ? ?MyThreadClass(?) ? ? ? ? ? ? ?{
11? ? ? ? ? ? ?a?=?0;
12? ? ? ? ?}
13? ? ? ? ?~MyThreadClass(){}
15? ? ? ? ?virtual?void?run();
16?};
17?
18?void?Receiver::run() ? ? {
19?????a++;
20?????std::cout<<a<<std::endl;
21?}
22?
23?int?main(int?argc,?char?*?argv[])
24?{
25?????MyThreadClass ??myThread;
26?????myThread.start();//創(chuàng)建了一個(gè)線程,運(yùn)行函數(shù)run()
27?????myThread.join();//等待線程結(jié)束
28?????return?0;
29?}
下面是Thread類的實(shí)現(xiàn),為了閱讀清晰,刪減了很多內(nèi)容
C++語言: Thread.h 01?#ifndef COMMUNITCATE_H
02?#define COMMUNITCATE_H
03?
04?
05?
06?#include "pthread.h"
07?
08?
09?class?Thread
10?{
11?protected:
12?????pthread_t?_tid;
13?????static?void*?run0(void*?opt);
14?????void*?run1();//如果類中有保存線程狀態(tài)的變量,可以在這個(gè)函數(shù)中可以進(jìn)行更改操作
15?public:
16?????Thread();
17?????~Thread();
18?????/**?
19????? * 創(chuàng)建線程,線程函數(shù)是 run0?
20????? *?
21????? * @return 成功返回 ture 否則返回 false
22????? */
23?????bool?start();
24?????/**?
25????? * join this thread
26????? *?
27????? */
28?????void?join();
29?????virtual?void?run(){
30?????????
31?????}
32?};
33?
34?
35?#endif
C++語言: Thread.cpp 01?#include "Thread.h"
02?
03?Thread::Thread(){
04?????
05?}
06?
07?Thread::~Thread(){
08?????
09?}
10?
11?
12?
13?void*?Thread::run0(void*?opt)
14?{
15?????Thread*?p?=?(Thread*)?opt;
16???? p->run1();
17?????return?p;
18?}
19?
20?void*?Thread::run1()
21?{
22?????_tid?=?pthread_self();
23?????run();
24?????_tid?=?0;
25?????pthread_exit(NULL);
26?}
27?
28?bool?Thread::start()
29?{
30?????return?pthread_create(&_tid,?NULL,?run0,?this)?==?0;
31?}
32?
33?void?Thread::join()
34?{
35?????if(?_tid?>?0?){
36?????????pthread_join(_tid,?NULL);
37?????}
38?}
總結(jié)
以上是生活随笔為你收集整理的c++的thread类(c++线程简单用法)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MongoDB可视化工具Robomong
- 下一篇: s3c2440移植MQTT