线程

LWP:light weight process,轻量级的线程,本质仍然是进程。
进程:独立地址空间,拥有PCB(Process Control Block)进程控制块。
线程:也有PCB,但没有独立的地址空间(共享)
区别:在于是否共享地址空间。
独居(进程);合租(线程)。
linux下:
线程:最小的执行单位
进程:最小的分配资源单位,可看成是只有一个进程的线程。
pthread_self函数
获取线程ID。其作用对应进程中getpid()函数。
pthread_t pthread_self(void);
返回值:成功 0;失败 无;
pthread_create函数
创建一个新线程。
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
返回值:成功 0;失败 错误号;
linux环境下,所有线程特点,失败均直接返回错误号。
创建线程示例代码pthread_create.c

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

void *thrd_func(void *arg)
{
	printf("In thread:thread id=%lu,pid=%u\n",pthread_self(),getpid());

	return NULL;
}

int main(void)
{
	pthread_t tid;
	int ret;

	printf("In main 1:thread id=%lu,pid=%u\n",pthread_self(),getpid());

	ret=pthread_create(&tid,NULL,thrd_func,NULL);
	if(ret!=0){
		printf("pthread_create error\n");
		exit(1);
	}

	sleep(1);
	printf("In main2:thread id=%lu,pid=%u\n",pthread_self(),getpid());
	
	return 0;
}
gcc -pthread pthread_create.c
./a.out

循环创建多个线程示例代码more_pthread.c

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

void *thrd_func(void *arg)
{

	int i=(int)arg;
	printf("%dth thread:thread id=%lu,pid=%u\n",i+1,pthread_self(),getpid());

	return NULL;
}

int main(void)
{
	pthread_t tid;
	int ret,i;

	for(i=0;i<50;i++){
		ret=pthread_create(&tid,NULL,thrd_func,&i);
		if(ret!=0){
			fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
			exit(1);
	}
	}
	sleep(i);
	return 0;
}

pthread_exit函数
将单个线程退出。
void pthread_exit(void *retval);
参数:retval表示线程退出状态,通常传NULL
线程退出示例代码pthread_exit.c

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

void *thrd_func(void *arg)
{
	printf("In thread:thread id=%lu,pid=%u\n",pthread_self(),getpid());

	return NULL;
}

int main(void)
{
	pthread_t tid;
	int ret;

	printf("In main 1:thread id=%lu,pid=%u\n",pthread_self(),getpid());

	ret=pthread_create(&tid,NULL,thrd_func,NULL);
	if(ret!=0){
		fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
		exit(1);
	}

	printf("In main2:thread id=%lu,pid=%u\n",pthread_self(),getpid());
	pthread_exit(NULL);	
	return 0;
}

pthread_join函数
阻塞等待线程退出,获取线程退出状态。其作用对应进程中waitpid()函数。
int pthread_join(pthread_t thread,void **retval);
成功:0;失败:错误号
线程退出示例代码pthread_join.c

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

typedef struct{
	char ch;
	int var;
	char str[64];
}exit_t;

void *thrd_func(void *arg)
{
	pthread_exit((void *)1);
}

int main(void)
{
	pthread_t tid;
	int ret;
	int *retval;

	printf("In main 1:thread id=%lu,pid=%u\n",pthread_self(),getpid());

	ret=pthread_create(&tid,NULL,thrd_func,NULL);
	if(ret!=0){
		fprintf(stderr,"pthread_create error:%s\n",strerror(ret));
		exit(1);
	}
	
	pthread_join(tid,(void **)&retval);
	printf("--------------%d\n",(int)retval);
	pthread_exit((void *)1);	
}

pthread_detach函数
实现线程分离
int pthread_detach(pthread_t thread);
成功:0;失败:错误号

pthread_cancel函数
杀死(取消)线程,其作用,对应进程中kill()函数。
int pthread_cancel(pthread_t thread);
成功:0;失败:错误号。
pthread_equal函数
比较两个线程ID是否相等。
int pthread_equal(pthread_t t1,pthread_t t2);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值