views:

42

answers:

3

I am trying to start a method from my main() as a new thread with pthread:

    int main(int argc, char** argv) {


    pthread_t shipGeneratorThread;

    Port portMelbourne;

    pthread_create(&shipGeneratorThread, NULL, portMelbourne.generateShips(), NULL);



    return (EXIT_SUCCESS);
}

The Port class has a function that generates a ship:

void Port::generateShips() { //Generate 1 cargo ship every 2.5 hours bool stop = false;

while(!stop) {
    if(numberOfShipsInBay < 20) {
        Ship ship;
        ship.setTicket(numberOfShipsInBay);
        shipsWaitingToDock[numberOfShipsInBay] = ship;
        term.displayMessage("A new ship has entered the port");
        numberOfShipsInBay++;
    } else {
        term.displayMessage("A ship has been sent to another port");
    }
    usleep(FIVE_MINUTES * 30); //2.5 hours simulated time
}
}

But the compiler gives me an error, "invalid use of void expression" for the pthread create function.

I am new to C++ and threading, any ideas?

+1  A: 

You need "static void* generateShips(void*)" and launch with "pthread_create(&shipGeneratorThread, NULL, Port::generateShips, NULL); ".

Regards.

Miguel Angel
If you need a Port instance, send Port instance as param, and into generateShips method do a cast from void* to Port*.Salu2.
Miguel Angel
I think you would need to drop the parentheses after `Port::generateShips`.
haggai_e
A: 
/* you need a wrapper function like this, it can be also a static method of some class */
void *GenerateShipsThread(void *port)
{
    /* do desired action */
    reinterpret_cast<Port*>(port)->generateShips();

    /* destroy Port object here or somewhere else */
    delete reinterpret_cast<Port*>(port);

    return NULL;
}

int main(int argc, char** argv) {
    pthread_t shipGeneratorThread;

    /* Port object is passed to the thread so you don't want it to be
     * destoyed when this function returns - create it on the heap */
    Port *portMelbourne = new Port();

    pthread_create(&shipGeneratorThread, NULL, GenerateShipsWrapper, portMelbourne);

    return (EXIT_SUCCESS);
}
adf88
This is not going to work at all because program can be terminated even before thread body starts ;).
Roman Nikitchenko
I wasn't fixing that problem, I was answering the question. Anyway, author gave minimal example, probably in real code main won't exit right after starting a thread, it would be senseless.
adf88
Fallowing your thinking we could also say "hey, it won't work, thee is no Port class!"
adf88
+1  A: 

Hi, you should use static method in this case and yes, look into man pthread_create. Signature of functiona is significant.

Also if you create thread way your code show it will be terminated as soon as main() exits. You need to wait for thread to accomplish. I put example below. It is not ideal but seems good enough for demonstration.

Please pay attention to static -> non-static method transition while handling thread start. It is common approach (although not the only one possible).

#include <stdio.h>
#include <pthread.h>

class Test {
    public:
        static void* Run(void* me) {
            static_cast<Test*>(me)->RunMe();
            return NULL;
        }

    private:
        void RunMe() {
            printf("%p: %p is running.\n", pthread_self(), this);
        }
};

int main() {

    pthread_t tid;
    Test test;

    printf("%p: creating thread for %p is running.\n", pthread_self(), &test);
    pthread_create(&tid, NULL, Test::Run, &test);
    printf("%p: waiting %p to finish.\n", pthread_self(), &test);

    // In real code you should check 'create' operation result.
    pthread_join(tid, NULL);
    printf("%p: OK, exiting\n", pthread_self());

    return 0;
}
Roman Nikitchenko