• 多线程程序设计---1


    一、什么是线程

      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
    ");
    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
    ",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
    ");
    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
    ",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 }
     
  • 相关阅读:
    代码格式化[转]
    ASP.NET错误大杂烩
    Web考勤管理系统 .net 2005 开发
    Ftp 类
    c#中Split等分割字符串的几种方法
    强大的firebug 使用 介绍
    一页面多个文本框回车提交不同事件问题解决
    Ajax电子书下载 发现的好东东贴上了
    编程技术书籍[转]
    推荐下权威的《IT十年经典书系列》1打
  • 原文地址:https://www.cnblogs.com/zxouxuewei/p/5380560.html
Copyright © 2020-2023  润新知