Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing a pthread waiting on a condition variable

Tags:

c++

pthreads

I have a pthread waiting on a condition variable using pthread_cond_wait(). It's waiting for data from a queue structure that is filled by another thread. I want to kill this thread, preferably without pthread_kill().

On Linux and WinPthreads doing a

pthread_cancel();
pthread_join()

is enough to kill it.

However, on OS X it hangs on the pthread_join() call. Any suggestions?

like image 321
CarbonAsh Avatar asked Aug 20 '10 04:08

CarbonAsh


People also ask

How do you kill pthreads?

When the thread must terminate, execute pthread_cancel() and wait for its termination with pthread_join() .

Does condition variable wait release lock?

std::condition_variable::wait 1) Atomically unlocks lock , blocks the current executing thread, and adds it to the list of threads waiting on *this. The thread will be unblocked when notify_all() or notify_one() is executed. It may also be unblocked spuriously.

Does Pthread cond wait unlock mutex?

The pthread_cond_wait() function atomically unlocks mutex and performs the wait for the condition. In this case, atomically means with respect to the mutex and the condition variable and another threads access to those objects through the pthread condition variable interfaces.

Can multiple threads wait on the same condition variable?

If multiple threads are waiting on the same condition variable, the scheduler can select any of those threads to be awakened (assuming that all threads have the same priority level). The cnd_broadcast() function unblocks all of the threads that are blocked on the specified condition variable at the time of the call.


2 Answers

Do you have access to the queue, and control of the object schema for enqueued items? If so, define a queue object type that when de-queued, instructs the thread that is processing the item to exit gracefully.

Now, to shut down these threads, simply post a number of these "quit" objects to the HEAD of the queue that corresponds to the number of threads that are servicing the queue, and join on the threads.

This seems much cleaner than the "nuclear option" of pthread_cancel/kill.

like image 101
Bukes Avatar answered Sep 23 '22 01:09

Bukes


pthread_cancel should wake a thread that is blocked in pthread_cond_wait --- this is one of the required cancellation points. If it doesn't work then something is wrong.

The first thing to check is that cancellation is indeed enabled on the target thread --- explicitly call pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate) on the target thread to make sure. If that doesn't work, then cancellation is broken on your platform and you'll have to resort to alternatives such as setting a "please stop now" flag and signalling the condition variable.

Do not use asynchronous cancellation unless you really know what you are doing --- it can trigger the cancellation in the middle of any operation (e.g. in the middle of setting up a function call stack frame or running a destructor), and thus can leave your code in a thoroughly inconsistent state. Writing async-cancel-safe code is hard.

Incidentally pthread_kill does not kill a thread --- it sends a signal to it.

like image 44
Anthony Williams Avatar answered Sep 23 '22 01:09

Anthony Williams