Is it possible to kill a spinning thread?

前端 未结 9 1496
遥遥无期
遥遥无期 2021-01-20 19:09

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         


        
9条回答
  •  无人共我
    2021-01-20 19:35

    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] .

提交回复
热议问题