c ++存储函数和参数列表以供以后使用 [英] c++ store function and parameter list for later use

查看:50
本文介绍了c ++存储函数和参数列表以供以后使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想通过用C ++编写一个小的线程池来挑战自己,我想尝试模仿std :: thread使用的简单易用的方式,您可以只创建一个线程,并在参数发送函数时以及该函数的参数,与pthread之类的东西相比,它迫使您将void *作为该函数的唯一数据.

So I wanted to challenge myself by writing a small threadpool in C++, and I wanted to try to mimic the easy to use way that std::thread work with, that you can just create a thread and as parameters send a function and parameters for that function, compared to something like pthreads which force you to have a void* as the only indata for the function.

到目前为止,我已经能够使用模板和参数包创建一个函数,该函数可以为其使用另一个函数和参数并执行该函数,但是我找不到一种存储它们的方法,因此可以在以下位置执行它们稍后的时间(当线程池中有空闲线程时).我已经尝试过同时使用std :: function和std :: tuple和std :: bind,但是由于我不知道我要处理的类型是什么,所以找不到方法来存储函数和参数这样我以后就可以在代码的另一部分中使用它们了,因为那时候我不再知道所有内容都是什么类型.下面是我一直在弄乱的一些代码,可能有助于显示我的意思.

So far I have been able to use templates and parameter packs to create a function that can take another function and parameters for it and execute it, but I can't find a way to store them so that I can execute them at a later time (when there is a free thread in the threadpool). I have tried using both std::function together with std::tuple, and std::bind, but since I don't know exactly what types I am dealing with I can't find a way to store the function and the parameters so that I can use them later on in another part of my code, since at that point I no longer know what types everything is of. Down below is some code I have been messing around with that might help show how I mean.

template<typename Function, typename... Arguments>
void TestFunction(Function func, Arguments... parameters)
{

std::function<std::result_of<Function(Arguments...)>::type(Arguments...)>* tempFunc;
tempFunc = new std::function<std::result_of<Function(Arguments...)>::type(Arguments...)>(func);
void* funcPtr = tempFunc;
std::tuple<Arguments...>* tempTuple;
tempTuple = new std::tuple<Arguments...>(parameters...);
void* tuplePtr = tempTuple;

//func(parameters...);
(Arguments...)>*)funcPtr, *(std::tuple<Arguments...>*)tuplePtr);

auto bindTest = std::bind(func, parameters...);
bindTest();
void* bindPtr = &bindTest;

}

int main()
{
TestFunction(std::printf, "%d, %d, %d\n", 3, 2, 1);

getchar();
return 0;
}

可能无法完成我想做的事情,在那种情况下,我想我只需要切换到更像pthreads的方法即可.但是,如果有人知道可以解决的问题,我将不胜感激.

It might be that it's not possible to do what I want to do, and in that case I guess I'll just have to switch to an approach more like pthreads. But if anyone knows a work around I would be grateful.

推荐答案

关键是可以将std :: bind的返回类型存储在std :: function中.因为std :: bind返回一个可调用的对象.然后,您应该能够存储std :: function实例,具体取决于您要如何处理返回类型.

The key thing is that you can store the return type of std::bind in a std::function. Because std::bind returns an object that is callable. You should then be able to store the std::function instance depending on how you want to handle the return type.

template<typename Function, typename... Arguments>
void TestFunction(Function func, Arguments... parameters)
{
    using Ret = typename std::result_of<Function>::type;
    std::function<Ret()> val{std::bind(func, parameters...)};
}

如果您在初次调用该函数时这样做,则不必再考虑参数类型,而只需考虑返回类型.处理返回类型的方式将取决于存储函数的用例.一种简单的方法是要求 Function 是一个void函数,如果没有办法将值传递回API使用者,这可能很有意义.

If you do this when you first recive the function you no longer have to think about the arguments type, and only the return type. How you handle the return type will depend on the usecase of storing the function. One simple approach is to require that Function is a void function, which may make sense if there is no way to pass the value back to the consumer of the API.

这篇关于c ++存储函数和参数列表以供以后使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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