I have a function called workForThread, that takes two arguments, and returns void. I would like to thread this function using something like:
我有一个名为workForThread的函数,它接受两个参数,并返回void。我想用以下方法来连接这个函数:
thread(workForThread,a,b);
Where a
and b
are of the appropriate types. The above code does not compile, giving a "too many arguments for call" error ("error C2197: 'void (__cdecl *)(char *)' : too many arguments for call")
其中a和b属于适当的类型。上面的代码没有编译,给出了“调用的参数太多”错误(“error C2197: 'void (__cdecl *)(char *):调用的参数太多”))
How can I resolve this?
我如何解决这个问题?
Note: I have looked at these two questions, but the resolutions that work there do not seem to work for me. Additionally, I have a feeling there is a way to do it built into c++11, and that is what I am looking for.
注意:我已经看了这两个问题,但是在那里起作用的决心似乎对我不起作用。另外,我觉得有一种方法可以把它构建成c++11,这就是我想要的。
2 个解决方案
#1
7
When using C++11 you could use a lambda function which may use (non-formal) parameter of the context. "Capturing"
使用c++ 11时,可以使用lambda函数,该函数可以使用上下文的(非形式的)参数。“捕捉”
Something like
类似的
void doIt (int a, int b) { // do something, your workForThread
}
..
int a = 1;
int b = 2;
std:thread r ([=](){doIt (a, b); return 1;});
When only calling a single function juanchopanza answer may be somewhat more efficient since no new function will be created.
当只调用一个函数时,juanchopanza answer可能会更有效,因为不会创建新的函数。
The lambda version allows you to configure more. Let say you are starting threads which calls in the end 2 functions out of 3. juanchopanza approach would require NAMED functions for each permutation.
lambda版本允许您配置更多。假设你正在启动一个线程,它调用最后2个函数。juanchopanza方法需要为每个排列指定函数。
At the moment I consider the difference of both approaches to be mostly a matter of taste.
目前,我认为这两种方法的不同主要在于品味。
When you like to read more about lambda functions
当你想读更多关于lambda函数的内容时
What is a lambda expression in C++11?
C+ 11的表达式是什么?
#2
13
In C++11, the way to do it is more or less the way you have attempted:
在c++ 11中,实现它的方法或多或少与您尝试过的方法类似:
std::thread myThread(workForThread,a,b);
provided workForThread is a (non-member) function that takes those two parameters.
提供的workForThread是一个(非成员)函数,它接受这两个参数。
#1
7
When using C++11 you could use a lambda function which may use (non-formal) parameter of the context. "Capturing"
使用c++ 11时,可以使用lambda函数,该函数可以使用上下文的(非形式的)参数。“捕捉”
Something like
类似的
void doIt (int a, int b) { // do something, your workForThread
}
..
int a = 1;
int b = 2;
std:thread r ([=](){doIt (a, b); return 1;});
When only calling a single function juanchopanza answer may be somewhat more efficient since no new function will be created.
当只调用一个函数时,juanchopanza answer可能会更有效,因为不会创建新的函数。
The lambda version allows you to configure more. Let say you are starting threads which calls in the end 2 functions out of 3. juanchopanza approach would require NAMED functions for each permutation.
lambda版本允许您配置更多。假设你正在启动一个线程,它调用最后2个函数。juanchopanza方法需要为每个排列指定函数。
At the moment I consider the difference of both approaches to be mostly a matter of taste.
目前,我认为这两种方法的不同主要在于品味。
When you like to read more about lambda functions
当你想读更多关于lambda函数的内容时
What is a lambda expression in C++11?
C+ 11的表达式是什么?
#2
13
In C++11, the way to do it is more or less the way you have attempted:
在c++ 11中,实现它的方法或多或少与您尝试过的方法类似:
std::thread myThread(workForThread,a,b);
provided workForThread is a (non-member) function that takes those two parameters.
提供的workForThread是一个(非成员)函数,它接受这两个参数。