views:

245

answers:

1

Hello,

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;
}
+1  A: 

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.

John Knoeller
I was thinking I would need WINAPI as that supports the __stdcall. As _beginthreadex expects that. thanks.
robUK
WINAPI is __stdcall. but _beginthreadex expects __cdecl. CreateThread expects __stdcall.
John Knoeller
_beginthread expects a __cdecl, in _beginthreadex the prototype must be a __stdcall (or __clrcall), so WINAPI is fine.
S.Skov
No. it isn't. Please note that I posted the declaration of `_beginthreadex`. Do you see any `WINAPI` or `__stdcall` in there?
John Knoeller
http://msdn.microsoft.com/en-us/library/kdzttdcb(VS.80).aspx
John Knoeller
start_addressStart address of a routine that begins execution of a new thread. For _beginthread, the calling convention is either __cdecl or __clrcall; for _beginthreadex, it is either __stdcall or __clrcall.Read the docs.
S.Skov
@S.Skov: You're right. It does need __stdcall. The comments say so even though the prototype doesn't. I've updated my answer.
John Knoeller