Nazar Vozniy
Nazar Vozniy

Reputation: 51

How to close thread detach C++?

I launched thread as detach. How to close thread from main function?

void My()
{
   // actions  
}


void main()
{

    std::thread thr7(receive);
    thr7.detach();

    // close ?

}

Upvotes: 0

Views: 3931

Answers (3)

Davide Spataro
Davide Spataro

Reputation: 7482

Short Answer:

Use a shared variable to signal threads when to stop.

Long answer:

You cannot call join or terminate a thread directly from its parent once detach is called unless you use some other methods.

Take a look at the following code (over simple and not very meaninful) which should show a simple way of doing what you are asking:

#include <atomic>
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>

std::mutex mu;
std::condition_variable cv;
bool finished = false;

void threadFunc()
{
    while(!finished)
    {
        
        std:: cout << "Thread doing work \n";
        std::this_thread::sleep_for(std::chrono::milliseconds(5));
    }
    
    std::cout << "End of Thread \n";
}

int main()
{

    {
        std::thread t1(threadFunc);
        t1.detach(); // Call `detach` to prevent blocking this thread

    } // Need to call `join` or `detach` before `thread` goes out of scope

    for (int i = 0; i < 5; ++i){
        std::this_thread::sleep_for(std::chrono::milliseconds(20));
        std::cout << "Main doing stuff: \n";
    }
    std::cout << "Terminating the thread\n";

    std::unique_lock<std::mutex> lock(mu);  
    finished = true;
    cv.notify_all();
    std::cout << "End of Main\n";
    return 0;
}

You use a shared variable to tell the threads when to terminate its execution.

Upvotes: 3

Galik
Galik

Reputation: 48625

You can control the thread something like this:

std::atomic_bool running = false; // set to stop thread
std::atomic_bool closed = false; // set by thread to indicate it ended

void detached_thread_function()
{
    running = true;

    // acquire resources

    while(running)
    {
        std::cout << "running" << '\n';
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    // release resources

    // set after all resources released
    closed = true;
}

int main()
{
    std::thread(detached_thread_function).detach();

    std::this_thread::sleep_for(std::chrono::seconds(3));

    std::cout << "stopping detached thread" << '\n';

    running = false; // stop thread

    while(!closed) // you could code a timeout here 
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    // or use a condition variable?

    std::cout << "end program" << '\n';
}

The thread is signaled to end its function and the thread sets a flag to let the main function know it is safe to exit.

If you have multiple threads you can use an atomic counter to exit when it reaches zero.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409196

If I understand you correctly, you want to tell the thread to exit its infinite loop and exit?

Then a simple boolean std::atomic object is all that's needed.

You initialize it to some value (say true), and in the thread loop while it's that value. Once you want the thread to exit, you change the value (to false) and then next when the thread loop iterates it will notice that and break the loop and continue with cleanup and exit.

Upvotes: 3

Related Questions