How to start a new thread for a procedure for a member object
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(&shipGeneratorT开发者_如何学Pythonhread, 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?
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;
}
You need "static void* generateShips(void*)" and launch with "pthread_create(&shipGeneratorThread, NULL, Port::generateShips, NULL); ".
Regards.
/* 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);
}
精彩评论