线程的创建、等待、终止、分离、同步和互斥

本文详细介绍了线程的概念、创建、管理和同步方法。包括线程的创建、分离、终止及互斥锁的使用,帮助读者理解多线程编程的基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程的概念:

线程就是进程的若干个执行流

有些情况需要在⼀个进程中同时执 ⾏多个控制流程,这时候线程就派上了⽤场,⽐如实现⼀个图形界⾯的下载软件,⼀⽅⾯需要和⽤ 户交互,等待和处理⽤户的⿏标键盘事件,另⼀⽅⾯又需要同时下载多个⽂件,等待和处理从多个 ⽹络主机发来的数据,这些任务都需要⼀个“等待-处理”的循环,可以⽤多线程实现,⼀个线程专门 负责与⽤户交互,另外⼏个线程每个线程负责和⼀个⽹络主机通信。

在系统内核中其实是不存在线程的,Linux使用进程模拟线程,线程的实现其实就是多个共享数据代码等信息的进程。所以我们把线程也叫做轻量级进程。

进程分配资源的基本单位,线程调度资源的基本单位。

线程中共享的资源: 
- 文件描述符表 
- 信号处理方式 
- 当前工作目录 
- uid,gid

线程中独立的资源: 
- 线程id(tid) 
线程的上下文信息,寄存器信息,PC制作,栈指针 
栈空间 
- 信号屏蔽字 
- 调度优先级 
- 线程私有数据

线程的函数大部分都放在pthread.h的头文件当中,并且在编译的时候我们需要注意的是加上-lpthread选项,这样就会去动态链接动态库。

线程的控制:

1.创建一个线程:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
第一个参数为指向新线程标识符的指针。
第二个参数用来设置线程属性。不想设置的情况下一般置为NULL.
第三个参数是新线程运行函数的起始地址。
最后一个参数是运行函数的参数。
若线程创建成功,则返回0,若线程创建失败,则返回出错编号。

2.线程创建以后,两个线程的pid和ppid都是一样的,操作系统会为之提供一个线程tid,这个tid我们可以通过一个函数获取。

pthread_t pthread_self();


#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<stdlib.h>

void * thread_run(void * arg)
{
	while (1)
	{
		printf("I am shm...mytid:%lu...pid:%d\n",pthread_self(),getpid());
		sleep(1);
	}
	return 0;
}

int main()
{

	pthread_t id;
	pthread_create(&id,NULL,thread_run,NULL);//这个id其实就是新线程的id
	printf("id = %lu\n",id);
	while (1)//两个线程的pid和ppid都是一样的,tid不同。
	{
		printf("I am main...mytid:%lu...pid:%d\n",pthread_self(),getpid());
		sleep(3);
	}
	return 0;
}



3.新的线程结束后,你的主线程也是需要等待的,然后回收新线程的资源及其他的信息。这样就能确保内存不泄露,所以这里使用一个函数pthread_join
来进行等待。

int phtread_join(pthread_t thread,void ** retval);
thread就是被等待的线程号,retval是一个二级指针,用途是用来获取新线程的退出码的。如果我们不关心新线程的退出码的话,直接传NULL,

这个函数是一个线程阻塞的函数,调用它的线程将一直等待到被等待的新线程结束为止,当函数返回时,被等待的线程的资源被回收,

0表示等待成功,失败返回错误码。


4.终止线程的三种方式

(1):从线程函数中直接return,主线程return表示所有的线程退出了。

(2):线程退出不能调用exit,如何任意一个线程调用了exit或_exit,则整个进程的所有线程都终止。使用pthread_exit函数终止自己的线程。

void pthread_exit(void *retval); 参数是返回的错误码,想要获得这个错误码,可以通过pthred_join来获得。

(3):使用pthread_cancel函数,可以用来取消进程。  自己可以取消,也可以被别人取消,一般是主线程取消新线程。int pthread_cancel(pthread_t thread); 参数为要终止的线程的tid.

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

void *thread1(void *_val1)
{
	printf("thread 1 returning....\n");
	sleep(5);
	return (void*)1;
}

void *thread2(void *_val2)
{
	printf("thread 2 exiting...\n");
	pthread_exit((void*)2);  //参数是返回的错误码。
}

void *thread3(void *_val3)
{
	while (1)
	{
		printf("pthread 3 is running ,wait for be cancal...\n");
		sleep(1);
	}
	return NULL;
}

int main()
{
	pthread_t tid;
	void *tret;


	//thread 1 return;
	pthread_create(&tid,NULL,thread1,NULL);
	pthread_join(tid,&tret);  //阻塞式等待新线程完成。

	printf("thread,return,thread id is:%lu,return code is:%d\n",tid,(int)tret);


	//thread 2 exit
	pthread_create(&tid,NULL,thread2,NULL);
	pthread_join(tid,&tret);
	printf("thread exit,thread id is :%lu,return code is :%d\n",tid,(int)tret);

	//thread 3 cancel by other
	pthread_create(&tid,NULL,thread3,NULL);
	sleep(3);
	pthread_cancel(tid);//取消子线程thread3
	pthread_join(tid,&tret);
	printf("thread return,thread id is:%lu,return code is:%d\n",tid,(int)tret);//此时的返回值是常数PTHREAD_CANCELED也就是-1.


	return 0;
}



5.线程分离:
int pthread_detach(phtread_t thread);
当我们把新线程设为可分离的时,这个时候主线程不再等待新线程,分离以后,这个时候的新线程是由操作系统来进行考虑的。
不再由主线程来考虑,这主线程中分离,这个时候主线程知道与新线程分离,主线程是不能调用pthread_join来进行等待的。

注:一般情况下新线程是默认可结合的,需要被主线程回收资源和杀死,一个可结合的线程会容易出现类似于僵尸进程的问题。
一般采用pthread_join来等待。否则就会出现主线程无法获取新线程信息,无法回收线程的资源,会造成内存泄露。

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

//验证子线程的分离


void *thread_run(void *_val)
{
	pthread_t tid = pthread_self();  //获取新线程id.
//	pthread_detach(tid); //子线程中分离。
	printf("%s\n",(char*)_val);
	return NULL;
}


int main()
{
	pthread_t tid;
	int tret = pthread_create(&tid,NULL,thread_run,"thread1 run...");
	if (tret != 0)
	{
		printf("create thread error!,info is:%s\n",strerror(tret));
		return tret;
	}

	//wait
	int ret = 0;
	sleep(1);
	pthread_detach(tid); //在主线程中分离。
	if (0 == pthread_join(tid,NULL)) //不关心退出码,直接置NULL,如果新线程已经分离,此时等待会失败.
	{
		printf("pthread wait success!\n");
	}
	else
	{
		printf("pthread wait failed!\n");
		ret = 1;
	}
	return ret;

}



线程同步和互斥:

当一个线程可以修改的变量,其他线程也可以读取或者修改的时候,这个时候就需要对这些线程进行同步,确保它们在访问变量的存储内容时不会访问到无效的值。其实实质就是当存储器读和存储器写这两个周期交叉的时候,就会使得得到与预想结果不一致的值。

线程的同步和互斥。当我们使用多线程的时候,多个线程对临界资源进行操作,这个时候如果非互斥的,那么这个时候对同一份临界资源进行操作的时候就会出现冲突的时候,比如当你对临界资源操作的时候,可能会中途进行线程的切换,这个时候原本你所读取的状态会随着硬件上下文和pc指针这些东西会保存下来,切换了线程以后,新切换的线程可能会去读取前一次你所读取的临界资源,然后对这份临界资源进行修改,然后这个时候新线程可能会再次切换,切换到你所原来保存的线程中,然后,回复了以前保存的硬件上下文和pc指针这些内容以后,这个时候线程所读取的临界资源的状态等信息还是在没有修改之前的,所以这个时候就会有隐患,造成一些缺点。

⽐如 两个线程都要把某个全局变量增加1,这个操作在某平台需要三条指令完成:
1. 从内存读变量值到寄存器
2. 寄存器的值加1
3. 将寄存器的值写回内存

正因为这三步操作,所以当不同步的时候,第一个线程已经对增量操作了,但是第二个线程读取到的依然是第一个线程增量操作之前的内容。这样就会出现问题,本来应该由1增加到3的,结果变为了由1到2。

所以从上面所说,可以发现多线程很容易发生上述的访问冲突,所以这里操作系统为我们提供了一个机制叫做互斥锁,这个互斥锁,我们可以去想前面所说的进程间通信的信号量,获得锁的线程可以对临界资源进行操作,没有获得锁的资源阻塞等待,二元信号量类似,获得锁的资源可以进行操作,没有获得锁的资源挂起进程放入挂起队列。

(1)使用锁的时候我们需要对锁进行初始化:
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);

如果mutex变量是静态分配的(全局变量或static变量)也可以用宏定义PTHREAD_MUTEX_INITIALIZER来初始化,
相当于用pthread_mutex_init初始化并且attr的参数为NULL.
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

(2)锁的操作函数:
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);

这三个函数第一个lock进行阻塞式申请锁,就是当你没申请到,那么就是一直阻塞等待。
第二个函数trylock是若锁资源无法获得,这个时候不阻塞,进行询问。每调用一次,进行询问一次,
最后一个unlock,这个函数就是用来解锁的,无论是lock还是trylock最后都要调用unlock来进行解锁。

(3)锁的销毁:
int pthread_mutex_destroy(pthread_mutex_t *mutex);

没有加上互斥锁代码:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>


int count = 0;//全局变量。

void* pthread_run(void *arg)
{
	int val = 0;
	int i = 0;

	while (i < 5000)
	{
		//这里会出现问题,就是当两个线程进行操作的时候count的+是非原子的。
		i++;
		val = count;
		printf("pthread:%lu,count:%d\n",pthread_self(),count);
		count = val + 1;
	}

	return NULL;
}

int main()
{
	pthread_t tid1;
	pthread_t tid2;

	pthread_create(&tid1,NULL,pthread_run,NULL);
	pthread_create(&tid2,NULL,pthread_run,NULL);

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	printf("count:%d\n",count);

	return 0;
}


加上互斥锁代码:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>


//加上互斥锁

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//锁的初始化。
int count = 0;

void* pthread_run(void *arg)
{
	int val = 0;
	int i = 0;

	while (i < 5000)
	{
		//在临界区加上互斥锁,这样就可解决线程访问冲突的问题了。

		pthread_mutex_lock(&mutex);//加锁,保证操作的原子性。
		i++;
		val = count;
		printf("pthread:%lu,count:%d\n",pthread_self(),count);
		count = val + 1;
		pthread_mutex_unlock(&mutex);//解锁
	}
	return NULL;
}


int main()
{
	pthread_t tid1;
	pthread_t tid2;

	pthread_create(&tid1,NULL,pthread_run,NULL);
	pthread_create(&tid2,NULL,pthread_run,NULL);

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	printf("count = %d\n",count);
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值