1

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?

Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110
pharma_joe
  • 591
  • 2
  • 9
  • 24
  • possible duplicate of [cannot convert '\*void(MyClass::\*)(void\*) to void\*(\*)(void\*) in pthread\_create function](http://stackoverflow.com/questions/12006097/cannot-convert-voidmyclassvoid-to-voidvoid-in-pthread-create-fu) – Jonathan Wakely May 20 '13 at 15:12

3 Answers3

1

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

Regards.

Miguel Angel
  • 630
  • 7
  • 18
1

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
  • 12,800
  • 7
  • 74
  • 110
0
/* 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
  • 4,277
  • 1
  • 23
  • 21
  • This is not going to work at all because program can be terminated even before thread body starts ;). – Roman Nikitchenko Sep 02 '10 at 09:24
  • 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 Sep 02 '10 at 09:53
  • Fallowing your thinking we could also say "hey, it won't work, thee is no Port class!" – adf88 Sep 02 '10 at 10:02