C++ 如何将函数指针传递到此函数
我尝试使用一个线程库,它有一个定义如下的线程启动函数:C++ 如何将函数指针传递到此函数,c++,function-pointers,C++,Function Pointers,我尝试使用一个线程库,它有一个定义如下的线程启动函数: int vg_thread_create(VGThreadFunction func, void *arg, VGThreadDescriptor *tid, void *stack, size_t stack_size, long priority, int flags ); void doit() { cout << "Woohoo printing stuff in new thread
int vg_thread_create(VGThreadFunction func,
void *arg,
VGThreadDescriptor *tid,
void *stack,
size_t stack_size,
long priority,
int flags );
void doit() {
cout << "Woohoo printing stuff in new thread\n";
}
vgthread函数的类型如下:
typedef void* (*VGThreadFunction) ( void* )
为了便于论证,我有一个这样的函数:
int vg_thread_create(VGThreadFunction func,
void *arg,
VGThreadDescriptor *tid,
void *stack,
size_t stack_size,
long priority,
int flags );
void doit() {
cout << "Woohoo printing stuff in new thread\n";
}
我明白了
错误C2440:“正在初始化”:无法从“void(u cdecl*)(void)”转换为“VGThreadFunction”
如何使用vg_线程_创建
编辑
我试过这个:
void* doit(void* donothingvar) {
cout << "printing stuff in new thread\n";
return donothingvar;
}
然后,我在调用vg_线程_创建时遇到访问冲突
如果我进入函数,它实际上会在调用_beginthreadex时崩溃
Unhandled exception at 0x0040f5d3 in threadtest.exe: 0xC0000005: Access violation writing location 0x00000000.
有什么想法吗
通过进入函数,我发现我必须传递一个有效的指针——传递零会导致空的解引用。抱歉,有点特殊。是的,函数签名必须匹配。只需在函数上添加一个伪参数。是的,函数签名必须匹配。只需在函数中添加一个伪参数。@H2CO3,根据编译器使用的参数传递约定,它可能会把事情搞砸。我有理由肯定它是未定义的行为,即使它有效。@H2CO3,[expr.reinterpret.cast]/6:“通过指向与函数定义中使用的类型不同的函数类型(8.3.5)的指针调用函数的效果是未定义的。”@H2CO3,根据编译器使用的参数传递约定,它可能会把事情搞砸。我有理由相信,即使它起作用,也是未定义的行为。@H2CO3[expr.reinterpret.cast]/6:“通过指向与函数定义中使用的类型不同的函数类型(8.3.5)的指针调用函数的效果是未定义的。”