一、进程与线程

cpu一般有m核n线程的说法,那么该cpu只能同时运行n个线程(线程中没有sleep)。

#include <thread>
#include <mutex>
#include <atomic>
#include <condition_variable>
#include <vector>
#include <GSLAM/core/Glog.h>
#include <GSLAM/core/Mutex.h> void simple_threadfunc()
{
LOG(INFO)<<"Simple thread function.";
} void simple_pooledfunc(int i){
LOG(INFO)<<"Thread "<<i<<", ID:"<<std::this_thread::get_id();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
} class MultiReadWrite{
public:
std::vector<int> _vec;
std::mutex _mutex;
std::atomic<bool> _shouldStop;
std::vector<std::thread> _threads; MultiReadWrite(){
_shouldStop=false;
for(int i=0;i<2;i++)
_threads.push_back(std::thread(&MultiReadWrite::writeThread,this));
for(int i=0;i<2;i++)
_threads.push_back(std::thread(&MultiReadWrite::readThread,this));
for(int i=0;i<2;i++)
_threads.push_back(std::thread(&MultiReadWrite::deleteThread,this));
}
~MultiReadWrite(){
_shouldStop=true;
for(auto& t:_threads) t.join();
} void writeThread(){
for(int i=0;!_shouldStop;i++)
{
_mutex.lock();
if(_vec.size()<100)
_vec.push_back(i);
_mutex.unlock();
std::this_thread::sleep_for(std::chrono::microseconds(5));
}
} void readThread(){
while(!_shouldStop)
{
{
std::unique_lock<std::mutex> lock(_mutex);
if(_vec.size())
LOG(INFO)<<std::this_thread::get_id()<<" get "<<_vec.back();
} std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
} void deleteThread(){
while(!_shouldStop){
{ std::unique_lock<std::mutex> lock(_mutex);
if(_vec.size()>1)
_vec.pop_back();
}
std::this_thread::sleep_for(std::chrono::microseconds(5));
}
}
}; class ConditionPool
{
public:
std::mutex _mutex;
std::condition_variable _condition;
std::vector<std::thread> _threads;
bool _ready; ConditionPool(int thread_num=4){
_ready=false;
for(int i=0;i<thread_num;i++)
_threads.push_back(std::thread(&ConditionPool::process,this));
} ~ConditionPool(){
_condition.notify_all();
for(auto& t:_threads) t.join();
} void process() {
std::unique_lock<std::mutex> lck(_mutex);
while (!_ready)
_condition.wait(lck);
LOG(INFO) << "thread " << std::this_thread::get_id();
} void go() {
std::unique_lock<std::mutex> lck(_mutex);
_ready = true;
_condition.notify_all();
}
}; // A simple threadpool implementation.
class ThreadPool {
public:
// All the threads are created upon construction.
explicit ThreadPool(const int num_threads): stop(false) {
CHECK_GE(num_threads, 1)
<< "The number of threads specified to the ThreadPool is insufficient.";
for (size_t i = 0; i < num_threads; ++i) {
workers.emplace_back([this] {
for (;;) {
std::function<void()> task; {
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock, [this] {
return this->stop || !this->tasks.empty();
});
if (this->stop && this->tasks.empty()) return;
task = std::move(this->tasks.front());
this->tasks.pop();
} task();
}
});
}
}
~ThreadPool(){
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for (std::thread& worker : workers)
worker.join();
} // Adds a task to the threadpool.
template <class F, class... Args>
auto Add(F&& f, Args&& ... args)
->std::future<typename std::result_of<F(Args...)>::type>; private:
// Keep track of threads so we can join them
std::vector<std::thread> workers;
// The task queue
std::queue<std::function<void()> > tasks; // Synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop; }; // add new work item to the pool
template <class F, class... Args>
auto ThreadPool::Add(F&& f, Args&& ... args)
->std::future<typename std::result_of<F(Args...)>::type> {
using return_type = typename std::result_of<F(Args...)>::type; auto task = std::make_shared<std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)); std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex); // don't allow enqueueing after stopping the pool
CHECK(!stop) << "The ThreadPool object has been destroyed! Cannot add more "
"tasks to the ThreadPool!"; tasks.emplace([task]() {
(*task)();
});
}
condition.notify_one();
return res;
} int cpp11_thread()
{
// simple thread call
std::thread thread1(simple_threadfunc);
thread1.join(); // simple thread with lamda func
std::vector<int> vec(4,10);
std::thread thread2([&vec]{for(auto i:vec) vec[0]+=i;});
thread2.join();
LOG(INFO)<<vec[0]; // simple thread pool
std::vector<std::thread> threads(4);
for(int i=0;i<threads.size();i++)
threads[i]=std::thread(simple_pooledfunc,i); for(auto& thread:threads) thread.join(); // simple multi readwrite, mutex
{
MultiReadWrite mutexTest;
std::this_thread::sleep_for(std::chrono::seconds(1));
} // simple condition usage
ConditionPool conditionPool(4);
conditionPool.go(); // thread pool with future return
ThreadPool pool(4);
std::vector<int> vec1(4,10);
auto result=pool.Add([vec1]{int sum=0;for(auto i:vec1) sum+=i;return sum;});
result.wait(); LOG(INFO)<<"sum is "<<result.get();
}

cpp11_thread线程的更多相关文章

  1. C++11下的线程池以及灵活的functional + bind + lamda

    利用boost的thread实现一个线程类,维护一个任务队列,以便可以承载非常灵活的调用.这个线程类可以方便的为后面的线程池打好基础.线程池还是动态均衡,没有什么别的.由于minGW 4.7 对 C+ ...

  2. [ 高并发]Java高并发编程系列第二篇--线程同步

    高并发,听起来高大上的一个词汇,在身处于互联网潮的社会大趋势下,高并发赋予了更多的传奇色彩.首先,我们可以看到很多招聘中,会提到有高并发项目者优先.高并发,意味着,你的前雇主,有很大的业务层面的需求, ...

  3. [高并发]Java高并发编程系列开山篇--线程实现

    Java是最早开始有并发的语言之一,再过去传统多任务的模式下,人们发现很难解决一些更为复杂的问题,这个时候我们就有了并发. 引用 多线程比多任务更加有挑战.多线程是在同一个程序内部并行执行,因此会对相 ...

  4. 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)

    前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...

  5. Java 线程

    线程:线程是进程的组成部分,一个进程可以拥有多个线程,而一个线程必须拥有一个父进程.线程可以拥有自己的堆栈,自己的程序计数器和自己的局部变量,但不能拥有系统资源.它与父进程的其他线程共享该进程的所有资 ...

  6. C++实现线程安全的单例模式

    在某些应用环境下面,一个类只允许有一个实例,这就是著名的单例模式.单例模式分为懒汉模式,跟饿汉模式两种. 首先给出饿汉模式的实现 template <class T> class sing ...

  7. 记一次tomcat线程创建异常调优:unable to create new native thread

    测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...

  8. Android线程管理之ThreadLocal理解及应用场景

    前言: 最近在学习总结Android的动画效果,当学到Android属性动画的时候大致看了下源代码,里面的AnimationHandler存取使用了ThreadLocal,激起了我很大的好奇心以及兴趣 ...

  9. C#多线程之线程池篇3

    在上一篇C#多线程之线程池篇2中,我们主要学习了线程池和并行度以及如何实现取消选项的相关知识.在这一篇中,我们主要学习如何使用等待句柄和超时.使用计时器和使用BackgroundWorker组件的相关 ...

随机推荐

  1. [Java] SpringMVC工作原理之四:MultipartResolver

    MultipartResolver 用于处理文件上传,当收到请求时 DispatcherServlet 的 checkMultipart() 方法会调用 MultipartResolver 的 isM ...

  2. 【FJWC 2019】min

    [FJWC 2019]min 题目描述 给你一张 \(n\) 个点 \(m\) 条边的无向图,走过每条边都需要花费 \(1\) 秒. 给你一个整数 \(k\) ,请你选择至多 \(k\) 个点,令经过 ...

  3. googLeNet网络

    1.什么是inception结构 2.什么是Hebbian原理 3.什么是多尺度处理 最近深度学习的发展,大多来源于新的想法,算法以及网络结构的改善,而不是依赖于硬件,新的数据集,更深的网络,并且深度 ...

  4. Mac中selenium使用出现错误

    解决方案是: 首先通过brew 安装 $ brew install geckodriver 然后设置配置文件~/.bash_profile文件 export PATH=$PATH:/path/to/g ...

  5. SQlite源码分析-体系结构

    体系结构 在内部,SQLite由以下几个组件组成:内核.SQL编译器.后端以及附件.SQLite通过利用虚拟机和虚拟数据库引擎(VDBE),使调试.修改和扩展SQLite的内核变得更加方便.所有SQL ...

  6. Apache Shiro对象概念

    #,Authentication,认证,也就是验证用户的身份,就是确定你是不是你,比如通过用户名.密码的方式验证,或者某些第三方认证,比如微信认证. #,Authorization,授权,也叫访问控制 ...

  7. 自然语言处理之LDA主题模型

    1.LDA概述 在机器学习领域,LDA是两个常用模型的简称:线性判别分析(Linear Discriminant Analysis)和 隐含狄利克雷分布(Latent Dirichlet Alloca ...

  8. [SDOi2012]吊灯

    嘟嘟嘟 这题想了半天,搞出了一个\(O(10 * d * n)\)(\(d\)为\(n\)的约数个数)的贪心算法,就是能在子树内匹配就在子树内匹配,否则把没匹配的都交给父亲,看父亲能否匹配.交上去开了 ...

  9. 【转】使用ffmpeg转码的MP4文件需要加载完了才能播放的解决办法

    1.前一段时间做了一个ffmpeg转码MP4的项目,但是转出来的MP4部署在网站上需要把整个视频加载完成才能播放,到处找资料,最后找到解决方案记录于此备忘. FFMpeg转码由此得到的mp4文件中, ...

  10. Java关系运算

    关系运算符用来比较两个值的关系.关系运算符是二元运算符,运算结果是 boolean 型.当运算符对应的关系成立时,运算结果是 true,否则是 false. 关系表达式是由关系运算符连接起来的表达式. ...