青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

#ant

The dreams in which I'm dying are the best I've ever had...

從Win32 API封裝Thread類[2]

在上一篇中介紹了創(chuàng)建Thread的兩種方法:從Thread類繼承或者實現(xiàn)Runnable接口。有時候這并不是特別方便,我們需要的是更靈活的方法,比如像boost庫中的Thread一樣可以用普通函數(shù)和函數(shù)對象
(functor?and function object)作為構(gòu)造函數(shù)參數(shù)。如果你熟悉STL,你應(yīng)該熟悉bind1st和bind2nd這兩個函數(shù)配接器(function adapter),bind1st和bind2nd可以將一個二元函數(shù)(binary function)轉(zhuǎn)換成一元函數(shù)(unary function)。為了使Thread類能夠用普通函數(shù)和函數(shù)對象作為參數(shù),我們需要一個bind將一元函數(shù)轉(zhuǎn)換成無參函數(shù):
bind.h
?1?#ifndef?BIND_H
?2?
#define?BIND_H
?3?

?4?template?<class?_Result>
?5?struct?trivial_function?{
?6?
????typedef?_Result?result_type;
?7?
};
?8?

?9?template?<class?_Operation>
10?class?binder?:?public?trivial_function<typename?_Operation::result_type>?{
11?public
:
12?????binder(const?_Operation& x,?const?typename?_Operation::argument_type&
y)
13?
????????:op(x),?value(y)?{}
14?????typename?_Operation::result_type?operator()()?const
?{
15?????????return
?op(value);
16?
????}
17?protected
:
18?
????_Operation?op;
19?
????typename?_Operation::argument_type?value;
20?
};
21?

22?template?<class?_Operation,?class?_Tp>
23?inline?binder<_Operation>?
24?bind(const?_Operation&?__fn,?const?_Tp&
?__x)?
25?
{
26?
????typedef?typename?_Operation::argument_type?_Arg_type;
27?????return?binder<_Operation>
(__fn,?_Arg_type(__x));
28?
}
29?

30?#endif/*BIND_H*/

有了bind我們還需要修改Thread類的構(gòu)造函數(shù),顯然我們必須將構(gòu)造函數(shù)聲明為成員模板(還有一種方法也可以達(dá)到同樣的目的,就是把Thread類聲明為模板,但是這樣的設(shè)計好像不太好),這樣才能夠讓Thread類的構(gòu)造函數(shù)可以接受各種類型的參數(shù),修改后的構(gòu)造函數(shù)應(yīng)該能夠使用如下三種類型的參數(shù):
1.Runnable *
2.no argument function
3.no argument functor
下面是修改后的頭文件:
runnable.h
?1?#ifndef?RUNNABLE_H
?2?
#define?RUNNABLE_H
?3?

?4?struct?Runnable?{
?5?????virtual?void?run()?=?0
;
?6?????virtual?~
Runnable()?{}
?7?
};
?8?

?9?template?<class?T>
10?class?RunnableFunctor?:?public?Runnable?{
11?public
:
12?????RunnableFunctor(const?T&
f)?:_func(f)?{}
13?????virtual?void
?run()?{?_func();?}
14?private
:
15?
????T?_func;
16?
};
17?

18?//base?template?for?no?argument?functor
19?template?<class?T>
20?struct?FuncImpl?{
21?????static?Runnable*?transfer(const?T&
t)?{
22?????????return?new?RunnableFunctor<T>
(t);
23?
????}
24?
};
25?

26?//partial?specialization?for?T*
27?template?<class?T>
28?struct?FuncImpl<T*>?{
29?????static?Runnable*?transfer(T*
t)?{
30?????????return
?t;
31?
????}
32?
};
33?

34?//partial?specialization?for?no?argument?function
35?template?<class?T>
36?struct?FuncImpl<T?(*)()>?{
37?????static?Runnable*?transfer(T?(*
t)())?{
38?????????return?new?RunnableFunctor<T?(*)()>
(t);
39?
????}
40?
};
41?

42?template?<class?T>
43?inline?Runnable*?transfer(const?T& t)?{
44?????return?FuncImpl<T>
::transfer(t);
45?
}
46?

47?#endif/*RUNNABLE_H*/

thread.h
?1?#ifndef?THREAD_H
?2?
#define?THREAD_H
?3?

?4?#include?<windows.h>
?5?#include?"bind.h"
?6?#include?"runnable.h"
?7?
?8?#define?CLASS_UNCOPYABLE(classname)?\
?9?????private
:?\
10?????classname(const?classname&
);?\
11?????classname&?operator=(const?classname&
);
12?

13?class?Thread?:?public?Runnable?{
14?
????CLASS_UNCOPYABLE(Thread)
15?public
:
16?
????Thread()
17?????????:_target(0
)
18?????????,_handle(0
)?{
19?

20?????}
21?????template?<class?T>

22?????explicit?Thread(const?T& op)
23?
????????:_target(transfer(op))
24?????????,_handle(0
)?{
25?

26?????}
27?????virtual?~
Thread();
28?????virtual?void
?run()?{}
29?????void
?start();
30?????void
?join();
31?private
:
32?????static?unsigned?__stdcall?threadProc(void*
param);
33?private
:
34?????Runnable*
_target;
35?
????HANDLE?_handle;
36?
};
37?

38?#endif/*THREAD_H*/

thread.cpp和前一篇的幾乎一樣,唯一的不同是去掉了構(gòu)造函數(shù)Thread(Runnable *),因為現(xiàn)在的構(gòu)造函數(shù)改成了成員模板,實現(xiàn)也放在thread.h中了。現(xiàn)在的構(gòu)造函數(shù)能夠接受各種類型的參數(shù),主要歸功于模板函數(shù)transfer,實現(xiàn)代碼在runnable.h中,主要技巧是用類的偏特化模擬函數(shù)模板的偏特化,不明白的請看為什么不要特化函數(shù)模版

下面是測試代碼:
test.cpp
?1?#include?"thread.h"
?2?#include?<iostream>
?3?#include?<functional>
?4?
?5?using?namespace?std;
?6?

?7?//no?argument?function
?8?void?print()?{
?9?????cout?<<?"print"?<<
?endl;
10?
}
11?

12?//unary?function
13?void?print1(int?n)?{
14?????cout?<<?"print1"?<<
?endl;
15?
}
16?

17?//binary?function
18?void?print2(int?m,?int?n)?{
19?????cout?<<?"print2"?<<
?endl;
20?
}
21?

22?
23?//no?argument?functor
24?struct?PrintFunctor?{
25?????void?operator()()?const
?{
26?????????cout?<<?"PrintFunctor"?<<
?endl;
27?
????}
28?
};
29?

30?//unary?functor
31?struct?PrintFunctor1?:?public?unary_function<int,?void>?{
32?????void?operator()(int?n)?const
?{
33?????????cout?<<?"PrintFunctor1"?<<
?endl;
34?
????}
35?
};
36?

37?//binary?functor
38?struct?PrintFunctor2?:?public?binary_function<int,?int,?void>?{
39?????void?operator()(int?m,?int?n)?const
?{
40?????????cout?<<?"PrintFunctor2"?<<
?endl;
41?
????}
42?
};
43?

44?int?main()?{
45?

46?????//construct?Thread?with?no?argument?function
47?????Thread?thread1(&print);
48?
????thread1.start();
49?

50?????//construct?Thread?with?unary?function
51?????Thread?thread2(bind(ptr_fun(print1),?5));
52?
????thread2.start();
53?

54?????//construct?Thread?with?binary?function
55?????Thread?thread3(bind(bind1st(ptr_fun(print2),?1),?2));
56?
????thread3.start();
57?

58?
59?????//construct?Thread?with?no?argument?functor
60?????Thread?thread4((PrintFunctor()));
61?
????thread4.start();
62?

63?????//construct?Thread?with?unary?functor
64?????Thread?thread5(bind(PrintFunctor1(),?5));
65?
????thread5.start();
66?

67?????//construct?Thread?with?binary?functor
68?????Thread?thread6(bind(bind1st(PrintFunctor2(),?1),?2));
69?
????thread6.start();
70?

71?????thread1.join();
72?
????thread2.join();
73?
????thread3.join();
74?
????thread4.join();
75?
????thread5.join();
76?
????thread6.join();
77?

78?????return?0;
79?}

當(dāng)然了,上面的并不是全部,修改后的Thread類不僅能夠使用原先的從Thread類繼承或者實現(xiàn)Runnable接口的方法,還可以使用任何無參函數(shù)或無參函數(shù)對象。除了test.cpp里示范的,你甚至可以用bind,bind1st,bind2st,mem_fun,mem_fun_ref的組合來用某個類的成員函數(shù)作為參數(shù),具有超強(qiáng)的靈活性。

目前實現(xiàn)的這些都是Thread類最基本的功能,其他功能如設(shè)置線程優(yōu)先級,掛起或恢復(fù)線程,異常處理等具體實現(xiàn)都比較簡單,這這里就不一一實現(xiàn)了。
源代碼下載:點擊下載

posted on 2007-08-31 11:58 螞蟻終結(jié)者 閱讀(3313) 評論(7)  編輯 收藏 引用 所屬分類: C++

Feedback

# re: 從Win32 API封裝Thread類[2] 2007-08-31 12:26 haskell

不錯,支持
介紹一個完美的c++多線程庫,intel的tbb。
http://osstbb.intel.com/index.php  回復(fù)  更多評論   

# re: 從Win32 API封裝Thread類[2] 2007-08-31 15:14 重劍

我download了源碼,看了下,有 new 沒 delete, why?  回復(fù)  更多評論   

# re: 從Win32 API封裝Thread類[2] 2007-08-31 15:26 螞蟻終結(jié)者

@重劍
oops!!!
delete不小心忘記寫了。
把析構(gòu)函數(shù)改成這樣就行了:
Thread::~Thread() {
if (_handle != 0)
CloseHandle(_handle);
if (_target != 0)
delete _target;
}

已經(jīng)更新了下載鏈接,也可以重新下載。
thanks!  回復(fù)  更多評論   

# re: 從Win32 API封裝Thread類[2] 2007-08-31 21:03 C/C++面試題

支持  回復(fù)  更多評論   

# re: 從Win32 API封裝Thread類[2] 2007-09-03 12:37 shaker(太子)

如果能使用boost::bind 代碼就漂亮多了  回復(fù)  更多評論   

# re: 從Win32 API封裝Thread類[2] 2007-09-03 21:10 螞蟻終結(jié)者

@shaker(太子)
boost::bind確實優(yōu)雅
上面的bind修改一下應(yīng)該也可以實現(xiàn)差不多的功能  回復(fù)  更多評論   

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            9l国产精品久久久久麻豆| 久久精品免视看| 亚洲欧美视频在线| 亚洲图色在线| 午夜精品福利在线观看| 欧美在线视频a| 久久久福利视频| 免费观看在线综合| 亚洲黄一区二区三区| 亚洲国产婷婷| 日韩午夜精品视频| 亚洲免费视频观看| 久久在线播放| 欧美日精品一区视频| 国产欧美日本在线| 亚洲黄色性网站| 亚洲一区二区三区777| 久久精品国产成人| 91久久夜色精品国产九色| 亚洲一区国产一区| 欧美暴力喷水在线| 国产欧美一区二区精品仙草咪 | 樱花yy私人影院亚洲| 尤物yw午夜国产精品视频| 99精品99| 久久一区亚洲| 夜夜夜精品看看| 六月婷婷久久| 国产乱人伦精品一区二区| 亚洲国产欧美国产综合一区 | 国产伦精品一区| 亚洲高清成人| 久久福利毛片| aa成人免费视频| 久久综合九色综合久99| 国产精品白丝jk黑袜喷水| 久久久女女女女999久久| 国模私拍一区二区三区| 亚洲日本中文字幕| 欧美中文在线观看国产| 亚洲日本va在线观看| 久久精品国产99| 国产精品久久久久影院亚瑟| 亚洲黄色在线视频| 久久偷看各类wc女厕嘘嘘偷窃| 欧美成人精品一区二区| 亚洲午夜一区二区三区| 欧美—级高清免费播放| 伊人成年综合电影网| 性欧美xxxx视频在线观看| 亚洲欧洲日韩女同| 蜜乳av另类精品一区二区| 国产欧美精品久久| 亚洲一区二区在线观看视频| 欧美大片在线看| 久久久人成影片一区二区三区| 欧美性片在线观看| 一本综合精品| 亚洲人成7777| 欧美激情亚洲激情| 亚洲国产视频直播| 欧美成人免费一级人片100| 欧美一区久久| 在线日韩欧美| 亚洲成色777777在线观看影院| 亚洲欧美日韩精品| 国产欧美韩国高清| 久久久久九九九| 久久精品国产在热久久 | 亚洲伊人一本大道中文字幕| 亚洲精品久久久久久久久| 欧美激情视频在线播放| 亚洲美女区一区| 99综合电影在线视频| 欧美亚州一区二区三区| 亚洲欧美在线播放| 欧美一级电影久久| 亚洲第一黄色网| 亚洲日本免费| 国产精品入口日韩视频大尺度| 亚洲一区日韩在线| 午夜日韩电影| 亚洲国产精品一区二区尤物区| 欧美大片一区二区三区| 欧美日韩精品免费看| 欧美一区91| 另类天堂av| 亚洲在线视频一区| 久久99在线观看| 99精品视频一区二区三区| 亚洲欧美不卡| 亚洲精品国久久99热| 亚洲视频在线视频| 亚洲大片在线| 亚洲一区二区精品| 日韩亚洲精品在线| 国产精品成人一区二区艾草| 亚洲女人小视频在线观看| 亚洲欧美一区二区三区极速播放| 欧美三区美女| 久久色中文字幕| 欧美精品一区三区| 久久精品亚洲| 欧美日韩日日骚| 欧美aⅴ一区二区三区视频| 欧美日韩一区二区视频在线| 久久久中精品2020中文| 国产精品va在线播放| 免费成人高清在线视频| 国产精品久久久久久久久久免费| 香蕉乱码成人久久天堂爱免费 | 亚洲黄色免费| 亚洲图片在线| 日韩视频中文| 久久精品视频播放| 亚洲欧美日韩在线综合| 免费中文字幕日韩欧美| 久久精品99无色码中文字幕 | 欧美护士18xxxxhd| 国产欧美一区二区色老头| 亚洲第一色中文字幕| 国产欧美日韩综合精品二区| 91久久香蕉国产日韩欧美9色| 国产农村妇女精品一二区| 亚洲精品一区二区在线| 亚洲三级色网| 裸体女人亚洲精品一区| 久久精品视频在线观看| 国产乱子伦一区二区三区国色天香 | 亚洲欧美中文另类| 伊伊综合在线| 久久久精品日韩欧美| 久久精品一区中文字幕| 国产精品日韩欧美| 一本色道久久综合| 一本久久a久久精品亚洲| 欧美成人免费网| 亚洲电影第1页| 亚洲精品久久久久| 久热精品在线视频| 快播亚洲色图| 影音先锋久久资源网| 久久精品99| 男女精品网站| 亚洲精品日产精品乱码不卡| 米奇777超碰欧美日韩亚洲| 欧美成人免费网站| 亚洲人成久久| 欧美日韩免费| 亚洲一区二区视频在线| 欧美亚洲免费高清在线观看| 国产九九精品视频| 国产人久久人人人人爽| 欧美在线高清| 韩国一区二区三区在线观看 | 国内精品久久久| 久久精品毛片| 欧美高清视频一区二区| 亚洲精品一二三区| 欧美三级在线视频| 午夜精品视频在线| 巨乳诱惑日韩免费av| 亚洲人成人一区二区在线观看| 欧美www视频| 洋洋av久久久久久久一区| 先锋影院在线亚洲| 伊人久久亚洲热| 欧美日韩免费看| 欧美一区亚洲一区| 亚洲激情在线观看| 午夜久久99| 最新中文字幕一区二区三区| 欧美日韩精品欧美日韩精品| 午夜久久电影网| 亚洲激情网站免费观看| 欧美怡红院视频| 亚洲国产91| 国产麻豆精品视频| 欧美激情影院| 欧美一区二区三区免费视频| 亚洲国产精品第一区二区| 欧美一区=区| 99视频一区二区| 伊人久久大香线| 国产精品久久影院| 欧美黄色大片网站| 欧美在线日韩精品| 亚洲视频第一页| 欧美成人一区二区三区片免费| 99ri日韩精品视频| 一区在线视频| 国产精品日本精品| 欧美成人蜜桃| 久久久久国产一区二区| 99精品欧美一区二区蜜桃免费| 欧美淫片网站| 亚洲在线观看视频| 99国产精品| 91久久精品日日躁夜夜躁欧美| 国产精品网站一区|