Boost多线程
一、概述
二、线程管理
- #include <boost/thread.hpp>
- #include <iostream>
- void wait(int seconds)
- {
- boost::this_thread::sleep(boost::posix_time::seconds(seconds));
- }
- void thread()
- {
- for (int i = ; i < ; ++i)
- {
- wait();
- std::cout << i << std::endl;
- }
- }
- int main()
- {
- boost::thread t(thread);
- t.join();
- }
- #include <boost/thread.hpp>
- #include <iostream>
- void wait(int seconds)
- {
- boost::this_thread::sleep(boost::posix_time::seconds(seconds));
- }
- void thread()
- {
- try
- {
- for (int i = ; i < ; ++i)
- {
- wait();
- std::cout << i << std::endl;
- }
- }
- catch (boost::thread_interrupted&)
- {
- }
- }
- int main()
- {
- boost::thread t(thread);
- wait();
- t.interrupt();
- t.join();
- }
- #include <boost/thread.hpp>
- #include <iostream>
- int main()
- {
- std::cout << boost::this_thread::get_id() << std::endl;
- std::cout << boost::thread::hardware_concurrency() << std::endl;
- }
三、同步
- #include <boost/thread.hpp>
- #include <iostream>
- void wait(int seconds)
- {
- boost::this_thread::sleep(boost::posix_time::seconds(seconds));
- }
- boost::mutex mutex;
- void thread()
- {
- for (int i = ; i < ; ++i)
- {
- wait();
- mutex.lock();
- std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
- mutex.unlock();
- }
- }
- int main()
- {
- boost::thread t1(thread);
- boost::thread t2(thread);
- t1.join();
- t2.join();
- }
- #include <boost/thread.hpp>
- #include <iostream>
- void wait(int seconds)
- {
- boost::this_thread::sleep(boost::posix_time::seconds(seconds));
- }
- boost::mutex mutex;
- void thread()
- {
- for (int i = ; i < ; ++i)
- {
- wait();
- boost::lock_guard<boost::mutex> lock(mutex);
- std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
- }
- }
- int main()
- {
- boost::thread t1(thread);
- boost::thread t2(thread);
- t1.join();
- t2.join();
- }
- #include <boost/thread.hpp>
- #include <iostream>
- void wait(int seconds)
- {
- boost::this_thread::sleep(boost::posix_time::seconds(seconds));
- }
- boost::timed_mutex mutex;
- void thread()
- {
- for (int i = ; i < ; ++i)
- {
- wait();
- boost::unique_lock<boost::timed_mutex> lock(mutex, boost::try_to_lock);
- if (!lock.owns_lock())
- lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds());
- std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl;
- boost::timed_mutex *m = lock.release();
- m->unlock();
- }
- }
- int main()
- {
- boost::thread t1(thread);
- boost::thread t2(thread);
- t1.join();
- t2.join();
- }
- #include <boost/thread.hpp>
- #include <iostream>
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- void wait(int seconds)
- {
- boost::this_thread::sleep(boost::posix_time::seconds(seconds));
- }
- boost::shared_mutex mutex;
- std::vector<int> random_numbers;
- void fill()
- {
- std::srand(static_cast<unsigned int>(std::time()));
- for (int i = ; i < ; ++i)
- {
- boost::unique_lock<boost::shared_mutex> lock(mutex);
- random_numbers.push_back(std::rand());
- lock.unlock();
- wait();
- }
- }
- void print()
- {
- for (int i = ; i < ; ++i)
- {
- wait();
- boost::shared_lock<boost::shared_mutex> lock(mutex);
- std::cout << random_numbers.back() << std::endl;
- }
- }
- int sum = ;
- void count()
- {
- for (int i = ; i < ; ++i)
- {
- wait();
- boost::shared_lock<boost::shared_mutex> lock(mutex);
- sum += random_numbers.back();
- }
- }
- int main()
- {
- boost::thread t1(fill);
- boost::thread t2(print);
- boost::thread t3(count);
- t1.join();
- t2.join();
- t3.join();
- std::cout << "Sum: " << sum << std::endl;
- }
- #include <boost/thread.hpp>
- #include <iostream>
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- boost::mutex mutex;
- boost::condition_variable_any cond; //条件变量
- std::vector<int> random_numbers;
- void fill()
- {
- std::srand(static_cast<unsigned int>(std::time()));
- for (int i = ; i < ; ++i)
- {
- boost::unique_lock<boost::mutex> lock(mutex);
- random_numbers.push_back(std::rand()); //尾部加入一位
- cond.notify_all();
- cond.wait(mutex);
- }
- }
- void print()
- {
- std::size_t next_size = ;
- for (int i = ; i < ; ++i)
- {
- boost::unique_lock<boost::mutex> lock(mutex);
- while (random_numbers.size() != next_size)
- cond.wait(mutex);
- std::cout << random_numbers.back() << std::endl;
- ++next_size;
- cond.notify_all();
- }
- }
- int main()
- {
- boost::thread t1(fill);
- boost::thread t2(print);
- t1.join();
- t2.join();
- }
四、线程本地存储
- #include <boost/thread.hpp>
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- void init_number_generator()
- {
- static bool done = false;
- if (!done)
- {
- done = true;
- std::srand(static_cast<unsigned int>(std::time())); //初始化随机数发生器
- }
- }
- boost::mutex mutex;
- void random_number_generator()
- {
- init_number_generator();
- int i = std::rand();
- boost::lock_guard<boost::mutex> lock(mutex);
- std::cout << i << std::endl;
- }
- int main()
- {
- boost::thread t[];
- for (int i = ; i < ; ++i)
- t[i] = boost::thread(random_number_generator);
- for (int i = ; i < ; ++i)
- t[i].join();
- }
- #include <boost/thread.hpp>
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- void init_number_generator()
- {
- static boost::thread_specific_ptr<bool> tls;
- if (!tls.get())
- tls.reset(new bool(false));
- if (!*tls)
- {
- *tls = true;
- std::srand(static_cast<unsigned int>(std::time()));
- }
- }
- boost::mutex mutex;
- void random_number_generator()
- {
- init_number_generator();
- int i = std::rand();
- boost::lock_guard<boost::mutex> lock(mutex);
- std::cout << i << std::endl;
- }
- int main()
- {
- boost::thread t[];
- for (int i = ; i < ; ++i)
- t[i] = boost::thread(random_number_generator);
- for (int i = ; i < ; ++i)
- t[i].join();
- }
Boost多线程的更多相关文章
- Boost多线程编程
Boost多线程编程 背景 • 今天互联网应用服务程序普遍使用多线程来提高与多客户链接时的效率:为了达到最大的吞吐量,事务服务器在单独的线程上运行服务程序: GUI应用程序将那些费时, ...
- 【C/C++学院】0904-boost智能指针/boost多线程锁定/哈希库/正則表達式
boost_array_bind_fun_ref Array.cpp #include<boost/array.hpp> #include <iostream> #includ ...
- Boost多线程-替换MFC线程
Mfc的多线程看起来简单,可以把线程直接压入向量,由系统类似进行调配,其实在内存的处理问题上留下了漏洞.在新线程里面载入大量流,会导致内存泄露. 方便之处:直接使用结构体传入函数参数,供 ...
- boost多线程使用简例
原文链接:http://www.cppblog.com/toMyself/archive/2010/09/22/127347.html C++ Boost Thread 编程指南 转自cnblog: ...
- boost多线程入门介绍
:first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...
- 27.boost多线程
#define _CRT_SECURE_NO_WARNINGS #include <boost/thread.hpp> #include <iostream> #include ...
- boost多线程编译出错
添加 -lpthread CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/tools/boost/includeexport CPLUS_INCLUDE_PATH LI ...
- linux下编译boost的多线程程序
linux下面用boost库进行多线程编程,一开始总是编译不成功,花了好多的时间. 下面是一段小示例代码: //start from the very beginning,and to create ...
- boost进程间通信经常使用开发一篇全(消息队列,共享内存,信号)
本文概要: 敏捷开发大家想必知道并且评价甚高,缩短开发周期,提高开发质量.将大project独立为不同的小app开发,整个开发过程,程序可用可測,所以提高了总体的质量.基于这样的开发模式和开发理念,进 ...
随机推荐
- BZOJ2563阿狸和桃子的游戏
2563: 阿狸和桃子的游戏 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 952 Solved: 682[Submit][Status][Discu ...
- CSS:word-wrap/overflow/transition
一 自动换行:一个div有固定宽高,如果其内容很长,必须两行以上才能显示完整的时候,有两种情况要留意 1 默认如果其内容都是中文,那么内容是可以自适应,而不会溢出div 2 如果内容除了中文之外,还有 ...
- Parallel Programming-Concurrent Collections
备忘:asp.net平台下线程安全集合类. Class Description BlockingCollection<T> Provides blocking and bounding ...
- 找工作-——网络IO
网络层 主要任务是把网络协议数据单元或分组从源计算机经过适当的路径发送到目的地计算机.从源计算机到目的计算机可能要经过若干个中间节点,这需要在通信子网中进行路由选择. 网络层与数据链路层有很大的差别, ...
- MyEclipse 手动安装Velocity 编辑器
最近项目有使用Velocity 模板引擎,从而会用到*.VM页面!Myeclipse打开VM页面字体一片漆黑,哪有JSP那样看起来舒服(个人感觉)!为了解决这一问题就要安装Velocity编辑器,安装 ...
- JS---script的位置
都可以,但各有千秋.放在head中:统一管理,方便维护:但浏览器会首先加载js文件,如果js文件过大,会造成页面在加载js的时候“无反应”时间过长,影响用户体验.放在body中(或放在body后):浏 ...
- windows7下安装gem包---bcrypt-ruby
在Gemfile中添加 gem 'bcrypt-ruby', '~> 3.0.0' 然后执行bundle install,rails服务启动没有问题,但是运行程序时页面报错如下: cannot ...
- MySQL绿色版的安装步骤
由于工作需要最近要开始研究MySQL了(看来学习都是逼出来的),本人对mysql没有研究,可以说一个小白. 下面就从安装开始吧,虽然网上关于这方面的东西很多,还是需要自己把操作过程写下来. 1.数据库 ...
- idea2016 64位 安装,jdk环境变量配置
idea 激活服务器地址: 地址1: http://www.iteblog.com/idea/key.php 地址2: http://idea.qinxi1992.cn/ intelli ...
- .Net下RabbitMQ发布订阅模式实践
一.概念AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的解耦,消息的发 ...