C语言 在 C 中终止一个线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6836894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:14:46  来源:igfitidea点击:

terminating a thread in C

cmultithreadingpthreads

提问by Vishal

I a have a C program which calls to threads.

我有一个调用线程的 C 程序。

iret1 = pthread_create( &thread1, NULL, readdata, NULL);
iret2 = pthread_create( &thread2, NULL, timer_func, NULL);
pthread_join(thread2, NULL);

Thread 2 returns after performing some function, after which I want to stop the execution of thread 1. How should I do this?

线程 2 在执行某些功能后返回,之后我想停止线程 1 的执行。我该怎么做?

回答by Antti

You can stop the thread using pthread_cancel:

您可以使用pthread_cancel以下命令停止线程:

pthread_cancel(thread1);

And in readdata:

并在readdata

/* call this when you are not ready to cancel the thread */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
...
/* call this when you are ready to cancel the thread */
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

See the pthread_cancel man pagefor more information - there's an example included.

有关更多信息,请参阅pthread_cancel 手册页- 包含一个示例。

If you don't want to use pthread_cancel, you can use a global flag that is set by the main thread and read by thread 1. Also, you can use any of the IPC methods, like establishing a pipebetween the threads.

如果不想使用 pthread_cancel,则可以使用由主线程设置并由线程 1 读取的全局标志。此外,您可以使用任何 IPC 方法,例如在线程之间建立管道

回答by David Heffernan

You should signal to the thread that you wish it to stop work, and then wait for it to do so. For example, you could set a boolean flag that the thread tests regularly. If that flag indicates that work has been cancelled, then the thread should return from the thread function.

您应该通知线程您希望它停止工作,然后等待它停止工作。例如,您可以设置线程定期测试的布尔标志。如果该标志表明工作已被取消,则该线程应从线程函数返回。

Don't attempt to forcibly terminate the thread from the outside because this will leave synchronisation objects in indeterminate state, lead to deadlocks etc.

不要试图从外部强行终止线程,因为这会使同步对象处于不确定状态,导致死锁等。

回答by André Puel

Thread1 must have a flag which it verifies from time to time to see if it should abort itself.

Thread1 必须有一个标志,它会不时验证它是否应该中止自身。

There are ways to abort a thread from outside, but this is very dangerous. Don't.

有多种方法可以从外部中止线程,但这是非常危险的。别。

Something like:

就像是:

thread1stop=TRUE; //Thread 1 has access to this boolean value
pthread_join(thread1, NULL);

回答by phlogratos

You can stop thread1 by calling pthread_cancel(thread1).

您可以通过调用来停止 thread1 pthread_cancel(thread1)

回答by Jake Kalstad

tkill(tid, SIGTERM) is the call you are looking for I do believe.

tkill(tid, SIGTERM) 是您正在寻找的电话,我相信。

回答by A. K.

pthread_cancel() function sends a cancellation request to the thread.

pthread_cancel() 函数向线程发送取消请求。

After sending the request to cancel the thread you should check the return code to confirm that the thread was actually cancelled or not.

发送取消线程的请求后,您应该检查返回码以确认线程是否实际被取消。

Here is a simple example:

这是一个简单的例子:

rc = pthread_cancel(iret2);
if(rc) printf("failed to cancel the thread\n");

For further reference:

进一步参考:

http://cursuri.cs.pub.ro/~apc/2003/resources/pthreads/uguide/users-39.htm

http://cursuri.cs.pub.ro/~apc/2003/resources/pthreads/uguide/users-39.htm

Other sources which might be useful to you.

其他可能对您有用的来源。

http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_create.3.html

http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_create.3.html

http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_cancel.3.html

http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_cancel.3.html