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

user2504694
  • 43
  • 1
  • 6
  • can you provide some code? – someone Jul 23 '13 at 06:49
  • see this may be helpful http://stackoverflow.com/questions/2156353/how-do-you-query-a-pthread-to-see-if-it-is-still-running – Jeyaram Jul 23 '13 at 06:56
  • 1
    Why couldn't the server thread abort the program itself? – jxh Jul 23 '13 at 06:57
  • Yeah - like @jlx says, just call your 'TerminateProcess' API, or whatever it's called on your OS, from the thread that detected the critical error. – Martin James Jul 23 '13 at 09:46
  • My main program is written in Fortran. At some point it calls a c function that starts the server (coupling via the iso_c_binding). I guess the best solution really would be to write some program exit function in the Fortran main program that I call in case there is an error in the server function (thanks jxh and Martin James). I know that it is always better to provide some pseudo code but I think it doesnt really make a lot of sense here. At least I have no idea what to post. – user2504694 Jul 23 '13 at 14:29
  • I just thought about how I would implement it and I see one issue that might arise. Following situation: There is an error in the server thread, I catch this error and the server thread calls the program exit function of the main program. What happens to the server thread then? For me it sounds like I dont get a clean and tidy exit there (the main program ends before the server thread actually has the chance to exit). Or does the main program exit also take care of a clean server thread exit? – user2504694 Jul 23 '13 at 14:34
  • @user2504694 The 'program exit' function is not bound to the 'main thread' - if your server thread calls it, it never returns because the OS terminates the thread within the call. No matter what thread of your process calls system.exit, or whatever, the effect is the same - the OS enforces the 'immediate' stop of all threads within the process of the calling thread, (yes, the OS can do that even if some process threads are running on another core), and then releases all user and OS resources, so making your process non-existent. – Martin James Jul 23 '13 at 18:55
  • Ok I understand but this still sounds like a harsh method. I think my whole situation boils down to this: Two parallel running threads that end themselves and the other thread in case of an error (and this as clean as possible). Ending the thread with the error is not an issue. I can just return from the function. But telling the other thread that the server thread is gone seems to be a problem. Because of the structure of the thread I cant just constantly check whether the server thread is gone or not. I guess I just call a program exit function now from the server thread for a harsh exit. – user2504694 Jul 25 '13 at 06:23

1 Answers1

0

You can use pthread_join to wait for termination of the child thread in the parent thread. Or pthread_cond_wait to implement a similar stuff.

vinayak
  • 86
  • 2
  • Hello vinayak, I think this doesnt work as I dont want the parent thread to wait for the child thread. I just want it to stop in case there is an error in the child thread. – user2504694 Jul 23 '13 at 14:32
  • @user2504694 then just return from the function on which the child thread is called as soon as you encounter an error. – Saksham Jul 24 '13 at 07:42