Boost::Thread使用示例
来源:互联网 发布:酒店网络销售代理协议 编辑:程序博客网 时间:2024/06/06 17:46
Boost::Thread的实现总体上是比较简单的,前面已经说过,thread只是一个跨平台的线程封装库,其中按照所使用的线程选项的不同,分别决定使用Windows线程API,pThread,或Mac平台的thread实现。以下只讨论Windows,即使用BOOST_HAS_WINTHREAD的情况。
Boost::Thread有两个构造函数:一个是thread(),构造一个表示当前执行线程的线程对象;一个是explicit thread(const boost::function0<void>& threadfunc),这里的boost::function0<void>可以简单看为一个无返回无参数的函数。这里的函数可以是类重载operator()构成的函数;该构造函数传入的是函数对象而并非是函数指针,这样一个具有一般函数特性的类也能作为参数传入,可以看下面的几个例子。
(1)最简单方法
- #include <boost/thread/thread.hpp>
- #include <iostream>
- void hello()
- {
- std::cout<<"Hello world, I'm a thread!"<<std::endl;
- }
- int main()
- {
- boost::thread thrd(&hello);
- thrd.join();
- system("pause");
- return 0;
- }
(2)复杂类型对象作为参数来创建线程
- #include <boost/thread/thread.hpp>
- #include <boost/thread/mutex.hpp>
- #include <iostream>
- boost::mutex io_mutex;
- struct count
- {
- count(int id) : id(id) {}
- void operator()()
- {
- for(int i = 0; i < 10; ++i)
- {
- boost::mutex::scoped_lock lock(io_mutex);
- std::cout<<id<<": "<<i<<std::endl;
- }
- }
- int id;
- };
- int main()
- {
- boost::thread thrd1(count(1));
- boost::thread thrd2(count(2));
- thrd1.join();
- thrd2.join();
- system("pause");
- return 0;
- }
(3)在类内部创建线程
类内部静态方法启动线程
- #include <boost/thread/thread.hpp>
- #include <iostream>
- class HelloWorld
- {
- public:
- static void hello()
- {
- std::cout<<"Hello world, I'm a thread!"<<std::endl;
- }
- static void start()
- {
- boost::thread thrd(hello);
- thrd.join();
- }
- };
- int main()
- {
- HelloWorld::start();
- system("pause");
- return 0;
- }
- #include <boost/function/function0.hpp>
- #include <boost/thread/thread.hpp>
- #include <iostream>
- class HelloWorld
- {
- public:
- void hello()
- {
- std::cout<<"Hello world, I'm a thread!"<<std::endl;
- }
- void start()
- {
- boost::function0<void> f = boost::bind(&HelloWorld::hello, this);
- boost::thread thrd(f);
- thrd.join();
- }
- };
- int main()
- {
- HelloWorld hello;
- hello.start();
- system("pause");
- return 0;
- }
(3)在Singleton模式内部创建线程:
- #include <boost/thread/thread.hpp>
- #include <iostream>
- class HelloWorld
- {
- public:
- void hello()
- {
- std::cout<<"Hello world, I'm a thread!"<<std::endl;
- }
- static void start()
- {
- boost::thread thrd(boost::bind(&HelloWorld::hello, &HelloWorld::getInstance()));
- thrd.join();
- }
- static HelloWorld& getInstance()
- {
- if(!instance)
- instance = new HelloWorld;
- return *instance;
- }
- private:
- HelloWorld() {}
- static HelloWorld* instance;
- };
- HelloWorld* HelloWorld::instance = 0;
- int main()
- {
- HelloWorld::start();
- system("pause");
- return 0;
- }
- #include <boost/thread/thread.hpp>
- #include <iostream>
- #include <string>
- class HelloWorld
- {
- public:
- void hello(const std::string& str)
- {
- std::cout<<str<<std::endl;
- }
- };
- int main()
- {
- HelloWorld obj;
- boost::thread thrd(boost::bind(&HelloWorld::hello, &obj, "Hello World, I'm a thread!"));
- thrd.join();
- system("pause");
- return 0;
- }
- 如果需要绑定的函数有参数则需要使用boost::bind。比如想使用boost::thread创建一个线程来执行函数void f(int i),如果这样写boost::thread thrd(f)是不对的,因为thread构造函数声明接受的是一个没有参数且返回类型为void的函数,而且不提供参数f也无法运行,这时就可以写boost::thread thrd(boost::bind(f, 1))。涉及到有参函数的绑定问题基本上都是boost::thread、boost::function、boost::bind结合起来使用。
0 0
- Boost::Thread使用示例
- Boost::Thread使用示例
- Boost::Thread使用示例
- Boost::Thread使用示例
- Boost::Thread使用示例
- Boost::Thread使用示例
- boost thread使用
- Boost使用Thread库
- boost:thread使用实例
- Boost Thread使用
- boost.property_tree使用示例
- <Boost> boost::thread 多线程的使用
- Boost::thread库的使用
- 使用boost的thread库
- Boost::thread库的使用
- Boost::thread库的使用
- boost thread 的初次使用
- Boost::thread库的使用
- MySQL命令行登陆,远程登陆MySQL
- 安卓:调用真正系统默认短信应用发短信. (含解决4.4系统环聊问题, 含调低版本apk调用高版本系统api)
- C/S架构和B/S架构的概念和区别
- MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
- 做个可以赚钱的宅男 - PTC网赚推荐
- Boost::Thread使用示例
- write函数
- C++之父评论C++与Java
- java中输入、输出流总结
- ASP.NET 图片下载
- 真理与文字
- objc delegate
- Redhat 出现丢包解决方法(ping: sendmsg: Operation not permitted)
- 为什么自媒体作者们突然开始讨论钱了?