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开发,整个开发过程,程序可用可測,所以提高了总体的质量.基于这样的开发模式和开发理念,进 ...
随机推荐
- 【二叉树的递归】05二叉树中找任意起点和终点使他们的路径和最大【Binary Tree Maximum Path Sum】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,寻找值最大的路径. ...
- 分享几个高效编写JS 的心得
原则 不要做任何优化除非的确需要优化 任何的性能优化都必须以测量数据为基础,如果你怀疑代码存在性能问题,首先通过测试来验证你的想法.性能优化三问 我还能做哪些工作从而让代码变得更有效率? 流行的J ...
- Mathf.Sin正弦
输入参数是弧度 Mathf.Sin public static float Sin(float f); Parameters Description Returns the sine of ang ...
- Linux编程之错误代码
头文件/usr/include/asm-generic/errno-base.h定义错误码: #ifndef _ASM_GENERIC_ERRNO_BASE_H #define _ASM_GENERI ...
- 微服务理论之六:ESB与SOA的关系
一.SOA和ESB一直是没有明确概念的两个缩略词 SOA----面向服务架构,实际上强调的是软件的一种架构,一种支撑软件运行的相对稳定的结构,表面含义如此,其实SOA是一种通过服务整合来解决系统集成的 ...
- String/ StringBuilder/ StringBuffer
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- [51nod1247]可能的路径(思维题)
题意:给定(a,b),(x,y) ,(a,b)可以通向(a-b,b) (a+b,b) (a,a+b) (a,a-b) 求能否到达(x,y) 解题关键:类似于更相减损,变换过程中gcd是一样的. #i ...
- 【总结整理】JQuery基础学习---样式篇
进入官方网站获取最新的版本 http://jquery.com/download/ 中文 https://www.jquery123.com/ <!--JQuery:轻量级的JavaScr ...
- Centos 查看机器型号
测试机器的硬件信息: 查看CPU信息(型号) # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 8 Intel(R) Xeon(R) CP ...
- 38、EST序列拼接流程
转载:http://fhqdddddd.blog.163.com/blog/static/18699154201241014835362/ http://blog.sina.com.cn/s/blog ...