I am using ZThreads to illustrate the question but my question applies to PThreads, Boost Threads and other such threading libraries in C++.
class MyClass: p
Yes,
Define keepAlive
variable as an int
.
Initially set the value of keepAlive=1
.
class MyClass: public Runnable
{
public:
void run()
{
while(keepAlive)
{
}
}
}
Now, when every you want to kill thread just set the value of keepAlive=0
.
Q. How this works ?
A. Thread will be live until the execution of the function continuous . So it's pretty simple to Terminate
a function . set the value of variable to 0
& it breaks which results in killing of thread . [This is the safest way I found till date
] .