现在的位置: 首页 > 综合 > 正文

Linux c 多线程 互斥量

2019年05月28日 ⁄ 综合 ⁄ 共 2901字 ⁄ 字号 评论关闭

多线程:

1.      了解多线程

        解决多任务实现

        历史上Unix服务器不支持多线程

        Unix/Linux上实现多线程有两种方法:

              内核支持多线程

              使用进程的编程技巧封装进程实现多线程:轻量级多线程

              多线程的库:

                    libpthread.so   -l pthread

                    pthread.h

        

           

2.      创建多线程

2.1.代码:

回调函数

2.2.线程ID

     pthread_t(类型)

2.3.运行线程

      pthread_create

         #include<pthread.h>

                 int pthread_create(

                               pthread_t *th,//返回进程ID

                               constpthread_attr_t  *attr,//线程属性,为NULL/0,使用进程的默认属性

                               void*(*run)(void*),//线程代码

                               void *data);//传递线程代码的数据

例子代码:

#include<stdio.h>

#include<pthread.h>

 

 

void* run(void *  data)

{

      printf(“我是线程\n”);

}

 

void  main()

{

       pthread_t  tid;

       pthread_create(&tid,0,run,0);

}

 

编译命令:gcccreateth.c –o main  -l pthread

 

结论:

1.      程序结束所有子线程就结束

       解决办法:等待子线程结束

                 sleep

                 pthread_join函数

2.      创建子线程后,主线程继续完成系统分配时间片

3.      子线程结束就是线程函数返回

4.      子线程和主线程有同等优先级

 

#include<stdio.h>

#include<pthread.h>

#include<stdlib.h>

 

 

void* run(void *  data)

{

      printf(“我是线程\n”);

}

 

void  main()

{

       pthread_t  tid;

       pthread_create(&tid,0,run,0);

       sleep(1);

}

 

pthread_join函数:

 

int pthread_join(pthread_t  thread ,//等待的线程id

void ** value_ptr); //子线程结束的返回值

 

#include<stdio.h>

#include<pthread.h>

 

 

void* run(void *  data)

{

      printf(“我是线程\n”);

}

 

void  main()

{

       pthread_t  tid;

       pthread_create(&tid,0,run,0);

       pthread_join(tid,(void**)0);

}

 

 

3.      线程的基本控制

      线程的状态:

         ready(准备状态)->runny->deady

                             |

                           sleep/pause

     怎么结束线程:

        内部自动结束:(建议)

         return 返回值 (在线程函数中使用)

         void pthread_exit(void * value)(任何线程代码中使用)

        外部结束一个线程。

              int   pthread_cancel(pthread_t  thread);

   

       

例子:

#include<stdio.h>

#include<pthread.h>

void*  run(void * data)

{

       printf(“我是线程\n”);

       //return “world”;

       pthread_exit(“world”);

}

void   main()

{

       pthread_t   tid;

       char *re;

       pthread_create(&tid,0,run,0);

       pthread_join(tid,&re);

       printf(“%s\n”,re);

}

 

程序打印:world

 

4.      多线程的问题

5.      多线程问题的解决

      互斥锁(互斥量)mutex

      1.定义互斥量  pthread_mutex_t

      2.初始化互斥量1   pthread_mutex_init

      3.互斥量操作置0 pthread_mutex_lock

                         判定互斥量0:阻塞

                                     1:置0 ,返回

                 置1  pthread_mutex_unlock

                          置1,返回

5.释放互斥量pthread_mutex_destroy

 

例子代码:

 

#include<stdio.h>

#include<pthread.h>

 

pthread_mutex_t  m;

 

void  display()

{

    pthread_mutex_lock(&m);

a++;

b++;

if(a!=b)

{

      printf(“%d!=%d”,a,b);

}

Pthread_mutex_unlock(&m);

}

 

void * r1(void *data)

{

      while(1)

      {

           display();

      }

}

 

void*  r2(void * data)

{

      while(1)

      {

         display();

      }

}

 

 

void  main()

{

      pthread_t t1,t2;

      pthread_mutex_init(&m,0);

     pthread_create(&t1,0,r1,0);

        pthread_create(&t2,0,r2,0);

        pthread_join(t1,(void**)0);

        pthread_join(t2,(void**)0);

        pthread_mutex_destroy(&m);

}

 

抱歉!评论已关闭.