简体   繁体   English

C ++ pthread - 如何取消线程?

[英]C++ pthread - How to cancel a thread?

I have a pthread that I created and now I want that in a specific time interval the thread execute some code. 我有一个我创建的pthread,现在我希望在特定的时间间隔内线程执行一些代码。 But the user should also be able to cancel the thread. 但是用户也应该能够取消该线程。 How can I cancel a thread and ensure that the thread is not cancelled when it execute the code? 如何取消线程并确保线程在执行代码时不被取消?

In Java you handle this with 在Java中,您可以使用它

while(!isInterrupted)

Is there any similar solution with pthreads. pthreads有没有类似的解决方案。

In the Question's example code you are checking some variable. 在问题的示例代码中,您正在检查某个变量。 This is not the normal pattern for interrupting threads in Java. 不是在Java中断线程的正常模式。

In Java, you interrupt a thread by calling the interrupt() method. 在Java中,您通过调用interrupt()方法来中断线程。

The thread then checks if it is interrupted inside IO and system calls (which can throw InterruptedException when this happens; this means a thread that is sleeping or waiting on IO can be awoken when interrupted) or by sampling the isInterrupted() flag (typically used in a condition in a loop, as in Question). 然后线程检查它是否在IO和系统调用中被中断(当发生这种情况时可以抛出InterruptedException ;这意味着正在休眠或等待IO的线程可以在被中断时唤醒)或者通过对isInterrupted()标志进行采样(通常使用)在循环中的条件,如问题)。

The distinction is important; 区别很重要; checking some flag variable you've declared is only possible in loops and your own code; 检查你声明的一些标志变量只能在循环和你自己的代码中; the Java interrupting system works for all threads and all non-CPU-blocking code without special effort on the part of the programmer. Java中断系统适用于所有线程和所有非CPU阻塞代码,而无需程序员的特别努力。

Pthreads has the pthread_cancel() pattern which works like the Java interrupting pattern. Pthreads具有pthread_cancel()模式,其作用类似于Java中断模式。

pthread_cancel is available for sending cancel requests: pthread_cancel可用于发送取消请求:

A thread's cancellation type, determined by pthread_setcanceltype(3) , may be either asynchronous or deferred (the default for new threads). pthread_setcanceltype(3)确定的线程的取消类型可以是异步的或延迟的(新线程的默认值)。 Asynchronous cancelability means that the thread can be canceled at any time (usually immediately, but the system does not guarantee this). 异步可取消性意味着可以随时取消线程(通常是立即取消,但系统不保证这一点)。 Deferred cancelability means that cancellation will be delayed until the thread next calls a function that is a cancellation point. 延迟可取消性意味着取消将被延迟,直到线程下一次调用作为取消点的函数。 A list of functions that are or may be cancellation points is provided in pthreads(7) . pthreads(7)提供了可能是或可能是取消点的函数列表。

A thread's cancelability state, determined by pthread_setcancelstate(3) , can be enabled (the default for new threads) or disabled. pthread_setcancelstate(3)确定的线程的可取消状态可以启用(新线程的默认值)或禁用。 If a thread has disabled cancellation, then a cancellation request remains queued until the thread enables cancellation. 如果线程已禁用取消,则取消请求将保持排队,直到线程启用取消。 If a thread has enabled cancellation, then its cancelability type determines when cancellation occurs. 如果线程已启用取消,则其可取消性类型确定何时取消。

So there are several options: 所以有几种选择:

1: while value checking (works very well, but you don't have much control). 1:值检查(效果很好,但你没有太多的控制权)。

2: check the pthread_cancel manpage, it works to but with strict rules. 2:检查pthread_cancel联机帮助页,但它有效,但有严格的规则。

3: using pthread_signal, first you need to block, than signal for resume. 3:使用pthread_signal,首先需要阻止,而不是恢复信号。 It has the same issues as the second option. 它与第二个选项具有相同的问题。

  • Using pthreads cancel and signal will only work from within the thread that must be locked. 使用pthreads cancel和signal只能在必须锁定的线程内工作。 So setting a variable to initiate the signal block. 因此设置变量以启动信号块。 Unlocking can be done by any other thread. 解锁可以由任何其他线程完成。
  • The same can be done using mutex or semaphores (pthread_mutex, pthread_semaphore). 使用互斥锁或信号量(pthread_mutex,pthread_semaphore)也可以完成相同的操作。

A site I recommend: http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html 我推荐的网站: http//www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

There's no specific function to cancel a thread You can use pthread_cancel to cancel the thread, as mentioned (but I would advise against it, unless you know what you're doing), and you have to set up your own timers. 取消线程没有特定的功能 你可以使用pthread_cancel来取消线程,如上所述(但我建议反对它,除非你知道你在做什么),并且你必须设置自己的计时器。 But the while(!isInterrupted) is pretty acceptable way of doing it. 但是while(!isInterrupted)是非常可接受的方式。

It should basically be like this: 它应该基本上是这样的:

while(!isInterrupted)
{
// whatever you want to do
sleep(howLongYouWantToWait);
}
// clean up and exit the thread function here

and in the main thread have a global (or other, see below) 并且在主线程中有一个全局(或其他,见下文)

volatile bool isInterrupted = false;

and set it to true when you're done, and pthread_join if you want to wait for the thread to finish. 完成pthread_join其设置为true ,如果要等待线程完成,则将其设置为pthread_join

Instead of global, you can use a class variable, or a flag pointer passed to the thread function, or any other way, global is the simplest and the least preferable. 您可以使用类变量或传递给线程函数的标志指针,或者任何其他方式,而不是全局,全局是最简单和最不可取的。

Of course, if you want to cancel the thread while it waits, and not to have it canceled only after it finishes the whole loop, then you need to deal with signals, and other stuff, but I think you're not looking for that. 当然,如果你想在它等待时取消线程,而不是在完成整个循环之后取消它,那么你需要处理信号和其他东西,但我认为你不是在寻找它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM