如何停止执行已创建的线程? [英] How to stop a thread from executing that is already created?

查看:74
本文介绍了如何停止执行已创建的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

1.)具有长度为4的布尔数组.
2.)我在函数1中创建了一个线程来执行函数3
3.)我有一个函数2删除布尔数组

如果删除布尔数组,如何停止执行function3的任何现有线程?

Scenario:

1.)Have a boolean array of length 4.
2.)I have a thread that is created in function 1 to execute function 3
3.)I have a function 2 that deletes the boolean array

how do i stop any existing threads from executing function3 if boolean array is deleted?

public:
function 1
function 2
function 3
bool bArray[4];
Handle mainHandle;


function1()//called for every one second until DestroyTimer() is called
{
mainHandle = CreateThread(NULL, 0, ::function3, this, 0, NULL);
}

function3()
{
bArray[2] = false;
}

function2()
{
DestroyTimer();
bArray = NULL;
//here comes the problem, event though i called destroy timer there are threads that are already created and when they execute function3 they throw an assertion that bArray is NULL.
}



因此要解决这个问题.在删除bArray之前,我要终止或暂停任何现有线程,我需要对该线程进行处理.我如何暂停或停止执行线程?



So to solve this. Before deleting bArray i want to terminate or suspend any existing threads, i have the Handle to the thread. how do i suspend or stop the thread from executing?

推荐答案

如果要阻止对function3执行某些无效数据,则可能必须标记数据以某种方式为无效,并检查function3内部的有效性.或者在启动线程之前在function1中执行此操作.我不明白您想做什么,但我认为您需要使用标准的同步对象.
If you want to prevent function3 to be executed with some invalid data, then you probably have to mark your data as invalid in some way, and to check inside function3 for validity. Or do it in function1 before starting thread. I don''t understand what you would like to do, but I think you need to use standard synchronization objects.


通常,人们使用一种机制来通知每个线程何时停止.例如,通过使用线程定期检查的bool变量或使用标准同步工具以某种更精细的方式执行.通常,这涉及定期在线程内部检查创建者是否希望您终止.

一旦有了这样的机制,您就可以为所有线程设置信号,等待它们终止,然后就可以销毁线程正在访问的内容.
Typically, one uses a mechanism to signal to each thread when it should stop executing, for example by a bool variable that the thread regularly checks or some more refined way using the standard synchronization tools. That usually involves checking regularly inside your thread whether your creator wants you to terminate.

Once you have such a mechanism in place, you simply set the signal for all of your threads, wait until they have terminated, and then you can start destroying things that are being accessed by your threads.


我认为您的概念是完全错误的或非常不寻常的,或者您没有很好地解释它.

通常,您不创建运行一行代码并退出的线程.

线程函数通常如下所示:

I think your concept is either plain wrong or really unusual, or you did not explain it very well.

You normally do not create a thread that runs one line of code and exits.

The thread function normally would look like:

function3()
{
  while (!bExitThread)
  {
      doSomething();
  }
};



您可以对此进行一些修改:



You could modify this a bit:

function3()
{
  while (!bExitThread)
  {
      if (!bSuspended)
      {
          doSomething();
      }
      else
      {
          Sleep(100); // avoid busy waiting
      }
  }
}



现在您可以:
1)通过将bExitThread设置为TRUE
使线程干净退出 2)通过将bSuspended设置为TRUE来使线程被挂起",而不会破坏底层的OS线程(您可以通过将bSuspended设置为FALSE来再次恢复它)



Now you can:
1) Make thread exit cleanly by setting bExitThread to TRUE
2) Make thread "suspended" by setting bSuspended to TRUE without destroying the underlying OS thread (and you can resume it again by setting bSuspended to FALSE)


这篇关于如何停止执行已创建的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆