声明同这篇文章,以下是代码
#include <cmnheader.h>
void *theThread(void *param)
{
printf("Thread: Entered\n");
while (1)
{
printf("Thread: Looping or long running request\n");
pthread_testcancel();
Sleep(1);
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc = 0;
void *status;
printf("Enter Testcase - %s\n", argv[0]);
printf("Create/start a thread\n");
rc = pthread_create(&thread, NULL, theThread, NULL);
checkResults("pthread_create()\n", rc);
printf("Wait a bit until we 'realize' the thread needs to be canceled\n");
Sleep(3);
rc = pthread_cancel(thread);
checkResults("pthread_cancel()\n", rc);
printf("Wait for the thread to complete, and release its resources\n");
rc = pthread_join(thread, &status);
checkResults("pthread_join()\n", rc);
printf("Thread status indicates it was canceled\n");
if (status != PTHREAD_CANCELED)
{
printf("Unexpected thread status\n");
}
printf("Main completed\n");
system("pause");
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/1e2b786421b0d8ff70588a16a583b099.png)