Take the 2-minute tour ×

what will be alternative of pthread_setcanceltype in windows thread programming in c++?

share|improve this question

2 Answers 2

Windows threads do not have cancellation points, so theres no system cancel type to consider.

As such, "canceling" a thread on windows means that you, the developer, needs to come up with a strategy for telling a thread to exit. If it is a GUI thread, you can post it a WM_QUIT message. If it is a non GUI thread, then it really depends on what the thread is doing. You need to analyse the thread and see if there is a point where your code can explicitly check if it needs to keep going, or exit.

There is a pthreads-win32 implementation available if you'd rather avoid the question and get pthreads complaint behaviors on Win32.

share|improve this answer
A number of the blocking system calls in Windows have alertable states, including WaitForSingleObject, WaitEx, etc., which is analogous to the cancellation points in various pthread functions. You can use something like QueueUserAPC msdn.microsoft.com/en-us/library/ms684954%28VS.85%29.aspx to do the analogue of pthread_cancel with a cancel type of PTHREAD_CANCEL_DEFERRED. For a PTHREAD_CANCEL_ASYNCHRONOUS, you can take a look at the Win32 implementation of pthread_cancel at locklessinc.com/articles/pthreads_on_windows but read the disclaimer about that there. –  Display Name Jul 5 '11 at 23:36
I meant WaitForSingleObjectEx –  Display Name Jul 5 '11 at 23:52

You could use events as synchronization objects. Check in your thread, from time to time the status of event (WaitForSingleObject with zero timeout), and if it is signaled, return from the thread's main function. To cancel the thread from outside, just set the event.

share|improve this answer
i have pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &OldType); there after i do something in critical section with other thread and after that i excute pthread_setcanceltype(OldType, NULL); how i do this in windows? –  Suri May 14 '10 at 15:09

Your Answer


