ant2009
ant2009

Reputation: 22486

multithreaded using _beginthreadex passing parameters

Visual Studio C++ 2008

I am using threads. However, I have this warnings and not sure what I am doing wrong.

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;
}

Upvotes: 0

Views: 1529

Answers (1)

John Knoeller
John Knoeller

Reputation: 34148

the Thread function that you pass to _beginthreadex has a different prototype than the one you pass to _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 
);

It's the same as what CreateThread expects,

DWORD WINAPI ThreadProc(
  __in  LPVOID lpParameter
);

So you need to change the function signature of your thread proc to

unsigned WINAPI get_info(void *arg_list)

remove WINAPI and change the return type.

Edit :

WINAPI is actually needed, the docs show the wrong prototype for _beginthredex, but they explicitly state that __stdcall is needed. Your problem is just the return type. Also, the error message, says that __stdcall is expected, so that settles it.

Upvotes: 1

Related Questions