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

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

#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://www.cnblogs.com/fengyhack/p/10603824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值