千家信息网

unix 多线程控制应用

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,1.线程三个属性的学习绑定分离优先级thread.c#include"thread.h"#include#include#include#include#includeextern int statu
千家信息网最后更新 2025年02月01日unix 多线程控制应用

1.线程三个属性的学习

绑定

分离

优先级


thread.c



  1. #include"thread.h"
  2. #include<stdio.h>
  3. #include<unistd.h>
  4. #include<pthread.h>
  5. #include<stdbool.h>
  6. #include<stdlib.h>
  7. extern int status_1;
  8. extern int status_2;
  9. void thread_0(char*pstr)//带参数
  10. {
  11. int counter = 10;
  12. while(counter--)
  13. {
  14. sleep(1);
  15. printf("%s\n",pstr);
  16. }
  17. pthread_exit(&status_1);//线程退出专用函数
  18. }
  19. void thread_1(void)//不带参数
  20. {
  21. int counter = 10;
  22. while(counter--)
  23. {
  24. sleep(1);
  25. printf("thread_1\n");
  26. }
  27. pthread_exit(&status_2);//线程退出专用函数
  28. }
  29. bool _creat_thread(pthread_t *thread, void *(*start_routine)(void *), void *arg, int SCOPE, int DETACH, int policy, int priority_val)
  30. {
  31. struct sched_param param;
  32. pthread_attr_t attr;
  33. if(0 != pthread_attr_init(&attr))//初始化 结构体attr, 如果不需要attr需要取消attr, pthread_attr_destroy
  34. {
  35. perror("pthread_attr_init\n");
  36. return false;
  37. }
  38. if(0 != pthread_attr_setscope(&attr, SCOPE))//设置线程属性 是否绑定 绑定(PTHREAD_SCOPE_SYSTEM) 非绑定(PTHREAD_SCOPE_PROCESS)
  39. {
  40. perror("pthread_attr_setscope\n");
  41. return false;
  42. }
  43. if(0 != pthread_attr_setdetachstate(&attr, DETACH))//设置线程属性 是否分离 分离(PTHREAD_CREATE_DETACHED) 非分离(PTHREAD_CREATE_JOINABLE)
  44. {
  45. perror("pthread_attr_setdetachstate\n");
  46. return false;
  47. }
  48. if(0 != pthread_attr_setschedpolicy(&attr, policy))//三种优先级策略选择 : SCHED_FIFO(值1-99), SCHED_RR(值1-99), and SCHED_OTHER
  49. {
  50. perror("pead_attr_setschedpolicy\n");
  51. return false;
  52. }
  53. if(priority_val>0 && priority_val<100)//判断优先级值是否在1-99范围
  54. {
  55. param.sched_priority = priority_val;
  56. }
  57. else
  58. {
  59. perror("priority_val_ wrong value range!!\n");
  60. }
  61. if(0 != pthread_attr_setschedparam(&attr, ¶m))//设置设置线程属性 优先级 通过 策略与值来判断如何调度
  62. {
  63. perror("pthread_attr_setschedparam\n");
  64. return false;
  65. }
  66. if(0 != pthread_create(thread, &attr, (void*)start_routine,arg))// 建立一个含有以上属性的线程
  67. {
  68. perror("pthread_create\n");
  69. return false;
  70. }
  71. if(0 != pthread_attr_destroy(&attr))//使用完后取消attr,
  72. {
  73. perror("pthread_attr_destroy\n");
  74. return false;
  75. }
  76. return true;
  77. }



thread.h




  1. #ifndef THREAD_H
  2. #define THREAD_H
  3. #include<stdio.h>
  4. #include<pthread.h>
  5. #include<stdbool.h>
  6. void thread_0(char*);
  7. void thread_1(void);
  8. bool _creat_thread(pthread_t *,void *(*)(void *), void *, int, int, int, int);
  9. #endif //end THREAD_H

main.c


  1. #include"thread.h"
  2. #include<pthread.h>
  3. #include<unistd.h>
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<stdbool.h>
  7. #define LOW_PRIO 1
  8. #define HIGH_PRIO 2
  9. int status_0 = 0;
  10. int status_1 = 1;
  11. int status_2 = 2;
  12. int main(void)
  13. {
  14. bool ret;
  15. char *str = "thread_0\n";
  16. pthread_t thd0,thd1;
  17. ret = _creat_thread(&thd0, (void*)thread_0, str, PTHREAD_SCOPE_SYSTEM, PTHREAD_CREATE_JOINABLE, SCHED_FIFO, LOW_PRIO);// 创建一个线程 带参数级相应属性
  18. if(ret)
  19. {
  20. printf("create thread successfully!\n");
  21. }
  22. else
  23. {
  24. perror("fail to create thread!\n");
  25. exit(1);
  26. }
  27. ret = _creat_thread(&thd1, (void*)thread_1, NULL, PTHREAD_SCOPE_SYSTEM, PTHREAD_CREATE_JOINABLE, SCHED_FIFO, HIGH_PRIO);// 创建一个线程 不带参数,含相应属性
  28. if(ret)
  29. {
  30. printf("create thread successfully!\n");
  31. }
  32. else
  33. {
  34. perror("fail to create thread!\n");
  35. exit(1);
  36. }
  37. int * thd_exit_status = NULL ;
  38. while(1)
  39. {
  40. sleep(1);
  41. printf("main\n");
  42. if(0 != pthread_join(thd0,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and the thd_exit_status = NULL
  43. {
  44. printf("thread_0 is not exist!!\n");
  45. //exit(1);
  46. }
  47. else
  48. {
  49. if(NULL== thd_exit_status && 1 != *thd_exit_status)
  50. {
  51. printf("pthd0 is runing\n");
  52. }
  53. else if(NULL!= thd_exit_status && 1 == *thd_exit_status)
  54. {
  55. printf("pthd0 has been terminated and return status:%d\n", *thd_exit_status);
  56. }
  57. }
  58. if(0 != pthread_join(thd1,(void**)&thd_exit_status))//if th0 no more exist, pthread_join return erro and the thd_exit_status = NULL
  59. {
  60. printf("thread_1 is not exist!!\n");
  61. //exit(1);
  62. }
  63. else
  64. {
  65. if(NULL == thd_exit_status && 2 != *thd_exit_status)
  66. {
  67. printf("pthd_1 is runing\n");
  68. }
  69. else if(NULL!= thd_exit_status && 2 == *thd_exit_status)
  70. {
  71. printf("pthd_1 has been terminated and return status:%d\n", *thd_exit_status);
  72. }
  73. }
  74. }
  75. return 0;
  76. }
附件:http://down.51cto.com/data/2362156
0