linux学习笔记之多线程

:多进程存在的问题:

1、多进程时候资源消耗过大; ===》4G
2、直接进程间通信有障碍; ===》共享内存
3、多进程之间的速度差异; ===》父快于子
4、多进程的创建个数限制; ===》ulimit -a

基于以上问题,提出多线程解决方案:

1、线程是一个轻量级进程;
2、线程是进程中的多个任务;
3、线程是CPU调度的最小单位;
进程是操作系统调度的最小单位

线程与进程的区别:
资源:
多线程比多进程多了共享内存;
多线程和多进程都有私有资源;

空间:
多线程中所有线程共享进程4g内存;
多进程中所有进程独享进程4g内存;

多线程的使用框架:
创建多线程 ====》读写多线程空间 ===》关闭回收多线程;
pthread_create 回调函数 pthread_cancel/pthread_exit
pthread_join

linux的线程模型使用的是POSIX 线程模型 : pthread;

1、编写代码的时候需要加入三方库头文件: #include <pthread.h>
2、编译代码的时候需要加入库编译选项: gcc xxxx.c -l pthread

1、创建多线程:pthread_create
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
功能:该函数可以用于动态创建一个执行线程;
参数:thread 线程id,一般在创建完毕会自动赋值;
attr 线程属性,一般不用调整,用NULL 表示;
start_routine 指向指针函数的函数指针;
指针函数为: void * fun(void * arg);
函数指针为: xxx (*pfun)(void * arg);
===>pfun == fun ===>函数名字 ===》回调函数
===》线程的执行空间;

  arg   线程回调函数的参数,即参数4的函数参数;
        如果没有参数传递用NULL表示;

返回值:成功 0
失败 -1

判断一个进程是否是多进程的依据:
1、ps aux ===>STAT 列有 l 符号
2、pstree |grep a.out ===>多个线程个数

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

void * fun(void * arg)
{
	printf("fun tid = %lu \n",pthread_self());
	while(1) sleep(1);
}

int main(int argc, char *argv[])
{
	pthread_t tid1 = 0,tid2 = 0;
	int ret = pthread_create(&tid1,NULL,fun,NULL);
	ret = pthread_create(&tid2,NULL,fun,NULL);
	if(ret < 0)
	{
		perror("pthread create error");
		return -1;
	}
	printf("pthread ret = %lu %lu\n",tid1,tid2);

	while(1) sleep(1);
    return 0;
}

多线程编程:

1、pthread_create()

2、pthread_t pthread_self(void)
功能:该函数可以获取当前线程的tid号并返回;
参数:无
返回值:成功 当前线程的tid号
失败 -1;

3、线程的退出:
5.1 在子线程内部执行 return 可以退出线程;
5.2 pthread_exit(); 可以在线程执行过程中退出线程;
5.3 pthread_cancel(); 可以在其他线程中杀死执行线程;

void pthread_exit(void *retval);
功能:该函数用于线程自身结束并退出当前线程;
参数:retval 线程退出时候的返回值;
返回值:无

int pthread_cancel(pthread_t thread);
功能:该函数可以用于结束指定线程的运行;
参数:thread 要结束的目标线程tid号
返回值:成功 0
		失败 -1;

子线程的资源回收: pthread_join()
int pthread_join(pthread_t thread, void **retval);
功能:该函数用于阻塞等待回收执行线程的资源。
参数:thread 要回收的目标线程tid;
retval 回收的目标线程返回值;如果不关心返回值用NULL;

返回值:成功0
失败 -1;

在这里插入代#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void * fun(void * arg)
{
	printf("fun tid = %lu \n",pthread_self());
	sleep(10);
	pthread_exit(0);
}

int main(int argc, char *argv[])
{
	pthread_t tid1 = 0,tid2 = 0;
	int ret = pthread_create(&tid1,NULL,fun,NULL);
	if(ret < 0)
	{
		perror("pthread create error");
		return -1;
	}
	printf("pthread create ret = %lu\n",tid1);
	ret= pthread_join(tid1,NULL);
	printf("pthread join ret = %d \n",ret);

    return 0;
}码片

练习: 设计多个(3个以上)子线程同时运行,要求每个线程退出的时机
不同,如何在线程中不阻塞回收每个子线程,要求谁先退出就
立即回收该子线程。

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_t a=0;

void * fun1(void * arg)
{
	sleep(3);
	printf("1\n");        
	a=pthread_self();
	pthread_exit(NULL);
}
void * fun2(void * arg)
{
	sleep(9);
	printf("2\n");        
	a=pthread_self();
	pthread_exit(NULL);
}
void * fun3(void * arg)
{
	sleep(6);
	printf("3\n");        
	a=pthread_self();
	pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
	pthread_t tid1=0,tid2=0,tid3=0;
	int n=3;
	pthread_t x=0;

	int ret =pthread_create(&tid1,NULL,fun1,NULL);
	ret=pthread_create(&tid2,NULL,fun2,NULL);
	ret=pthread_create(&tid3,NULL,fun3,NULL);

	if(ret<0)
	{
		perror("pthread_create failed");
		return -1;

	}

	printf("pthread_create=%lu %lu %lu\n",tid1,tid2,tid3);
	while(n)
	{
		x=a;
		usleep(1);
        x=a;
		if(x!=a)
		{
			pthread_join(a,NULL);

		}
		else continue;
		n--;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值