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?

5 Years
Discussion Span
Last Post by L7Sqr

Threads are particularly tricky for people who have not programmed them before. If all you want is blocking behavior just encode it in your design. FOr instance

int MoveToHQ () { ... }
EmptyVehicle () { ... }
GoToResource () { ... }
StartCollecting () { ... }

Then, in the driver for the program, just call them sequentially.

MoveToHQ ();
EmptyVehicle ();
GoToResource ();
StartCollecting ();

If there is a separate thread controlling each of those functions I'd suggest that you have a design flaw. However, other than the top level description it is really hard to tell what you may or may not be doing wrong.


It can't be done. The game is real-time so we need to use threads. Secondly all the methods are already done, I don't have the time to rewrite all my code just for one extra functionality.


Using threads is not a fault. Using them incorrectly is. If you implement functions as threads then you need to synchronize; fail to synchronize and you get spurious results.
If you dont have the time to refactor a poor design then you will be forced to deal with that design (as a time sink) as long as it is used.

I'm not in a position to critique your work - I haven't seen any of it. However, if you are having trouble synchronizing your execution then I'd suggest you stop the press until you understand your environment. Going 'live' with something you dont understand is a recipe for disaster.

My personal opinion, of course. Feel free to disregard as you see fit.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.