How to kill uncompleted/ progressed thread? - multithreading

I created a thread from main and detach it from main thread.
I want to kill this thread when it is in progress state.
I tried std::terminate() But it kill whole process . This thing i didn't want.
I want to kill that progress thread and start another thread in C++.
Please help me to solve this for std::thread.

Related

Is there any way to let main thread do its own task and not waiting for its sub thread?

I'm doing a project on Linux recently. I want my main thread to create subthreads and then go back to its own work. And when a subthread terminates, the main thread join the subthread. It's something like calling wait_pid() in SIG_CHLD handler when using process.
Is there any way to implement this in thread?

Can gdb pause current thread but let other threads keep running?

When I attach to a process, or when a breakpoint is hit, all the threads are paused. I can't find out how to pause one thread, and let other threads continue running.
I can't find out how to pause one thread, and let other threads continue running.
Besides the non-stop mode mentioned in this answer, and scheduler locking mentioned here, you could do this:
# arrange for GDB to stop in the thread you want to suspend.
(gdb) call sleep(1000) # current thread will sleep, all others will continue running

Detaching thread in C++11

I want to start a thread from a process and detach it and terminate the process. But the thread will be running continuously in the background. Can I achieve this with c++11 ?
I have detached my thread like this
std::thread(&thread_func, param1, param2).detach();
But it gets terminated once the process is terminated.
Detaching is not the same as running in the background. If you detach a thread then you simply tell the OS "I don't want to join the thread manually after it exits, please take care of that for me". But the OS will usually kill all child threads/processes when the main process exits.
So what you want is to run a deamon. However turning a process into a deamon (note that you can't daemonize a thread) is OS dependent. On linux you would call daemon function:
http://man7.org/linux/man-pages/man3/daemon.3.html
I don't know how to do that on Windows or other OS. Also you may want to read this:
Creating a daemon in Linux

How to kill the thread created by tensorflow.train.SummaryWriter

I have a quick question. I noticed that each time tensorflow.train.SummaryWriter is called, a new thread is created. Calling close() method of summarywriter will not kill the thread.
I am wondering if there is a way to kill the threads created by summarywriters?
Thank you for your help!
Best

Use cases for detached threads

In case of detached threads, if the main thread finishes executing before the detached thread, this would terminate the process killing all the threads. pthread_join() doesn't work in for detached threads.
So in what scenarios are detached threads used because I should be sure that detached thread has finished execution before terminating the process?
Why do you care whether the thread has finished execution? What you care about is whether any work that you need done has been done. If you use some other way to track what work is done, you don't need to wait for the thread to finish execution.

Resources