1

my main program (all c code) starts a server function as a separate thread (pthread_create). This function waits for incoming input and does certain things with it. Meanwhile the main program keeps doing things as well (the main program does not wait for the server function to finish its job before it continues).

Now there is the chance that an error occurs in the server function (socket problem, ...). The server function then returns a certain value (for example "-1"). If that is the case I want to abort the execution of the whole program.

How can I handle this?

My only idea is to start a second thread that constantly verifies a global variable that the server thread changes in case of an error. Then the second thread handles the program abortion.

What other options do I have?

Kind regards

4

1 回答 1

0

您可以使用 pthread_join 来等待父线程中的子线程终止。或者 pthread_cond_wait 来实现类似的东西。

于 2013-07-23T07:02:15.247 回答