[Quickfix-developers] Confused by possible bug in Utility.cpp / thread_spawn
Brought to you by:
orenmnero
|
From: K. F. <kfr...@gm...> - 2013-01-10 17:36:28
|
Hello List!
Background: I am trying to build QuickFIX with mingw-w64. To do this,
I am starting with the windows "version" of the code, i.e.,the code
paths that are active when _MSC_VER is defined.
I am confused by what looks to me like a bug, but I probably don't
understand it correctly. If the bug I'm imagining were actually
present, it would seem that QuickFIX would neither compile nor
run on windows.
Specifically the function thread_spawn is defined in Utility.cpp.
The function begins:
bool thread_spawn( THREAD_START_ROUTINE func, void* var, thread_id& thread )
In the windows section is the line:
result = _beginthreadex( NULL, 0, &func, var, 0, &id );
It appears to me that the '&' in "&func" is an error, and shouldn't
be there.
I reason as follows:
THREAD_START_ROUTINE is a typedef in Utility.h. Its windows version is:
typedef unsigned int (_stdcall THREAD_START_ROUTINE)(void *);
I read this as a pointer to a function that takes a void* and returns
an unsigned.
Compare with the unix version:
extern "C" { typedef void * (THREAD_START_ROUTINE)(void *); }
which I also read as a pointer to a function, but now the function
returns a void*.
Because of the '&' in "&func", a pointer to a pointer to a function
is being passed to _beginthreadex, rather than pointer to a function.
(recall: result = _beginthreadex( NULL, 0, &func, var, 0, &id );)
>From msdn, _beginthreadex has the prototype:
uintptr_t _beginthreadex(
void *security,
unsigned stack_size,
unsigned ( *start_address )( void * ),
void *arglist,
unsigned initflag,
unsigned *thrdaddr
);
which I read as _beginthreadex expecting a pointer to a function.
Compare with the unix version. From Utility.cpp:
if( pthread_create( &result, 0, func, var ) != 0 ) return false;
Here there is no '&' on func, so pthread_create is being passed a
pointer to a function (as expected).
>From the pthreads documentation, pthread_create has the prototype:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void*), void *arg);
which I read as taking a pointer to a function that takes a void*
and returns a void*.
So it looks to me that the unix version is correct and there is
no type mismatch in the function call to pthread_create, but in
the windows version there is a type mismatch in the corresponding
call to _beginthreadex, with a pointer-to-function being expected,
but a pointer-to-pointer-to-function being passed, with the extra
"pointer-to" coming from the '&' in "&func".
Of course, the problem with my interpretation is that the windows
version shouldn't compile, and if one were to shut the compiler
up by casting away the type mismatch, the program should crash.
My QuickFIX source is from quickfix-1.13.3.zip, downloaded on
Jan. 2.
(I have been tweaking the code to get it to build with mingw-w64,
and after tweaking that doesn't change this part of the code,
mingw-w64 does give the type-mismatch error described.)
Any help understanding what is going on here would be appreciated.
K. Frank
|