基于状态的1对N无锁多线程程序的一般结构

5128阅读 2评论2011-07-08 duanjigang
分类:C/C++

总结了下,通过状态来实现线程工作切换的1对N多线程模型(1个调度,多个工作的模型),一般的代码结构大致如下,与大家分享下,希望听到更好的建议和想法。
 
首先是状态数据结构
  1. enum thread_status
  2. {
  3.         status_free = 1,
  4.         status_ready = 2,
  5.         status_running = 3,
  6.         status_over = 4,
  7.         status_dead = 5
  8. };
其次是线程体结构(这个结构作为pthread_create时的参数)
  1. typedef struct
  2. {
  3.         pthread_t thread; //thread
  4.         unsigned char status; //status
  5.         unsigned int index;
  6. }thread_t;
最后是线程体(thread_func的一般代码结构)
初始化:
  1. for ( index = 0; index < pp->thread_num; index++)
  2.         {
  3.                 pp->thread_list[index].status = status_free;
  4.                 pp->thread_list[index].index = index;
  5.                 //create thread
  6.                 if (pthread_create (
  7.                         &(pp->thread_list[index].thread), //thread entry
  8.                         0, //attribute
  9.                         app_thread, //start function
  10.                         pp->thread_list + index))//parameter passed to thread function
  11.                 {
  12.                         printf ("create thread for thread %u failed\n", index);
  13.                         return -1;
  14.                 }
  15.         }

运行时:

 

  1. void * app_thread(void*data)
  2. {
  3.     thread_t * pt = (thread_t*)data;
  4.     pthread_detach (pthread_self());

  5.     while (pt->status != status_dead)
  6.     {
  7.         //when it is free, do nothing...
  8.         if (pt->status == status_free)
  9.         {
  10.             printf ("thread %u is free now\n", pt->index);
  11.             sleep (1);
  12.             continue;
  13.         }
  14.     
  15.         //ready means it ought to do something,free -> ready is changed by outter thread
  16.         //but not itself
  17.         if (pt->status == status_ready)
  18.         {
  19.             printf ("thread_%d: ready....go !!!\n");
  20.             pt->status = status_running;
  21.             continue;
  22.         }

  23.         //running, do something
  24.         if (pt->status == status_running)
  25.         {
  26.             while (do_some_thing())
  27.             {
  28.                 sleep (1);
  29.             }
  30.             //when everything is done, change status to status_over myself
  31.             pt->status = status_over;
  32.             continue;
  33.         }

  34.         //when i am running over, just waitting someone else to change the
  35.         //status to status_ready to run new tasks or status_dead to exit
  36.         if (pt->status == status_over)
  37.         {
  38.             sleep(1);
  39.         }

  40.         //it's time to say goodbye...
  41.         if (pt->status == status_dead)
  42.         {
  43.             printf ("good luck, i'm now exiting..bye\n");
  44.             break;
  45.         }
  46.     }
  47.     pthread_exit (0);
  48. }
上一篇:shell脚本之生成任务列表
下一篇:《UNIX编程艺术》节选

文章评论