Ang*_*ber 0 c++ function-pointers
我试图使用一个线程库,它具有如下定义的线程启动函数:
int vg_thread_create(VGThreadFunction func,
void *arg,
VGThreadDescriptor *tid,
void *stack,
size_t stack_size,
long priority,
int flags );
Run Code Online (Sandbox Code Playgroud)
VGThreadFunction是以下typedef:
typedef void* (*VGThreadFunction) ( void* )
Run Code Online (Sandbox Code Playgroud)
为了论证,我有一个这样的函数:
void doit() {
cout << "Woohoo printing stuff in new thread\n";
}
Run Code Online (Sandbox Code Playgroud)
这个函数的签名不是void*func(void*) - 我是否被迫使用这样的函数签名?
如果我尝试:
VGThreadFunction testfunc = &doit;
Run Code Online (Sandbox Code Playgroud)
我明白了
错误C2440:'初始化':无法从'void(__ cdecl*)(void)'转换为'VGThreadFunction'
我如何使用vg_thread_create?
编辑
我试过这个:
void* doit(void* donothingvar) {
cout << "printing stuff in new thread\n";
return donothingvar;
}
Run Code Online (Sandbox Code Playgroud)
然后在一个函数中:
VGThreadFunction testfunc = (VGThreadFunction)doit;
int ret = vg_thread_create(testfunc, 0, 0, 0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
然后我在调用vg_thread_create时遇到访问冲突.
如果我进入该函数,它实际上会在调用_beginthreadex时崩溃
Unhandled exception at 0x0040f5d3 in threadtest.exe: 0xC0000005: Access violation writing location 0x00000000.
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
通过单步执行函数,我得出了一个有效的指针传递 - 传递零导致空取消引用.对不起,有点具体.
是的,函数签名必须匹配.只需在您的函数上放置一个虚拟参数.