views:

34

answers:

2

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

+1  A: 

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.

Cătălin Pitiș
i have pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 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?
+2  A: 

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.

Chris Becke