Hello, I need a way to wait for threads and I don't think that pthread_join works in my case. Here's the problem. I'm currently working on a very basic space game. We have vehicles that collect resources and must bring them back to the HQ. In order to do this I'm doing the following things:
1. Tell the vehicle to stop collecting (which also tells the resource that the vehicle isn't collecting on it).
2. Move the vehicle to HQ.
3. Empty the Vehicle.
4. Get back to the resource.
5. Start collecting resources again.
The problem I have is that everything happens super fast and not in the desired order. For example the vehicle begins to move to HQ even before it receives the command that tells him to stop collecting. Then while still traveling to HQ, it empties it resources, begins the journey back to the resource and starts collecting again. I need a way to wait for the actions to actually finish before doing the next. The problem is that I have no idea how to wait for threads. For example moving the vehicles calls several methods before calling a thread and then it will call it several times depending on how far is the resource. Secondly, sending a message goes to the client and to the server which both have a thread on their own.
I tried waiting, and using while(moving)/(collecting) but the lock the entire program which isn't what I need. Is there a way I can do the 5 steps I posted above but wait for the previous step to be over before beginning the next one?