mercredi 24 février 2021

Is there a way to pas rvalue reference of some unique_ptr to thread pool

I'm using C++11 (I can't use newer C++'s standards). I can't pass a function with rvalue reference of unique_ptr to my thread pool.

Here is a simple code.

Working with test2 function and not working with test1.

#include "thread-pool.hpp"
#include <iostream>
#include <string>

struct Mystruct
{
    int a;
    int b;
    std::string c;
};

void test1(int id, std::unique_ptr<Mystruct>&& ms, int a)
{
    while (true)
    {
        std::cout << ms->c << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(a));
    }
}

void test2(int id, std::string&& c, int a)
{
    while (true)
    {
        std::cout << c << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(a));
    }
}

int main()
{
    ctpl::thread_pool p(10);
    Mystruct* ms = new Mystruct;
    std::unique_ptr<Mystruct> msp(ms);
    p.push(test1, msp, 10);
    p.push(test2, "this is the end", 10);
    p.push(test2, "hello from the other side", 5);
    return 0;
}

I am getting these errors:

no instance of overloaded function "ctpl::thread_pool::push" matches the argument list
C2893   Failed to specialize function template 'std::future<unknown-type> ctpl::thread_pool::push(F &&,Rest &&...)'
C2780   'std::future<unknown-type> ctpl::thread_pool::push(F &&)': expects 1 arguments - 3 provided
C2672   'ctpl::thread_pool::push': no matching overloaded function found

I am using vit vit repository for my thread pool implementations.I am using this link for thread pool implementation for my thread-pool.hpp file.

I can't change my test1's function arguments due i am using another API. Is there a way to pass this function to my thread pool object or another implementation of thread pool.

Aucun commentaire:

Enregistrer un commentaire