# linux多线程编程

# 基本概念

程序执行的最小单位

进程是线程的容器,不是基本执行单位,是线程容器

线程是进程中的不同执行路径, 有独立的堆栈、局部变量(因为线程需要线程函数

进程有自己的内存空间,线程在进程内,所以多进程程序比多线程健壮,但是进程切换开销大于线程切换。

# 基本api

需要-lpthred 编译选项,因为需要pthread库

线程开发基本概念有3个: 线程、互斥锁、条件

  • 线程操作: 创建,退出和等待3种状态。
//创建线程
	int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
	参数一:线程id
	参数二:线程属性,默认可置为NULL,表示线程属性取缺省值
	参数三:线程入口函数
	参数四:*线程入口函数的参数

//线程退出
	void pthread_exit(void *retval);
	参数:线程退出时的返回值,也可为NULL

//线程等待
	int pthread_join(pthread_t thread, void **retval);
	参数二:收回线程的退出状态线程的返回值不感兴趣,可以把rval_ptr置为NULL

 //获取线程ID
	pthread_t pthread_self(void);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  • demo
#include <stdio.h>
#include <pthread.h>
void *func(void *arg)
{
        static char *p = "xiaobian henshuai";
        printf("this is t1 thread pid=%ld\n",(unsigned long)pthread_self());
        printf("deliver param is %d\n",*(int *)arg);
        pthread_exit((void *)p);
        //退出时返回退出时的状态,返回一个字符串
}
int main()
{
        pthread_t t1;
        char *p =NULL;
        int param = 10;
        pthread_create(&t1,NULL,func,(void*)&param);
        //创建线程时,传递了一个param参数
        pthread_join(t1,(void **)&p);
        //等待t1线程的退出,用p收集,t1线程退出时的状态
        printf("return value is %s\n",p);
        return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  • 互斥锁操作:创建,销毁,加锁,解锁 4种操作
#include <pthread.h>
//互斥锁的初始化
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
参数一:互斥锁变量mutex
参数二:通常为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);
1
2
3
4
5
6
7
8
9
10
11

-demo

#include <stdio.h>
#include <pthread.h>
int data = 0;//定义一个全局变量验证线程是共享内存空间的
pthread_mutex_t mutex;
void *func1(void *arg)
{
         static char *p = "xiaobian henshuai";
         printf("t1:this is t1 thread pid=%ld\n",(unsigned long)pthread_self());
         printf("t1:deliver param is %d\n",*(int *)arg);
         int i=3;
         pthread_mutex_lock(&mutex);//加锁的目的是在执行以下代码的过程中不会被其他的进程打断
         while(i--)
         {
                 printf("t1:data=%d\n",data++);
                 sleep(1);
         }
         pthread_mutex_unlock(&mutex);
         pthread_exit((void *)p);
}

void *func2(void *arg)
{
        pthread_mutex_lock(&mutex);
        static char *p = "xiaobian henshuai";
        printf("t2:this is t2 thread pid=%ld\n",(unsigned long)pthread_self());
        printf("t2:deliver param is %d\n",*(int *)arg);
        while(1)
        {
                printf("t2:data=%d\n",data++);
                sleep(1);
        }
        pthread_mutex_unlock(&mutex);
        pthread_exit((void *)p);
}
int main()
{
        pthread_t t1;
        pthread_t t2;
        char *p =NULL;
        int param = 10;
        pthread_create(&t1,NULL,func1,(void*)&param);
        pthread_create(&t2,NULL,func2,(void*)&param);
        pthread_mutex_init(&mutex,NULL);
        pthread_join(t1,(void **)&p);
        pthread_join(t2,(void **)&p);
        pthread_mutex_destroy(&mutex);
        printf("main:return value is %s\n",p);
        return 0;
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

死锁是怎么产生的:前提是有两个锁,线程一获得锁一的同时,又想获得锁二,线程二获得锁二的同时,又想获得锁一,线程一和二都想拿到对方的锁,导致线程停滞不前。

  • 条件操作: 创建,销毁,触发,广播,等待 5种操作
#include <pthread.h>
//创建条件变量
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
//销毁条件变量
int pthread_cond_destroy(pthread_cond_t* cond);
//等待
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
//触发
int pthread_cond_signal(pthread_cond_t c*ond)
//广播
int pthread_cond_broadcast(pthread_cond_t cond);
1
2
3
4
5
6
7
8
9
10
11
  • demo
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
int data = 0;
pthread_mutex_t mutex;
pthread_cond_t cond;
void *func1(void *arg)
{
        printf("t1:this is t1 thread pid=%ld\n",(unsigned long)pthread_self());
        printf("t1:deliver param is %d\n",*(int *)arg);
        while(1)
        {
                pthread_cond_wait(&cond,&mutex);//等待某个条件的唤醒
                printf("t1 run========\n");
                data=0;
        }
}
void *func2(void *arg)
{
        printf("t2:this is t2 thread pid=%ld\n",(unsigned long)pthread_self());
        printf("t2:deliver param is %d\n",*(int *)arg);
        while(1)
        {
                printf("t2:data=%d\n",data);
                pthread_mutex_lock(&mutex);
                data++;
                if(data==3)
 		{
                        pthread_cond_signal(&cond);//唤醒条件执行线程t1
                }
                pthread_mutex_unlock(&mutex);
                sleep(1);
        }
}
int main()
{
        pthread_t t1;
        pthread_t t2;
        char *p =NULL;
        int param = 10;
        pthread_create(&t1,NULL,func1,(void*)&param);
        pthread_create(&t2,NULL,func2,(void*)&param);
        pthread_mutex_init(&mutex,NULL);
        pthread_cond_init(&cond,NULL);
        pthread_join(t1,(void **)&p);
        pthread_join(t2,(void **)&p);
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
        printf("main:return value is %s\n",p);
        return 0;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51