Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancelling a thread running a long operation

I'm trying to work out a design predicament I have.

ClassWithLongOperation
{
    Run()
    {
        RecrusiveOperation();
    }

    RecrusiveOperation()
    {
        /* RECURSION */
    }
}

MyThread
{
    ClassWithLongOperation Op1(10);
    Op1.Run();  // Takes several minutes.

    ClassWithLongOperation Op2(20);
    Op2.Run();

    SomeOtherClassWithLongOperation Op3;
    Op3.Run();

    // Do some other stuff
}

The GUI starts MyThread, which runs for a good 5-6 minutes. I want to be able to have a big fat Cancel button on my GUI, so the user can cancel the operation.

I could create a global boolean variable bCancelled, and check if its been set in RecursiveOperation, but I want to be a good C++ & OO programmer and avoid global variables. Especially if they would have to spread across multiple files.

So how would I (following good design) safely cancel MyThread? What could I change in my setup to allow this?

I'm also using _beginthreadex to start the thread, but I could use boost if it would allow for an easier solution.

like image 492
Josh Avatar asked Sep 06 '12 12:09

Josh


People also ask

What happens when a thread is Cancelled?

Thread cancellation allows a thread to terminate the execution of any other thread in the process. The target thread (the one being cancelled) can keep cancellation requests pending and can perform application-specific cleanup when it acts upon the cancellation notice.

How do you handle thread cancellation?

Thread cancellation lets a thread terminate the execution of any other thread in the process. When a cancellation requested is acted on, the target thread (the thread being cancelled) is allowed to defer cancellation requests and to perform application-specific cleanup processing.

What are the two scenarios for canceling a target thread?

Cancellation of a target thread may occur in two different scenarios: Asynchronous cancellation: One thread immediately terminates the target thread. Deferred cancellation: The target thread periodically checks whether it should terminate, allowing it an opportunity to terminate itself in an orderly fashion.

Can we cancel thread?

Thread class does not offer built-in support for cancellation tokens, you can pass a token to a thread procedure by using the Thread constructor that takes a ParameterizedThreadStart delegate. The following example demonstrates how to do this. using System; using System.


1 Answers

Your flag not need to be global to your entire program, but it needs to be visible to your class code. Create the flag to be a private instance member and a public function to change it to false/true. In your recursive function, test its value to verify if the task should continue. When you want, set its value to false (through the function of course) to stop the recursive calls, i.e., when the user clicks the button you call the function in the desired instance. This way you will not break any OO principle, since you have a private flag and a public member function to safely change it.

like image 173
davidbuzatto Avatar answered Nov 03 '22 00:11

davidbuzatto