多线程程序设计

               多线程程序设计

一、什么是线程

  1.线程就是“轻量级”的进程。

  2.线程与创建它的进程共享代码段,数据段。

  3.线程拥有自己独立的栈。

二、函数学习

1创建线程

1).函数名

  pthread_create

2).函数原型

    int  pthread_create(pthread_t *thread, const pthread_attr_t *attr ,void *(*start_routine)(void *), void  *arg)

3).函数功能

创建一个新的线程

4).所属头文件

  <pthread.h>   特别注意:编译时候必须链接pthread库

  gcc –lpthread

5).返回值

  成功:0

  失败:错误编码

6).参数说明

  thread:新创建的线程ID

  attr:待创建线程的属性,一般为NULL

  start_routine:线程的入口函数

  arg:线程入口函数的参数,可以为NULL

2等待线程结束

1).函数名

  pthread_join

2).函数原型

  int pthread_join(pthread_t thread,void  **retval)

3).函数功能

  用于等待线程结束

4).所属头文件

  <pthread.h>   特别注意:编译时候必须链接pthread库

  gcc –lpthread

5).返回值

  成功:0

  失败:错误编号

6).参数说明

  thread:要等待结束的线程id

  retval:保存线程退出时的状态,一般为NULL

3退出线程

1).函数名

  pthread_exit

2).函数原型

  void pthread_exit(void *retval)

3).函数功能

  结束线程

4).所属头文件

  <pthread.h>   特别注意:编译时候必须链接pthread库

  gcc –lpthread

5).返回值

  空

6).参数说明

  retval:保存返回值

三、线程互斥

  在实际应用中,多个线程往往会访问同一数据或资源,为避免线程之间相互影响,需要引入线程互斥机制,而互斥锁(mutex)是互斥机制中的一种。

3.1互斥锁初始化

1).函数名

  pthread_mutex_init

2).函数原型

  int pthread_mutex_init(pthread_mutex_t *restrict mutex ,const pthread_mutexarrt_t *restrict attr);

3).函数功能

  初始化互斥锁

4).所属头文件

  <pathread.h>

5).返回值

  成功:0

  失败:错误的编码

6).参数说明

  mutex:要初始化互斥锁的指针

  attr:可以添加的属性,为空则为默认属性

3.2获取互斥锁

1).函数名

  pthread_mutex_lock

2).函数原型

  int  pthread_mutex_lock(pthread_mutex_t *mutex);

3).函数功能

  锁住互斥锁

4).所属头文件

  <pthread.h>

5).返回值

  成功:0

  失败:错误的编码

6).参数说明

  mutex:要锁住互斥锁的指针

3.3释放互斥锁

1).函数名

  pthread_mutex_unlock

2).函数原型

  int  pthread_mutex_unlock(pthread_mutex_t *mutex);

3).函数功能

  解开互斥锁

4).所属头文件

  <pthread.h>

5).返回值

  成功:0

  失败:错误的编码

6).参数说明

  mutex:要解开互斥锁的指针

四、实例学习

  通过主函数创建两个进程,使这两个进程交互完成一项任务

 1 #include <pthread.h>
 2 #include <string.h>
 3 #include <stdio.h>
 4 
 5 pthread_t thread[2];  6 int number = 0;  7 
 8 pthread_mutex_t mut;  9 
10 void *worker1() 11 { 12     int i=0; 13     printf("I am worker1\n"); 14     for(i=0;i<10;i++) 15  { 16         pthread_mutex_lock(&mut); 17         number++; 18         pthread_mutex_unlock(&mut); 19         printf("worker1 number is %d\n",number); 20         sleep(1); 21  } 22  pthread_exit(NULL); 23 } 24 
25  
26 void *worker2() 27 { 28     int i=0; 29     printf("I am worker2\n"); 30     for(i=0;i<10;i++) 31  { 32         pthread_mutex_lock(&mut); 33         number++; 34         pthread_mutex_unlock(&mut); 35         printf("worker2 number is %d\n",number); 36         sleep(1); 37  } 38  pthread_exit(NULL); 39 } 40 
41 int main() 42 { 43     pthread_mutex_init(&mut,NULL); 44     
45     //创建工人1线程
46     pthread_create(&thread[0],NULL ,worker1,NULL); 47     
48     //创建工人2线程
49     pthread_create(&thread[1],NULL ,worker2,NULL); 50     
51     //等待工人1线程结束 
52     pthread_join(thread[0],NULL); 53     
54     //等待工人2线程结束
55     pthread_join(thread[1],NULL); 56     
57     
58     return 0; 59 }

 

多线程程序设计_第1张图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.4综合实例

通过主函数创建两个进程,使这两个进程交互完成一项任务

 

你可能感兴趣的:(多线程程序设计)