Chinaunix首页 | 论坛 | 博客
  • 博客访问: 309967
  • 博文数量: 42
  • 博客积分: 2718
  • 博客等级: 少校
  • 技术积分: 467
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-19 10:23
个人简介

青 田 酒 店 服 务 小 姐 价 格 186-6531-7773

文章分类

全部博文(42)

文章存档

2011年(36)

2010年(6)

分类: C/C++

2011-07-08 11:17:26

总结了下,通过状态来实现线程工作切换的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. }
阅读(5088) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

网络安全服务2011-08-11 10:56:05

duanjigang2011-07-08 12:01:19

稍微加些修改
if (pt->status == status_over)
{
  sleep (1);
  continue;
}
少加了continue;
  
}