Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I kill a pThread in C++, do destructors of objects on stacks get called?

I'm writing a multi-threaded C++ program. I plan on killing threads. However, I am also using a ref-counted GC. I'm wondering if stack allocated objects get destructed when a thread gets killed.

like image 284
anon Avatar asked Jan 29 '10 15:01

anon


People also ask

How to terminate a pthread in C?

Use a cancellation point: The thread terminates whenever a cancellation function is executed. When the thread must terminate, execute pthread_cancel() and wait for its termination with pthread_join() .

What does pthread_ create return?

pthread_create() returns zero when the call completes successfully. Any other return value indicates that an error occurred. When any of the following conditions are detected, pthread_create() fails and returns the corresponding value.

What is pthread_ t tid?

In main() we declare a variable called thread_id , which is of type pthread_t . This is basically an integer used to identify the thread in the system. After declaring thread_id , we call the pthread_create function to create a real, living thread.


1 Answers

The stack does not unwind when you 'kill' a thread.

Killing threads is not a robust way to operate - resources they have open, such as files, remain open until the process closes. Furthermore, if they hold open any locks at the time you close them, the lock likely remains locked. Remember, you are likely calling a lot of platform code you do not control and you can't always see these things.

The graceful robust way to close a thread is to interrupt it - typically it will poll to see if it's been told to close down periodically, or it's running a message loop and you send it a quit message.

like image 170
Will Avatar answered Nov 05 '22 18:11

Will