user16937
user16937

Reputation: 21

Trying to create posix thread and get invalid conversion from 'void*' to 'void* (__attribute__((__cdecl__)) *)(void*) error

Sorry if I'm not as descriptive as I should be, I'm nearly asleep as I type this. I am trying to create a posix thread in my c++ code compiling with g++ on mingw. Here's the except of the code I'm trying to compile

static void processNextNodeOnQueue(queue<TreeNode*> &toComputeQueue) {...}

void static processNodes(void* ptr) {
    pair<queue<TreeNode*>*, bool*> *input = (pair<queue<TreeNode*>*, bool*>*) ptr;
    while(*(input->second)) {
        pthread_mutex_lock(&mutex1);
        if(input->first->empty()) return;
        pthread_mutex_unlock(&mutex1);
        processNextNodeOnQueue(*(input->first));
    }   
}

void startThinking() {
    thinking = true;
    mutex1 = PTHREAD_MUTEX_INITIALIZER;
    pthread_t thread;
    pair<queue<TreeNode*>*, bool*> *input = new pair<queue<TreeNode*>*, bool*>(&toComputeQueue, &thinking);
    int thereadId = pthread_create(&thread, NULL, (void*)&processNodes, (void*) input );
    delete input;
}

void stopThinking() {
    //set thinking to false and wait for threads to wrap up
    thinking = false;
}

void processForTime(double seconds) {
    clock_t t;
    int f;
    t = clock();
    startThinking();
    //something to wait for an amount of time
    stopThinking();
}

Here's the full compiler output in case I missed something

C:\Users\Maxwell\SkyDrive\RPI\Fall 2013\IHSS-1964\Connect 4\MaxAI>g++ -g -pthread -std=c++11 -o3 *.cpp -o Max.exe
In file included from unit_tests.h:5:0,
                 from main.cpp:11:
search_tree.h: In member function 'void BoardSearchTree::startThinking()':
search_tree.h:221:85: error: invalid conversion from 'void*' to 'void* (__attribute__((__cdecl__)) *)(void*)' [-fpermissive]
   int thereadId = pthread_create(&thread, NULL, (void*)&processNodes, (void*) input );
                                                                                     ^
In file included from search_tree.h:12:0,
                 from unit_tests.h:5,
                 from main.cpp:11:
c:\mingw\include\pthread.h:940:31: error:   initializing argument 3 of 'int pthread_create(pthread_t*, pthread_attr_t_*const*, void* (__attribute__((__cdecl__)) *)(void*), void*)' [-fpermissive] PTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid,

Upvotes: 2

Views: 1753

Answers (2)

Austin Phillips
Austin Phillips

Reputation: 15776

Assuming you're using the pthread-w32 library from here, then the following header information is relevant:

#define PTW32_CDECL __cdecl

PTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid,
                            const pthread_attr_t * attr,
                            void *(PTW32_CDECL *start) (void *),
                            void *arg);

What this shows is that the start parameter should be a function pointer that uses the __cdecl calling convention.

Try changing your processNodes function prototype to the following:

static void PTW32_CDECL processNodes(void* ptr)

The compiler should then create your thread function with the correct calling convention as expected by pthread_create.

Upvotes: 1

uk4321
uk4321

Reputation: 1078

The third parameter to pthread_create is a void *(*start_routine) (void *) - a function pointer. You are casting the function to a (void*) which is both unnecessary and incorrect. Just remove the cast:

pthread_create(&thread, NULL, processNodes, (void*) input );

Also since input is a pointer you don't need to cast it either, all pointers (save pointers-to-members) are implicitly convertible to void*):

pthread_create(&thread, NULL, processNodes, input );

Upvotes: 1

Related Questions