Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1219331
  • 博文数量: 261
  • 博客积分: 4196
  • 博客等级: 上校
  • 技术积分: 3410
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-17 17:05
文章分类

全部博文(261)

文章存档

2018年(1)

2017年(22)

2016年(2)

2015年(8)

2014年(27)

2013年(40)

2012年(161)

分类: LINUX

2012-04-18 09:15:36

  1. form:luozhiyong131.blog.chinaunix.net

  2. /*
  3.  * 线程同步——信号量
  4.  * 主线程从键盘读入字符,新线程输出读入的字符
  5.  * Lzy 2011-6-19
  6.  */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <pthread.h>
  10. #include <semaphore.h>                //信号量头文件
  11. #include <string.h>

  12. sem_t sem;                            //定义信号量
  13. char buf[256];

  14. void *mypthread(void *arg)            //新线程运行函数
  15. {
  16.     sem_wait(&sem);                    //阻塞当前线程
  17.     while(strncmp(buf, "exit", 4) != 0)
  18.     {
  19.         printf("接收字符:");
  20.         puts(buf);
  21.         sem_wait(&sem);                //阻塞当前线程
  22.     }
  23.     pthread_exit(NULL);                //线程退出
  24. }

  25. int main(void)
  26. {
  27.     pthread_t tid;                        //定义线程标识符
  28.     sem_init(&sem, 0,0);                //信号量初始化    
  29.     pthread_create(&tid, NULL, mypthread, NULL);    //创建新线程    
  30.     
  31.     while(strncmp(buf, "exit", 4) != 0)
  32.     {
  33.         printf("输入字符:");
  34.         gets(buf);
  35.         sem_post(&sem);            //激活阻塞在信号量上的线程
  36.     }

  37.     pthread_exit(NULL);            //等待线程结束
  38.     sem_destroy($sem);            //注销信号量
  39.     
  40.     return 0;
  41. }
  42. =====================================================================================
    1. /*
    2.  * 线程同步——互斥量
    3.  * 创建两个线程,使用互斥量使任一时刻只有一个线程对全局变量进行

    4. 操作
    5.  * Lzy 2011-6-19
    6.  */
    7. #include <stdio.h>
    8. #include <stdlib.h>
    9. #include <pthread.h>
    10. pthread_mutex_t mutex;                    /* 定义

    11. 互斥量 */
    12. int x;                                

    13.     /* 定义全局变量 */
    14. void thread1(void)                /* 定义线程1运

    15. 行的函数,其功能是对全局变量x进行逐减操作 */
    16. {
    17.   while(x>0)
    18.   {
    19.     pthread_mutex_lock(&mutex);            /* 对互斥量进行

    20. 加锁操作 */
    21.     printf("Thread 1 is running : x=%d \n",x);
    22.     x--;
    23.     pthread_mutex_unlock(&mutex);        /* 对互斥量进行

    24. 解锁操作 */
    25.     sleep(1);
    26.   }
    27.   pthread_exit(NULL);
    28. }
    29. void thread2(void)                /* 定义线程2运

    30. 行的函数,功能与thread2相同 */
    31. {
    32.   while(x>0)
    33.   {
    34.     pthread_mutex_lock(&mutex);            /* 对互斥量进行

    35. 加锁操作 */
    36.     printf("Thread 2 is running : x=%d \n",x);
    37.     x--;
    38.     pthread_mutex_unlock(&mutex);        /* 对互斥量进行

    39. 解锁操作 */
    40.     sleep(1);
    41.   }
    42.   pthread_exit(NULL);
    43. }
    44. int main(void)
    45. {
    46.   pthread_t id1,id2;                        

    47. /* 定义线程的标识符 */
    48.   int ret;
    49.   ret = pthread_mutex_init(&mutex,NULL);    /* 对互斥量进行

    50. 初始化,这里使用默认的属性 */
    51.   if(ret != 0)
    52.   {
    53.     printf ("Mutex initialization failed.\n");        /* 如果

    54. 初始化失败,打印错误信息 */
    55.     exit (1);
    56.   }
    57.   x=10;                                

    58. /* 对全局变量赋初值 */
    59.   ret = pthread_create(&id1, NULL, (void *)&thread1, NULL);    

    60.     /* 创建线程1 */
    61.   if(ret != 0)
    62.   {
    63.     printf ("Thread1 creation failed.\n");
    64.     exit (1);
    65.   }
    66.   ret = pthread_create(&id2, NULL, (void *)&thread2, NULL);    

    67.     /* 创建线程2 */
    68.   if(ret != 0)
    69.   {
    70.     printf ("Thread2 creation failed.\n");
    71.     exit (1);
    72.   }
    73.   pthread_join(id1, NULL);                /*线程

    74. 合并 */
    75.   pthread_join(id2, NULL);
    76.   return (0);
    77. }
    1. /*
    2.  * 线程同步
    3.  * ——读写锁
    4.  *     只要没有进程持有某个给定的读写锁用于写,那么任意数目的

    5. 线程都可持有该读写锁用于读
    6.  *     仅当没有线程持有某个给定的读写锁用于读或写,才能分配该

    7. 读写锁用于写。
    8.  * Lzy 2011-6-19
    9.  */

    10. #include <stdio.h>
    11. #include <stdlib.h>
    12. #include <pthread.h>

    13. int product = 0;        //定义全局变量

    14. pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;    //静态

    15. 初始化读写锁    

    16. void * threadRead(void * arg)            //线程函数读
    17. {
    18.     int cnt = 0;
    19.     while(cnt++ < 100)
    20.     {
    21.         pthread_rwlock_rdlock(&rwlock);    //读锁
    22.         
    23.         printf("Read: product = %d\n", product);
    24.         
    25.         pthread_rwlock_unlock(&rwlock);    //解锁
    26.         sleep(1);
    27.     }
    28. }

    29. void * tidProduce(void * arg)    //线程函数写 加1
    30. {
    31.     int cnt = 0;
    32.     while(cnt++ < 100)
    33.     {
    34.         pthread_rwlock_wrlock(&rwlock);    //写锁
    35.         
    36.         product++;
    37.         printf("Produce: product = %d\n", product);
    38.         
    39.         pthread_rwlock_unlock(&rwlock);    //解锁
    40.         sleep(1);
    41.     }
    42. }

    43. void * threadConsume(void * arg)    //线程函数写 减1
    44. {
    45.     int cnt = 0;
    46.     while(cnt++ < 100)
    47.     {
    48.         pthread_rwlock_wrlock(&rwlock);    //写锁
    49.         
    50.         product--;
    51.         printf("Consume: product = %d\n", product);
    52.         
    53.         pthread_rwlock_unlock(&rwlock);    //解锁
    54.         sleep(2);
    55.     }
    56. }

    57. int main(void)
    58. {
    59.     int i;
    60.     pthread_t tid[10], tidconsume, tidproduce;
    61.     
    62.     for(i = 0; i < 2; i++)
    63.     {
    64.         if(pthread_create(&tid[i], NULL, threadRead,

    65. NULL))
    66.         {
    67.             printf("pthread_create error\n");
    68.             exit(0);
    69.         }
    70.     }
    71.     if(pthread_create(&tidproduce, NULL, tidProduce, NULL))
    72.     {
    73.         printf("pthread_create error\n");
    74.         exit(0);
    75.     }

    76.     if(pthread_create(&tidconsume, NULL, threadConsume,

    77. NULL))
    78.     {
    79.         printf("pthread_create error\n");
    80.         exit(0);
    81.     }

    82.     pthread_exit(NULL);        //等待所有线程结束

    83.     return 0;
    84. }
阅读(1416) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~