pthread_cancel() =该函数使用线程ID取消特定线程。该函数向线程发送取消请求。
语法:-int pthread_cancel(pthread_t thread);
第一个程序:-取消自线程
// C program to demonstrates cancellation of self thread
// using thread id
#include
#include
#include
void* calls(void* ptr)
{
printf("GeeksForGeeks");
// To exit the current thread
// pthread_self() return the particular thread id
pthread_cancel(pthread_self());
return NULL;
}
int main()
{
// NULL when no attribute
pthread_t thread;
// calls is a function name
pthread_create(&thread, NULL, calls, NULL);
// Waiting for when thread is completed
pthread_join(thread, NULL);
return 0;
}
输出:
GeeksForGeeks
如果使用Linux,则编译此程序gcc program_name.c -lpth