当您进入队列时如何将参数传递给函数 [英] How to pass parameter into function when you push into queue

查看:166
本文介绍了当您进入队列时如何将参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.如何在推入队列时将参数传递给函数

1.How to pass parameter into function when push into queue

2.如果使用双端队列而不是队列,将发生死锁,

2.Will deadlock occur if using deque instead of queue,

  我想始终在双端队列的前面推送写入功能,并在双端队列的末尾推送读取功能.线程池只会首先弹出前端.这种设计有效吗?

    i want to always push write function at the front of deque, and read function at the end of deque. threadpool only first pop the front.Do this design work?

 

 

#include <thread>
#include <atomic>
#include <vector>
#include <queue>

class join_threads
{
    std::vector<std::thread>& threads;
public:
    explicit join_threads(std::vector<std::thread>& threads_):threads(threads_)
    {}
    ~join_threads()
    {
        for(unsigned long i=0;i<threads.size();++i)
        {
            if(threads[i].joinable())
                threads[i].join();
        }
    }
};

template<typename T>
class thread_safe_queue
{
private:
    mutable std::mutex mut;
    std::queue<T> data_queue;
    std::condition_variable data_cond;
public:
    thread_safe_queue(){}
    thread_safe_queue(thread_safe_queue const& other)
    {
        std::lock_guard<std::mutex> lk(other.mut);
        data_queue=other.data_queue;
    }
    void push(T new_value)
    {
        std::lock_guard<std::mutex> lk(mut);
        data_queue.push(new_value);
        data_cond.notify_one();
    }
    void wait_and_pop(T& value)
    {
        std::unique_lock<std::mutex> lk(mut);
        //data_cond.wait(lk,[this]{return !data_queue.empty();});
        data_cond.wait(lk);
        value=data_queue.front();
        data_queue.pop();
    }
    std::shared_ptr<T> wait_and_pop()
    {
        std::unique_lock<std::mutex> lk(mut);
        //data_cond.wait(lk,[this]{return !data_queue.empty();});
        data_cond.wait(lk);
        std::shared_ptr<T> res(new T(data_queue.front()));
        data_queue.pop();
        return res;
    }
    bool try_pop(T& value)
    {
        std::lock_guard<std::mutex> lk(mut);
        if(data_queue.empty())
        return false;
        value=data_queue.front();
        data_queue.pop();
        printf("pop");
        return true;
    }
    std::shared_ptr<T> try_pop()
    {
        std::lock_guard<std::mutex> lk(mut);
        if(data_queue.empty())
        return std::shared_ptr<T>();
        std::shared_ptr<T> res(new T(data_queue.front()));
        data_queue.pop();
        return res;
    }
    bool empty() const
    {
        std::lock_guard<std::mutex> lk(mut);
        return data_queue.empty();
    }
};

class thread_pool
{
    join_threads joiner;
    std::atomic_bool done;
    thread_safe_queue<std::function<void()> > work_queue;
    std::vector<std::thread> threads;

    void worker_thread()
    {
        while(!done)
        {
            //printf("workerthread");
            std::function<void()> task;
            if(work_queue.try_pop(task))
            {
                printf("task start\n");
                task();
                printf("task end\n");
            }
            else
            {
                std::this_thread::yield();
            }
        }
    }
public:
    thread_pool() : joiner(threads),done(false)
    {
        //unsigned const thread_count=std::thread::hardware_concurrency();
        try
        {
            for(unsigned i=0;i<6;++i)
            {
                printf("push %d",i);
                threads.push_back(std::thread(&thread_pool::worker_thread,this));
            }
        }
        catch(std::bad_alloc)
        {
            done=true;
            throw;
        }
    }
    bool getDone()
    {
        return done.load(std::memory_order_acquire);
    }
    ~thread_pool()
    {
        done=true;
    }
    template<typename FunctionType>
    void submit(FunctionType f, int& num)
    {
        work_queue.push(std::function<void()>(f)(num));
    }
};

void a(int& num)
{
    while(true)
    {
        num += 1;
        printf("a %d \n", num);
    }
}
void b(int& num)
{
    while(true)
    {
        num += 1;
        printf("b %d \n", num);
    }
}
void c(int& num)
{
    while(true)
    {
        num += 1;
        printf("c %d \n", num);
    }
}
int main()
{
    printf("begin\n");
    thread_pool* pool = new thread_pool();
    printf("submit start\n");
    int num = 0;
    pool->submit(a,num);
    pool->submit(b,num);
    pool->submit(c,num);

    printf("submit finish\n");
    while(pool->getDone() == false){sleep(100);};
}

 

推荐答案

在我看来,您应该使用函数指针而不是FunctionType.
我们可以创建一个这样的函数指针:typedef void(* fun)(int& num);

In my opinion, you should use function pointers instead of FunctionType.
We can create a function pointer like this: typedef void (*fun)(int& num);


我认为您可以将函数和参数封装在一起,例如,可以创建一个包含指向函数及其参数的指针的结构.
然后将结构添加到队列中.
 
如果将函数的指针和参数放入数据类型,我认为使用双端队列时不会发生死锁.


I think you can encapsulate your function and the parameter together, for example, you can create a struct containing the pointer to a function and its parameters.
And then add the structs into the queue.
 
If you put the function's pointer and parameter into a data type, I think deadlock will not occur when using deque.


这篇关于当您进入队列时如何将参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆