Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the thread be closed (pthread library)?

Tags:

c++

c

pthreads

I have some code, roughly:

pthread_create(thread_timeout, NULL, handleTimeOut, NULL);

void handleTimeOut()
{
  /*...*/
  pthread_cancel(thread_timeout);
  /*...*/
}

But as I noticed by pthread's manual the cancellation must be used by another threads. I have tried to use pthread_exit() function instead, but this thread hangs on again...

How must the tread termination be handled correctly? Will it be terminated successfully if the function handleTimeOut() just ends without special pthread functions?

like image 476
Rorschach Avatar asked Mar 08 '26 05:03

Rorschach


1 Answers

Killing a thread without its cooperation is a recipe for problems. The right solution will be one that allows an external thread to request the thread to clean up and terminate, and has the thread periodically example this state and when it's been requested, it follows through with the request. Such a request can be done through anything that all threads can share.

like image 137
mah Avatar answered Mar 10 '26 20:03

mah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!