1、进程线程区别
1.进程占内存,比如父子进程copy内存空间
线程共享内存空间
2.线程切换和创建速度比进程快
进程开销大约是线程的30倍
3.线程因为共享内存,所以通信快
2、线程(创建退出等待)
1.API
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
创建
返回值:成功0失败-1
参数1:pthread_t型指针t1
参数2:线程属性,NULL
参数3:函数指针
参数4:无类型数据
例:
函数void* func1(void*arg)
输出主函数传过来的值,*((int*)arg)
set=pthread_create(&t1,NULL,func1,(void*)¶m);
void pthread_exit(void *retval);
退出
参数:无类型指针,int char都可以,但线程函数里定义要static,保证内存不释放,主函数才能打印。
例:
static int set=10
pthread_exit((void*)&set)
int pthread_join(pthread_t thread, void **retval);
等待,主函数阻塞一直等待线程退出
参数1:线程t1,创建线程的参数1
参数2:线程退出返回的值,二级指针
例:
int *pret=NULL
pthread_join(t1,(void**)&pret)
输出直接输出pret地址内容
pthread_t pthread_self(void);
返回线程id
返回值:长整型
例:
主线程和新线程都有id
%ld输出
(unsigned long)pthread_self()
int pthread_equal(pthread_t t1, pthread_t t2);
id比较,相等返回非0,否则0
2.例:
创建线程
编写线程函数,输出主函数的值和线程id,退出返回一个数据
主函数,输出id,等待线程,输出线程退出的数
注:编译加-lpthread
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *fun1(void *arg)
{
static char *ptr = "wei";
printf("t1:main=%d\n",*((int *)arg));
printf("t1:id=%ld\n",(unsigned long)pthread_self());
pthread_exit((void *)ptr);
}
int main()
{
pthread_t t1;
int arg = 100;
char *a = NULL;
if(pthread_create(&t1, NULL, fun1, (void *)&arg) == 0){
printf("found thread succeed\n");
}
printf("main:id=%ld\n",(unsigned long)pthread_self());
pthread_join(t1, (void **)&a);
printf("main:t1 exit,%s\n",(char*)a);
return 0;
}
/*
found thread succeed
t1:main=100
t1:id=140084299699968
main:id=140084308203328
main:t1 exit,wei
*/
3、互斥量,锁
1.互斥量本质上是锁
加锁,解锁在线程开头结尾,可以保证线程在执行完之前不被打断。(主函数可以打断)
2.API
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr)
互斥量创建(锁)
参数1:全局变量,指针
参数2:默认属性互斥量,NULL
int pthread_mutex_destroy(pthread_mutex_t *mutex)
销毁锁
参数:创建锁时的全局变量
int pthread_mutex_lock(pthread_mutex_t *mutex)
加锁
参数:全局变量指针
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁
参数:全局变量指针
3.线程的共享内存
线程改变全局变量
4.例:
全局变量data和锁变量
创建锁,创建2个线程
2线程函数
1: 加锁,循环data++,sleep延时1s,if3解锁退出线程
2:死循环输出data,加锁data++,解锁,延时1s
主函数
打印data
等待线程
销毁锁
注:线程2加锁是因为锁被线程1拿了,没有锁拿就会卡在那里。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
int data = 0;
pthread_mutex_t nutex;
void *fun1(void *arg)
{
printf("t1:id=%ld\n",(unsigned long)pthread_self());
pthread_mutex_lock(&nutex);
while(1){
data++;
printf("t1:data=%d\n",data);
if(data == 3){
printf("t1 quit =============================\n");
pthread_mutex_unlock(&nutex);
pthread_exit(NULL);
}
sleep(1);
}
}
void *fun2(void *arg)
{
printf("t2:id=%ld\n",(unsigned long)pthread_self());
while(1){
pthread_mutex_lock(&nutex);
data++;
pthread_mutex_unlock(&nutex);
printf("t2:data=%d\n",data);
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
pthread_t t1;
pthread_t t2;
int arg = 100;
pthread_mutex_init(&nutex, NULL);
if(pthread_create(&t1, NULL, fun1, (void *)&arg) == 0){
printf("found thread succeed\n");
}
if(pthread_create(&t2, NULL, fun2, (void *)&arg) == 0){
printf("found thread succeed\n");
}
printf("main:id=%ld\n",(unsigned long)pthread_self());
while(1){
printf("main:%d\n",data);
sleep(1);
}
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&nutex);
return 0;
}
/*
found thread succeed
t1:id=140562014590720
t1:data=1
found thread succeed
main:id=140562023094080
main:1
t2:id=140562006198016
main:1
t1:data=2
t1:data=3
t1 quit =============================
main:2
t2:data=4
main:4
t2:data=5
t2:data=6
main:6
*/
3、什么是死锁
2个线程有时会造成死锁,2个锁
一个线程拿到锁1,要拿锁2
另一个拿到锁2,要拿锁1
造成2个线程都不能继续运行下去
4、条件
1.一个线程等待触发条件。
2.API
int pthread_cond_init
创建
pthread_cond_destroy
销毁
int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex)
等待
参数1:条件
参数2:锁
pthread_cond_signal
触发
参数:条件&cond
pthread_cond_broadcast
广播
3.例:
2个线程,锁,条件初始化
线程1等待条件
线程2加锁data++,触发条件
线程1执行
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
int data = 0;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t nutex = PTHREAD_MUTEX_INITIALIZER;
void *fun1(void *arg)
{
while(1){
pthread_cond_wait(&cond, &nutex);
printf("t1:%d\n",data);
data = 0;
}
}
void *fun2(void *arg)
{
while(1){
pthread_mutex_lock(&nutex);
data++;
pthread_mutex_unlock(&nutex);
printf("t2:data=%d\n",data);
if(data == 3){
pthread_cond_signal(&cond);
}
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
pthread_t t1;
pthread_t t2;
pthread_mutex_init(&nutex, NULL);
pthread_create(&t1,NULL,fun1,NULL);
pthread_create(&t2,NULL,fun2,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&nutex);
return 0;
}
/*
t2:data=1
t2:data=2
t2:data=3
t1:3
t2:data=1
t2:data=2
t2:data=3
t1:3
t2:data=1
t2:data=2
t2:data=3
t1:3
*/
5、线程初始化宏
动态初始化
pthread_cond_init(&cond, NULL);
静态初始化
pthread_cond_t cond=PTHREAD_COND_INITIALIZER
6、生产者消费者
数据缓冲期访问
师承上官可编程 —— 陈立臣