Linux 多线程通信方面的问题

本文详细介绍了进程与线程的概念,重点讲解了多线程编程的基础知识,包括使用pthread库进行线程创建、退出和等待的方法,并通过示例代码演示了如何在C语言中实现多线程。此外,还探讨了线程间的资源共享问题,如互斥锁、信号灯及条件变量等同步机制。


一.进程与线程

进程通信:资源分配的最小单位

有独立的地址空间,十分耗内存。

 

线程通信:程序执行的最小单位

无独立的地址空间(多任务操作模式,多线程共享地址空间,节省开销)

          线程间有方便的通信机制(共享数据,有独立堆栈和局部变量

         

 

二.多线程API

1.pthread_create

函数的作用:创建一个线程

函数的原型:int pthread_create(pthread_t *thread,pthread_attr_t *attr, (void *)(*start_routine(void *),(void *)arg);

函数的参数:thread:线程的标识符

            attr:线程的属性,一般设为NULL

            start_routine:线程的执行函数

            arg:传入到线程执行函数的参数

返回 值:成功:0,出错:-1

头文 件:#include <pthread.h>

 

2.pthread_exit

函数的作用:线程的退出

函数的原型:void pthread_exit(void * retval)

 

3.pthread_join

函数的作用:等待线程的退出

函数的原型:pthread_join(pthread_t th,void ** thread_return)

函数的参数:th:线程的标识符

            thread_return:不为NULL时,存储线程结束时返回值

返回 值:成功:0  出错:<0

 

三.            多线程应用

1.thread_create.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>    
  2. #include <pthread.h>    
  3.      
  4. void *myThread1(void)    
  5. {    
  6.     int i;    
  7.     for (i=0; i<100; i++)    
  8.     {    
  9.         printf("This is the 1st pthread,created by zieckey.\n");    
  10.         sleep(1);//Let this thread to sleep 1 second,and then continue to run    
  11.     }    
  12. }    
  13. void *myThread2(void)    
  14. {    
  15.     int i;    
  16.     for (i=0; i<100; i++)    
  17.     {    
  18.         printf("This is the 2st pthread,created by zieckey.\n");    
  19.         sleep(1);    
  20.     }    
  21. }    
  22.      
  23. int main()    
  24. {    
  25.     int i=0, ret=0;    
  26.     pthread_t id1,id2;    
  27.         
  28.     ret = pthread_create(&id1, NULL, (void*)myThread1, NULL);    
  29.     if (ret)    
  30.     {    
  31.         printf("Create pthread error!\n");    
  32.         return 1;    
  33.     }    
  34.         
  35.     ret = pthread_create(&id2, NULL, (void*)myThread2, NULL);    
  36.     if (ret)    
  37.     {    
  38.         printf("Create pthread error!\n");    
  39.         return 1;    
  40.     }    
  41.      
  42.     pthread_join(id1, 0);    
  43.     pthread_join(id2, NULL);    
  44.         
  45.     return 0;    
  46. }    

2.thread_struct.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>    
  2. #include <pthread.h>    
  3. #include <unistd.h>    
  4. #include <stdlib.h>    
  5.      
  6. struct menber    
  7. {    
  8.     int a;    
  9.     char *s;    
  10. };    
  11.      
  12. void *create(void *arg)    
  13. {    
  14.     struct menber *temp;    
  15.     temp=(struct menber *)arg;    
  16.     printf("menber->a = %d  \n",temp->a);    
  17.     printf("menber->s = %s  \n",temp->s);    
  18.     return (void *)0;    
  19. }    
  20.      
  21. int main(int argc,char *argv[])    
  22. {    
  23.     pthread_t tidp;    
  24.     int error;    
  25.     struct menber *b;    
  26.     b=(struct menber *)malloc( sizeof(struct menber) );    
  27.     b->a = 4;    
  28.     b->s = "zieckey";    
  29.      
  30.     error = pthread_create(&tidp, NULL, create, (void *)b);    
  31.      
  32.     if( error )    
  33.     {    
  34.         printf("phread is not created...\n");    
  35.         return -1;    
  36.     }    
  37.     sleep(1);    
  38.     printf("pthread is created...\n");    
  39.     return 0;    
  40. }    



 

 

四.线程之间对资源的竞争:

1.互斥锁Mutex

    基本操作:

互斥锁初始化:pthread_mutex_init()

互斥锁上锁:pthread_mutex_lock()

互斥锁判断上锁:pthread_mutex_trylock()

互斥锁接锁:pthread_mutex_unlock()

消除互斥锁:pthread_mutex_destroy()

 

2.信号灯Semaphore

信号量初始化:sem_init

PV操作:

int sem_wait(sem_t*sem)   --P操作

            int sem_trywait(sem_t*sem)

            int sem_post(sem_t*sem)   --V操作

            intsem_getvalue(sem_t *sem)

   int sem_destroy(sem_t*sem)

 

3.条件变量Conditions

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值