
The threa_cancel() is used to cancel one particular thread by the thread id. This function sends one cancellation request to the thread for termination. The syntax of the pthread_cancel() is like below −
int pthread_cancel(pthread_t th);
现在,让我们看看如何使用这个函数来取消线程。
Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁。Shell既是一种命令语言,又是一种程序设计语言。作为命令语言,它交互式地解释和执行用户输入的命令;作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。它虽然不是Linux系统核心的一部分,但它调用了系统核心的大部分功能来执行程序、建立文件并以并行的方式协调各个程序的运行。因此,对于用户来说,shell是最重要的实用程序,深入了解和熟练掌握shell的特性极其使用方法,是用好Linux系统
24
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
int count = 0;
pthread_t sample_thread;
void* thread_one_func(void* p) {
while (1) {
printf("This is thread 1</p><p>");
sleep(1); // wait for 1 seconds
count++;
if (count == 5) {
//if the counter is 5, then request to cancel thread two and exit from current thread
pthread_cancel(sample_thread);
pthread_exit(NULL);
}
}
}
void* thread_two_func(void* p) {
sample_thread = pthread_self(); //store the id of thread 2
while (1) {
printf("This is thread 2</p><p>");
sleep(2); // wit for 2 seconds
}
}
main() {
pthread_t t1, t2;
//create two threads
pthread_create(&t1, NULL, thread_one_func, NULL);
pthread_create(&t2, NULL, thread_two_func, NULL);
//wait for completing threads
pthread_join(t1, NULL);
pthread_join(t2, NULL);
}This is thread 2 This is thread 1 This is thread 1 This is thread 2 This is thread 1 This is thread 1 This is thread 1 This is thread 2 This is thread 2 This is thread 2 This is thread 2 This is thread 2 This is thread 2 This is thread 2 This is thread 2 This is thread 2 This is thread 2 This is thread 2 This is thread 2
以上就是在C语言中,pthread_cancel()函数的含义是取消一个线程的详细内容,更多请关注php中文网其它相关文章!
C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号