Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41863
  • 博文数量: 7
  • 博客积分: 162
  • 博客等级: 入伍新兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-20 06:47
文章分类
文章存档

2011年(7)

我的朋友

分类: C/C++

2011-09-13 09:47:38

reader_writer2.c:
 
  1. #include <iostream>
  2. #include <vector>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <semaphore.h>

  10. #define SIZE 1000
  11. #define SECS 10

  12. using namespace std;

  13. vector<int> vec(SIZE);
  14. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  15. sem_t sem_empty, sem_full;

  16. // record the times to meet lock
  17. int read_lock_times = 0;
  18. int write_lock_times = 0;

  19. // record the times of reading or writing
  20. int read_num = 0;
  21. int write_num = 0;

  22. void sig_handler(int sig_num)
  23. {
  24.         printf("Meet %d read locks\n", read_lock_times);
  25.         printf("Meet %d write locks\n", write_lock_times);
  26.         printf("Read %d numbers\n", read_num);
  27.         printf("Write %d numbers\n", write_num);

  28.         exit(0);
  29. }

  30. void * read_thread(void * args)
  31. {
  32.         while(1)
  33.         {
  34.                 sem_wait(&sem_empty);

  35.                 if( pthread_mutex_trylock(&mutex) == EBUSY)
  36.                 {
  37.                         ++ read_lock_times;
  38.                         pthread_mutex_lock(&mutex);
  39.                 }

  40.                 if(vec.size() > 0)
  41.                 {
  42.                         vec.erase(vec.begin());
  43.                         ++ read_num;
  44.                 }

  45.                 pthread_mutex_unlock(&mutex);
  46.                 sem_post(&sem_full);
  47.         }
  48. }

  49. void * write_thread(void * args)
  50. {
  51.         while(1)
  52.         {
  53.                 sem_wait(&sem_full);

  54.                 if( pthread_mutex_trylock(&mutex) == EBUSY)
  55.                 {
  56.                         ++ write_lock_times;
  57.                         pthread_mutex_lock(&mutex);
  58.                 }

  59.                 vec.push_back(write_num);
  60.                 ++write_num;

  61.                 pthread_mutex_unlock(&mutex);

  62.                 sem_post(&sem_empty);
  63.         }
  64. }

  65. int main()
  66. {
  67.         // install the signal handler
  68.         struct sigaction action;
  69.         action.sa_handler = sig_handler;
  70.         sigaction(SIGALRM, &action, NULL);

  71.         alarm(SECS);

  72.         sem_init(&sem_empty, 0, 0);
  73.         sem_init(&sem_full, 0, SIZE);

  74.         pthread_t tid1, tid2;
  75.         pthread_create(&tid1, NULL, read_thread, NULL);
  76.         pthread_create(&tid2, NULL, write_thread, NULL);

  77.         pthread_join(tid1, NULL);
  78.         pthread_join(tid2, NULL);
  79. }

 

改动不大,就是把原来固定容量的数组改为了容量可变的 STL 容器 vector.

注意这里的 semaphore 同步机制是必须的,否则会因为写线程写的太快导致内存不够,std::bad_alloc.

命令 time ./reader_writer2 运行结果如下:

Meet 0 read locks
Meet 9 write locks
Read 316228 numbers
Write 317227 numbers

real 0m10.017s
user 0m0.764s
sys 0m8.709s

显然,速度比固定容量的数组慢了很多,差别应该是在 vector 的操作上,需要调整内存,维护成员指针等等。

如果只有一个线程执行读写操作(先读后写), 则 10 秒钟内可以读写 2000 多万次,多线程后性能只有原来的 15%.

阅读(2373) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~