thread自然是boost::thread庫的主
角,但thread類的實現總體上是比較簡單的,前面已經說過,thread只是一個跨平臺的線程封裝庫,其中按照所使用的編譯選項的不同,分別決定使用
Windows線程API還是pthread,或者Macintosh Carbon平臺的thread實現。以下只討論Windows,即使用
BOOST_HAS_WINTHREADS的情況。
thread類提供了兩種構造函數:
thread::thread()
thread::thread(const function0<void>& threadfunc)
第 一種構造函數用于調用GetCurrentThread構造一個當前線程的thread對象,第二種則通過傳入一個函數或者一個functor來創建一個 新的線程。第二種情況下,thread類在其構造函數中間接調用CreateThread來創建線程,并將線程句柄保存到成員變量m_thread中,并 執行傳入的函數,或執行functor的operator ()方法來啟動工作線程。
我們可以用以下三種方式啟動一個新線程:
1、傳遞一個工作函數來構造一個工作線程
2、傳遞一個functor對象來構造一個工作線程
3、無需將類設計成一個functor,借助bind來構造functor對象以創建工作線程
其中bind是一個函數模板,它可以根據后面的實例化參數構造出一個functor來,上面的boost::bind(&count::do_count, &c1, 10)其實等價于返回了一個functor:
struct countFunctor
{
int operator() ()
{
(&c1)->do_count(10); // just a hint, not actual code
}
};
因此,以后就跟2中是一樣的了。
thread類提供了兩種構造函數:
thread::thread()
thread::thread(const function0<void>& threadfunc)
第 一種構造函數用于調用GetCurrentThread構造一個當前線程的thread對象,第二種則通過傳入一個函數或者一個functor來創建一個 新的線程。第二種情況下,thread類在其構造函數中間接調用CreateThread來創建線程,并將線程句柄保存到成員變量m_thread中,并 執行傳入的函數,或執行functor的operator ()方法來啟動工作線程。
我們可以用以下三種方式啟動一個新線程:
1、傳遞一個工作函數來構造一個工作線程
1 #include <boost/thread/thread.hpp>
2 #include <boost/thread/mutex.hpp>
3 #include <iostream>
4
5 boost::mutex io_mutex;
6
7 void count() // worker function
8 {
9 for (int i = 0; i < 10; ++i)
10 {
11 boost::mutex::scoped_lock lock(io_mutex);
12 std::cout << i << std::endl;
13 }
14 }
15
16 int main(int argc, char* argv[])
17 {
18 boost::thread thrd1(&count);
19 boost::thread thrd2(&count);
20 thrd1.join();
21 thrd2.join();
22
23 return 0;
24 }
25
2 #include <boost/thread/mutex.hpp>
3 #include <iostream>
4
5 boost::mutex io_mutex;
6
7 void count() // worker function
8 {
9 for (int i = 0; i < 10; ++i)
10 {
11 boost::mutex::scoped_lock lock(io_mutex);
12 std::cout << i << std::endl;
13 }
14 }
15
16 int main(int argc, char* argv[])
17 {
18 boost::thread thrd1(&count);
19 boost::thread thrd2(&count);
20 thrd1.join();
21 thrd2.join();
22
23 return 0;
24 }
25
2、傳遞一個functor對象來構造一個工作線程
1 #include <boost/thread/thread.hpp>
2 #include <boost/thread/mutex.hpp>
3 #include <iostream>
4
5 boost::mutex io_mutex;
6
7 struct count
8 {
9 count(int id) : id(id) { }
10
11 void operator()()
12 {
13 for (int i = 0; i < 10; ++i)
14 {
15 boost::mutex::scoped_lock lock(io_mutex); // lock io, will be explained soon.
16 std::cout << id << ": " << i << std::endl;
17 }
18 }
19
20 int id;
21 };
22
23 int main(int argc, char* argv[])
24 {
25 boost::thread thrd1(count(1));
26 boost::thread thrd2(count(2));
27 thrd1.join();
28 thrd2.join();
29 return 0;
30 }
31
2 #include <boost/thread/mutex.hpp>
3 #include <iostream>
4
5 boost::mutex io_mutex;
6
7 struct count
8 {
9 count(int id) : id(id) { }
10
11 void operator()()
12 {
13 for (int i = 0; i < 10; ++i)
14 {
15 boost::mutex::scoped_lock lock(io_mutex); // lock io, will be explained soon.
16 std::cout << id << ": " << i << std::endl;
17 }
18 }
19
20 int id;
21 };
22
23 int main(int argc, char* argv[])
24 {
25 boost::thread thrd1(count(1));
26 boost::thread thrd2(count(2));
27 thrd1.join();
28 thrd2.join();
29 return 0;
30 }
31
3、無需將類設計成一個functor,借助bind來構造functor對象以創建工作線程
1 #include <boost/thread/thread.hpp>
2 #include <boost/thread/mutex.hpp>
3 #include <boost/bind.hpp>
4 #include <iostream>
5
6 boost::mutex io_mutex;
7
8 struct count
9 {
10 static int num;
11 int id;
12
13 count() : id(num++) {}
14
15 int do_count(int n)
16 {
17 for (int i = 0; i < n; ++i)
18 {
19 boost::mutex::scoped_lock lock(io_mutex);
20 std::cout << id << ": " << i << std::endl;
21 }
22 return id;
23 }
24 };
25
26 int count::num = 1;
27
28 int main(int argc, char* argv[])
29 {
30 count c1;
31 boost::thread thrd1(boost::bind(&count::do_count, &c1, 10));
32 thrd1.join();
33 return 0;
34 }
2 #include <boost/thread/mutex.hpp>
3 #include <boost/bind.hpp>
4 #include <iostream>
5
6 boost::mutex io_mutex;
7
8 struct count
9 {
10 static int num;
11 int id;
12
13 count() : id(num++) {}
14
15 int do_count(int n)
16 {
17 for (int i = 0; i < n; ++i)
18 {
19 boost::mutex::scoped_lock lock(io_mutex);
20 std::cout << id << ": " << i << std::endl;
21 }
22 return id;
23 }
24 };
25
26 int count::num = 1;
27
28 int main(int argc, char* argv[])
29 {
30 count c1;
31 boost::thread thrd1(boost::bind(&count::do_count, &c1, 10));
32 thrd1.join();
33 return 0;
34 }
其中bind是一個函數模板,它可以根據后面的實例化參數構造出一個functor來,上面的boost::bind(&count::do_count, &c1, 10)其實等價于返回了一個functor:
struct countFunctor
{
int operator() ()
{
(&c1)->do_count(10); // just a hint, not actual code
}
};
因此,以后就跟2中是一樣的了。