Windows下pthread多线程使用(4):CancelThread

来源:互联网 发布:资产证券化 知乎 编辑:程序博客网 时间:2024/05/23 11:49

声明同这篇文章,以下是代码

#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;}


0 0
原创粉丝点击