Linux下线程的启动-取消-终止

#pragma once
#include<pthread.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
class CThread
{
public:
	int Start();
	void Stop();
	void Run();
	static void* ThreadProc(void* param);
	static void* ThreadProc_1(void* pParam);
	static void* ThreadProc_2(void* pParam);
	static void* ThreadProc_3(void* pParam);
	static void* ThreadProc_4(void* pParam);
	pthread_t GetThreadId()
	{
		return m_pid;
	}
private:
	pthread_t m_pid;
};
#include"thread.h"
int a = 20;
int CThread::Start()
{
	return pthread_create(&m_pid, NULL, ThreadProc_4,this);
}
void* CThread::ThreadProc(void* pParam)
{
	CThread* pThread = (CThread*)pParam;
	pThread->Run();
	int a = 10;
	int* p = &a;
	cout << "I am new thread" << endl;
	return (void*)p;
}
//在栈上分配内存
void* CThread::ThreadProc_1(void* pParam)
{
	int a = 10;
	int* p = &a;
	cout << "I am new thread" << endl;
	return (void*)p;
}
//在栈上分配内存
void* CThread::ThreadProc_2(void* pParam)
{
	int a = 10;
	int* p = &a;
	int count = 0;
	while (1)
	{
		++count;
		if (count > 5)
		{
			pthread_exit(p);
		}
		cout << "I am new thread" << endl;
		sleep(1);
	}
}
//在堆上分配内存
void* CThread::ThreadProc_3(void* pParam)
{
	int a = 10;
	int* p = (int*) malloc(sizeof(int));
	*p = 10;
	int count = 0;
	while (1)
	{
		++count;
		if (count > 5)
		{
			pthread_exit(p);
		}
		cout << "I am new thread" << endl;
		sleep(1);
	}
}
// 全局变量
void* CThread::ThreadProc_4(void* pParam)
{
	int* p = &a;
	int count = 0;
	while (1)
	{
		++count;
		if (count > 5)
		{
			pthread_exit(p);
		}
		cout << "I am new thread" << endl;
		sleep(1);
	}
}
void CThread::Run()
{
	while (1)
	{
		cout << "I am new thread: " << pthread_self() << endl;
		sleep(1);
	}
}
/*
线程停止函数:
1- pthread_cancel();线程的取消并不是实时的,而是有一定的延时,需要等待线程到达某个取消点(检查点),
pthread_cancel()可以取消同一个进程中其他线程
2- pthread_exit() :终止的线程可以调用pthread_exit()来终止自己
3- 需要从终止的线程中return 
4-5 pthread_exit 和 return 返回的指针所指向的内存单元必须是全局或者是用malloc分配的,不能在线程函数栈上分配,因为这块内存其他线程还需要使用
如代码ThreadProc_1-4测试所示
*/
void CThread::Stop()
{
	cout << "Thread: " << m_pid << " Exit"<<endl;
//	pthread_exit(szReturnVal);
	void* ret;
	pthread_cancel(m_pid);
	pthread_join(m_pid,&ret);
	if (ret == PTHREAD_CANCELED)
	{
		cout << "thread is return,thread id: " << m_pid << " return code:PTHREAD_CANCELED\n" << endl;
	}
	else
		cout << "thread is return,thread id: " << m_pid << "return code:NULL\n"<<endl;
}
#include"thread.h"

int main()
{
	CThread cThread;
	if (0 == cThread.Start())
	{
		cout << "Thread Start" << endl;
	}

	pthread_t tid;
	void* ret;
	tid = cThread.GetThreadId();
	cout << pthread_join(tid, &ret);
	cout << "return value: " << *((int*)ret) << endl;
	/*int count = 1;
	while (1)
	{
		++count;
		if(count > 30)
			cThread.Stop();
	}*/
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值