设为首页 加入收藏

TOP

6.8 扩展线程接口类(2)
2013-10-07 13:00:23 来源: 作者: 【 】 浏览:59
Tags:6.8 扩展 线程 接口

6.8  扩展线程接口类(2)

在程序清单6-3中,第3行~第10行定义的构造函数为SchedAttr这个类初始化一个线程属性对象。它将inheritsched属性设置为PTHREAD_EXPLICIT_SCHED,这样使用这个属性的对象创建的线程负责定义它的调度策略和优先级,而不是从创建者线程继承调度策略和优先级。默认情况下,线程的状态是JOINABLE。其他方法的含义一看便知:

  1. setPriority(int Priority)  
  2. setSchedPolicy(int Policy)  
  3. setContentionscope(int Scope)  
  4. setDetached()  
  5. setJoinable() 

在第20行调用pthread_join( )之前,使用join( )检查线程是不是可结合的。当在第78行中创建线程时,pthread_create( )使用SchedAttr对象:

  1. pthread_create(&Tid, &SchedAttr, thread,this); 

程序清单6-4显示了filter_thread的定义。

程序清单6-4

  1. //Listing 6-4  A definition of the filter_thread class.  
  2.                 
  3. 1   #include "thread_object.h"  
  4. 2  
  5. 3  
  6. 4   filter_thread::filter_thread(void)  
  7. 5   {  
  8. 6      pthread_attr_init(&SchedAttr);  
  9. 7  
  10. 8  
  11. 9   }  
  12. 10  
  13. 11  
  14. 12  filter_thread::~filter_thread(void)  
  15. 13  {  
  16. 14  
  17. 15  }  
  18. 16  
  19. 17  void filter_thread::do_something(void)  
  20. 18  {  
  21. 19     struct sched_param Param;  
  22. 20     int Policy;  
  23. 21     pthread_t thread_id = pthread_self();  
  24. 22     string Schedule;  
  25. 23     string State;  
  26. 24     string Scope;  
  27. 25  
  28. 26     pthread_getschedparam(thread_id,&Policy,&Param);  
  29. 27     if(NewPolicy == SCHED_RR){Schedule.assign("RR");}  
  30. 28     if(NewPolicy == SCHED_FIFO){Schedule.assign("FIFO");}  
  31. 29     if(NewPolicy == SCHED_OTHER){Schedule.assign("OTHER");}  
  32. 30     if(NewState == PTHREAD_CREATE_DETACHED){State.assign("DETACHED");}  
  33. 31     if(NewState == PTHREAD_CREATE_JOINABLE){State.assign("JOINABLE");}  
  34. 32     if(NewScope == PTHREAD_SCOPE_PROCESS){Scope.assign("PROCESS");}  
  35. 33     if(NewScope == PTHREAD_SCOPE_SYSTEM){Scope.assign("SYSTEM");}  
  36. 34     cout << Name << ":" << thread_id << endl 
  37. 35          << "----------------------" << endl 
  38. 36          << " priority:  "<< Param.sched_priority    << endl 
  39. 37          << " policy:    "<< Schedule               
    << endl 
  40. 38          << " state:     "<< State                               << endl 
  41. 39          << " scope:         "<< Scope            
    << endl << endl;  
  42. 40  
  43. 41  }  
  44. 42 

在程序清单6-4中,第4行~第9行的filter_thread构造函数使用线程属性对象SchedAttr进行初始化。定义了do_something( )方法。在filter_thread中,这个方法将如下线程信息发送到cout:

线程名称

线程id

优先级

调度策略

状态

范围

有些值可能未被初始化,因为它们不是在属性对象中设置的。下一章将会对这个方法进行重新定义。

现在可以创建多个filter_thread对象,每个对象可以设置线程的属性。程序清单6-5显示了如何创建多个filter_thread对象。

程序清单6-5

  1. //Listing 6-5 is main line to create multiple filter_thread objects.  
  2.                 
  3.  1  #include "thread_object.h"  
  4.  2  #include <unistd.h> 
  5.  3  
  6.  4  
  7.  5  int main(int argc,char *argv[])  
  8.  6  {  
  9.  7     filter_thread  MyThread[4];  
  10.  8  
  11.  9     MyThread[0].name("Proteus");  
  12. 10     MyThread[0].setSchedPolicy(2);  
  13. 11     MyThread[0].setPriority(7);  
  14. 12     MyThread[0].setDetached();  
  15. 13  
  16. 14     MyThread[1].name("Stand Alone Complex");  
  17. 15     MyThread[1].setContentionScope(1);  
  18. 16     MyThread[1].setPriority(5);  
  19. 17     MyThread[1].setSchedPolicy(2);  
  20. 18  
  21. 19     MyThread[2].name("Krell Space");  
  22. 20     MyThread[2].setPriority(3);  
  23. 21  
  24. 22     MyThread[3].name("Cylon Space");  
  25. 23     MyThread[3].setPriority(2);  
  26. 24     MyThread[3].setSchedPolicy(2);  
  27. 25  
  28. 26     for(int N = 0;N < 4;N++)  
  29. 27     {  
  30. 28        MyThread[N].run();  
  31. 29        MyThread[N].join();  
  32. 30     }  
  33. 31     return (0);  
  34. 32  } 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇6.8 扩展线程接口类(3) 下一篇6.8 扩展线程接口类(1)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: