使用_beginthreadex传递参数的多线程

时间:2010-03-06 20:15:42

标签: multithreading winapi

Visual Studio C ++ 2008

我正在使用线程。但是,我有这个警告,不知道我做错了什么。

1. unsigned int (__stdcall *)(void *) differs in levels of indirection from 'void *(__stdcall *)(void *)'
2. _beginthreadex different types for formal and actual parameters 3

/* Function prototype */
void* WINAPI get_info(void *arg_list)

DWORD thread_hd;
DWORD thread_id;

/* Value to pass to the function */
int th_sock_desc = 0;

/* Create thread */
thread_hd = _beginthreadex(NULL, 0, get_info, (void*) th_sock_desc, 0, &thread_id);
if(thread_hd == 0)
{
    /* Failed to create thread */
    fprintf(stderr, "[ %s ] [ %s ] [ %d ]\n", strerror(errno), __func__, __LINE__);
    return 1;
}

1 个答案:

答案 0 :(得分:1)

传递给_beginthreadex的Thread函数的原型与传递给_beginthread

的原型不同
uintptr_t _beginthread( 
   void( *start_address )( void * ),
   unsigned stack_size,
   void *arglist 
);
uintptr_t _beginthreadex( 
   void *security,
   unsigned stack_size,
   unsigned ( *start_address )( void * ),
   void *arglist,
   unsigned initflag,
   unsigned *thrdaddr 
);

CreateThread期望的内容相同,

DWORD WINAPI ThreadProc(
  __in  LPVOID lpParameter
);

因此您需要将线程proc的函数签名更改为

unsigned WINAPI get_info(void *arg_list)

删除WINAPI更改返回类型。

编辑:

实际上需要WINAPI,文档显示_beginthredex的错误原型,但他们明确声明需要__stdcall。你的问题只是返回类型。此外,错误消息表示__stdcall是预期的,因此可以解决它。