title: How to stop a thread created with dispatch_async?
body: <p>I want to forcibly stop the thread created by <code>dispatch_async</code> if its in use for too much time, for example pass over 5 minutes. By searching over the internet, I got some one thought there was no way to stop the thread, does any one know that?</p>
  
  <p>In my imagine， I want to create a <code>NSTimer</code> to stop the thread when time specified passed.   </p>
  
  <pre><code>+ (void)stopThread:(NSTimer*)timer
  {
      forcibly stop the thread???
  }
  
  + (void)runScript:(NSString *)scriptFilePath
  {
      [NSTimer scheduledTimerWithTimeInterval:5*60 target:self selector:@selector(stopThread:) userInfo:nil repeats:NO];
  
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  
          [LuaBridge runLuaFile:scriptFilePath];
  
      });
  } 
  </code></pre>
  
  <p>My runLuaScript method:</p>
  
  <pre><code>+ (void)runLuaFile:(NSString *)filePath
  {
  
      lua_State *L = luaL_newstate();
      luaL_openlibs(L);
  
      int error2 = luaL_dofile(L, [filePath fileSystemRepresentation]);
      if (error2) {
          fprintf(stderr, "%s", lua_tostring(L, -1));
          lua_pop(L, 1);
      }
  
      lua_close(L);
  }
  </code></pre>
  
  <p>Dear @Martin R, should I use lstop like that, and when I want to stop the thread, just call <code>stopLuaRunning</code> method?</p>
  
  <pre><code>static lua_State *L = NULL;
  
  + (void)runLuaFile:(NSString *)filePath
  {
  
      L = luaL_newstate();
      luaL_openlibs(L);
  
      int error2 = luaL_dofile(L, [filePath fileSystemRepresentation]);
      if (error2) {
          fprintf(stderr, "%s", lua_tostring(L, -1));
          lua_pop(L, 1);
      }
  
      lua_close(L);
  }
  
  + (void)stopLuaRunning:(lua_State *L)
  {
      lua_sethook(L, NULL, 0, 0);
      luaL_error(L, "interrupted!");
  }
  </code></pre>