Hi there. I've just written an extremely simple cross-platform wrapper for posix threads. I compiled it on unix and it compiles with no errors. The windows side... not so pretty. I'm using MinGW.

gcc compiles fine but throws a couple of warnings at me (I'll explain in a bit.) g++ gives me errors.

I'm compiling with '-Wall -ansi -g' switches.

The error that I'm getting is related to the _beginthreadex function. In my header file I've defined this:

#ifdef _WIN32
#define THREAD_PROC unsigned (*)(void*)
#define THREAD_PROC void* (*)(void*)

THREAD_T InitiateDetachedThread(THREAD_PROC, void*);

And in my .c file is:

#ifdef _WIN32
THREAD_T InitiateDetachedThread(unsigned (*start)(void*), void* a) {
    return (THREAD_T)_beginthreadex(NULL, 0, start, a, 0, NULL); 
THREAD_T InitiateDetachedThread(void* (*start)(void*), void* a) {
    THREAD_T t;
    pthread_create(&t, NULL, start, a);
    return t;
#endif /*_WIN32*/

Like I said, the unix side compiles with no complaints. When i compile with gcc on MinGW I get:

$ gcc -Wall -ansi -g -c threads.c
threads.c: In function `InitiateDetachedThread':
threads.c:16: warning: passing arg 3 of `_beginthreadex' from incompatible pointer type

And g++ gives me the full-blown error:

threads.c: In function `void* InitiateDetachedThread(unsigned int (*)(void*), void*)':
threads.c:16: error: invalid conversion from `unsigned int (*)(void*)' to `unsigned int (*)(void*)'
threads.c:16: error:   initializing argument 3 of `long unsigned int _beginthreadex(void*, unsigned int, unsigned int (*)(void*), void*, unsigned int, unsigned int*)'

I don't get why it won't cast... it shouldn't have to! It's like the compiler's saying "Can't cast from int to int."

Never mind. I solved it. For anyone else who gets similar errors when dealing with function pointers; check the calling convention. As it turned out, _beginthreadex must be given a function pointer with the __stdcall calling convention.


YOU ARE MY HERO. I did this once but forgot how I solved it, 2nd round I'm struggling for 2 hours! THANK YOU.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.