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开发,整个开发过程,程序可用可測,所以提高了总体的质量.基于这样的开发模式和开发理念,进 ...
随机推荐
- C/C++ 安全编码 —— 不安全的函数
1. 文件与IO操作 gets():从控制台输入到字符数组: char response[8]; gets(response); 如果控制台输入超过 8 个字符,程序便会发生不确定的行为.其主要问题在 ...
- delete操作符
delete操作符通常用来删除对象的属性: Js代码 var o = { x: 1 }; delete o.x; // true o.x; // undefined 而不是一般的变量: Js代 ...
- 简单的RBAC用户角色权限控制
Java web项目中,无论项目是大是小,或多或少都会涉及到用户访问权限的控制,权限管理总体的设计思路就是,不该看的不看,不该做的不做!据我目前的了解,我所知道的几种实现访问权限控制的方式有: JQu ...
- Zend Server 安装与配置图文教程
Zend Server是一款专业的PHP Web开发应用服务器,一些初次接触并使用此程序的朋友可能不太了解安装方法,本文为您提供了Zend Server 安装与配置图文教程,欢迎大家阅读,并提出自己的 ...
- BluetoothFindFirstRadio 函数
HBLUETOOTH_RADIO_FIND BluetoothFindFirstRadio( BLUETOOTH_FIND_RADIO_PARAMS* pbtfrp, HANDLE* phRadio ...
- viewstate的基本用法
转自:http://www.cnblogs.com/ooip/p/4743536.html 在web窗体将控件属性设置为runat=server时,这个控件会被添加一个隐藏属性_ViewState,_ ...
- 安装yum在ubnutu上
安装yum在ubnutu上1:首先检测是否安装了build-essential程序包 apt-get install build-essential2.安装yumapt-get install yum ...
- Windows 10 PC 安装 Docker CE
系统要求 Docker CE 支持 64 位版本的 Windows 10 Pro,且必须开启 Hyper-V. 如果系统是win 10 家庭版安装 docker 很恶心, 我也是废了2天才安装, 由 ...
- Learning Python 010 函数 2
Python 函数 2 函数的参数 位置参数(普通,正常的参数) 随便编写一个求x^n的值的函数power(x, n): def power(x, n): s = 1 while n > 0: ...
- Struts2学习第七课 通配符映射
一个WEB应用可能有长百上千个action声明,可以利用struts提供的通配符映射机制吧多个彼此相识的映射关系简化为一个映射关系. 通配符映射规则: --若找到多个匹配,没有通配符的那个将胜出(精确 ...