这工作:构建一个std ::线程与功能
std::thread t = std::thread(printf, "%d", 1);
这不起作用:
t2 = std::thread(my_thread_func , std::ref(context));
OR
std::thread t1 = std::thread(my_thread_func , context_add);
my_thread_func定义:
int my_thread_func(long long *context_add)
背景是一些结构..我试图 '通过引用传递'
错误的事:
function call missing argument list;
error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments
>>>编辑< < <
对不起混乱。 ..实际上我在公共MainPage中定义了my_thread_func,所以我不能使用native类型,因此我认为值得尝试很长时间并给出它的地址。
/* test data types, context for thread function, in .cpp (not in namespace) */
typedef struct _test_context
{
HANDLE hHandle;
unsigned int flag;
}test_context_t;
test_context_t *context;
//Do something with its member
context_add = (long long *)context;
std::thread t2 = std::thread(sem_waiting_thread, context_add);
错误:
error C3867: 'App1::MainPage::my_thread_func': function call missing argument list; use '&App1::MainPage::my_thread_func' to create a pointer to member
error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments
我的命名空间的样子:
namespace App1
{
public ref class MainPage sealed
{
public:
MainPage();
public:
MainPage();
int my_thread_func(long long cotext);
..
};
}
< < < < EDIT 2 >>> 我现在很好奇..这个简单的一个也不起作用!
void f1(int n)
{
for(int i=0; i<5; ++i) {
// Print n
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
.
.
.
int n=0;
std::thread t2(f1, n+1); (//This didn't work,same error!)
.
.
.
but this worked!
std::thread t2;
.
.
.
t2= std::thread (f1, n+1);
从这里尝试:http://en.cppreference.com/w/cpp/thread/thread/thread
的问题是有点混乱.. 。* context是一些struct *,但是在声明中它是一个指向'long long'的指针......你可以发布函数的实际声明和你想传入的类型吗? – 2012-07-19 17:20:43
你应该展示一个完整的例子,在非工作的std :: thread构造函数行注释掉的情况下编译。然后有人可以告诉你如何正确地编写该行。否则,我们必须猜测。尽管如此,我认为betabandido确实在这种情况下能够成功回答 - 但我们大多数人并不像他那样通灵。 – abarnert 2012-07-19 17:22:00
你是什么意思“我不能使用原生类型”?你为什么要将'context'投射到'long long'? – betabandido 2012-07-19 17:33:11