Suri
Suri

Reputation: 3357

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

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

Upvotes: 2

Views: 602

Answers (2)

Chris Becke
Chris Becke

Reputation: 36101

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.

Upvotes: 2

Cătălin Pitiș
Cătălin Pitiș

Reputation: 14317

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.

Upvotes: 1

Related Questions