About gthread cancellation



Hi all,

I searched through the archives and found that glib does not have an equivalent of pthread_cancel because the developers decided not to have it. I agree that pthread_cancel can be tricky, but I have gone through a lot of work to build a server application using glib code, and now I am stuck because of the lack of a cancellation function. My code is very simple (standard server pseudo-code):

while(1) {
	accept(); // man 2 accept
	g_thread_create(listen_and_reply);
}

The point now is that I want to exit the loop upon some condition (e.g. SIGTERM caught) so my application can exit gracefully (cleaning memory and, the more important, closing databases to avoid corruption).
If I had a g_thread_cancel call, I could do something like this:

while(1) {
	accept_thread = g_thread_create(accept);
        cancel_accept = g_thread_create(cancel_accept_upon_condition);
	g_thread_join(accept);
	g_thread_cancel(cancel_accept);
}

The function cancel_accept_upon_condition would call g_thread_cancel to cancel accept_thread upon the observation of some condition (GCond). And, just to avoid a race condition, the accept_thread should call set_cancelstate(CANCEL_DISABLE) just before exiting.

My doubt is: if I don't have g_thread_cancel, how can I implement this simple server loop? The only other solution I can see is to use no-blocking mode and select() with timeouts, which is rather ugly and not desired if I do use threads. Any tips?

Thanks in advance,
Davi de Catro Reis




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]