菜鸟笔记
提升您的技术认知

c 11线程池-ag真人游戏

 这个版本的线程池,可以多次添加task,基本可用

缺陷:

  • 没有使用c 11的条件变量 
#include 
#include 
#include 
#include 
#include 
#include 
::sem_t tasksem;
class threadpool
{
public:
    threadpool():
    threadnums(10)
    {   
        ::sem_init(&tasksem, 0, 0);
    }
    ~threadpool()
    {
        for(auto &i : threadlist)
        {
            i.join();
        }
    }
private:
    std::vector threadlist;
    using task = std::function;
    std::queue taskqueue;
    int threadnums;
public:
    void setthreadnums(int nums) { threadnums = nums; }
    void start()
    {
        for(int i = 0; i < threadnums; i  )
        {
            threadlist.emplace_back(std::thread(threadpool::runtask, this));
        }
    }
    void addtask(task task)
    {
        taskqueue.push(task);
        ::sem_post(&tasksem);
    }
private:
    void runtask()
    {
        ::sem_wait(&tasksem);
    
        task task = taskqueue.front();
        taskqueue.pop();
        task();
    }
};
int main()
{
    threadpool threadpool;
    threadpool.start();
    threadpool.addtask(
        [](){std::cout << "addtask" << std::endl;}
    );
    threadpool.addtask(
        [](){std::cout << "addtask" << std::endl;}
    );
    threadpool.addtask(
        [](){std::cout << "addtask" << std::endl;}
    );
 
    return 0;
}

加了锁的线程池

//"threadpool.h"
#ifndef threadpool
#define threadpool
#pragma once
#include 
#include 
#include 
#include 
#include 
// #include 
::sem_t tasksem;
std::mutex mutex;
namespace threadpool
{
class threadpool
{
private:
    int threadnums;
    using task = std::function;
    std::vector taskqueue;
    std::vector threadpool;
public:
    threadpool();
    ~threadpool();
    void setthreadnums(int nums) { threadnums = nums; }
public:
    void start();
    void addtask(task task);
private:
    void run();
};
}
#endif

 

//"threadpool.cc"
#include "./threadpool.h"
#include 
#include 
threadpool::threadpool::threadpool():
    threadnums(10)
{
    ::sem_init(&tasksem, 0, 0);
}
threadpool::threadpool::~threadpool()
{
    for(auto &i : threadpool)
    {
        i->join();
    }
}
void threadpool::threadpool::start()
{
    for(int i; i < threadnums;   i)
    {
        threadpool.emplace_back(new std::thread(threadpool::threadpool::run, this));
    }
}
void threadpool::threadpool::addtask(task task)
{
    {
        std::unique_lock lock(mutex);
        taskqueue.emplace_back(task);
        ::sem_post(&tasksem);
    }
}
void threadpool::threadpool::run()
{
    {
        ::sem_wait(&tasksem);//有任务到来,收到通知,开始运行
        std::unique_lock lock(mutex);
        task task = taskqueue.front();
        taskqueue.pop_back();
        task();
    }
}
void add(threadpool::threadpool& threadpool, int i)
{
    std::this_thread::sleep_for(std::chrono::microseconds(1000));
    threadpool.addtask(
        [&](){std::cout << "addtask" << i << std::endl;}
    );
}
int main()
{
    threadpool::threadpool threadpool;
 
    threadpool.setthreadnums(400);
    threadpool.start();
    for(int i = 0; i < 400; i  )
    {
        add(threadpool, i);
    }
    return 0;
}

 

网站地图