资讯专栏INFORMATION COLUMN

理解线程3 c语言示例线程基本操作

A Loity / 729人阅读

摘要:基本线程的动作继续之前语言线程的文章文章文章来了解基本的线程操作。属性用完后对其进行清理回收通过共享的变量来检测子线程是否已经结束代码如下设置调度属性线程库提供以下调度策略先进先出调度。

基本线程的动作

继续之前C语言线程的文章:文章1 文章2 来了解基本的线程操作。

设置线程属性 设置脱离状态

下面代码中关键的地方在于:

通过 res = pthread_attr_init(&thread_attr); 初始化一个线程属性

通过 res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); 将属性设置为脱离状态(PTHREAD_CREATE_DETACHED),即不能通过调用 pthread_join 来获得另一个线程的退出状态

另外还有一个常用的默认状态是 PTHREAD_CREATE_JOINABLE ,可以允许两个线程重新合并。

属性用完后对其进行清理回收 (void)pthread_attr_destroy(&thread_attr);

通过共享的变量 thread_finished 来检测子线程是否已经结束

代码如下:

</>复制代码

  1. #include
  2. #include
  3. #include
  4. #include
  5. void *thread_function(void *arg);
  6. char message[] = "Hello World";
  7. int thread_finished = 0;
  8. int main() {
  9. int res;
  10. pthread_t a_thread;
  11. pthread_attr_t thread_attr;
  12. res = pthread_attr_init(&thread_attr);
  13. if (res != 0) {
  14. perror("Attribute creation failed");
  15. exit(EXIT_FAILURE);
  16. }
  17. res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
  18. if (res != 0) {
  19. perror("Setting detached attribute failed");
  20. exit(EXIT_FAILURE);
  21. }
  22. res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);
  23. if (res != 0) {
  24. perror("Thread creation failed");
  25. exit(EXIT_FAILURE);
  26. }
  27. (void)pthread_attr_destroy(&thread_attr);
  28. while(!thread_finished) {
  29. printf("Waiting for thread to say it"s finished...
  30. ");
  31. sleep(1);
  32. }
  33. printf("Other thread finished, bye!
  34. ");
  35. exit(EXIT_SUCCESS);
  36. }
  37. void *thread_function(void *arg){
  38. printf("thread_function is running. Argument was %s
  39. ", (char *)arg);
  40. sleep(4);
  41. printf("Second thread setting finished flag, and exiting now
  42. ");
  43. thread_finished = 1;
  44. pthread_exit(NULL);
  45. }
设置调度属性

线程库提供以下调度策略:

</>复制代码

  1. | SCHED_FIFO | 先进先出 (FIFO) 调度。每个线程都有一个固定的优先级;当多个线程具有相同的优先级时,它们按照先进先出 (FIFO) 的顺序运行直到完成 |
  2. | SCHED_RR | 循环 (RR) 调度。每个线程都有固定的优先级;当多个线程具有相同的优先级时,它们按照先进先出 (FIFO) 的顺序在一个 固定的时间片内运行。 |
  3. | SCHED_OTHER | 缺省的 AIX® 调度。每个线程都有一个由调度程序根据线程的活动动态修改的初始优先级;线程的执行是按时间分割的。在其他系统上,这个调度策略可能会不同。 |

设置调度属性和设置很相似:

</>复制代码

  1. #include
  2. #include
  3. #include
  4. #include
  5. void *thread_function(void *arg);
  6. char message[] = "Hello World";
  7. int thread_finished = 0;
  8. int main() {
  9. int res;
  10. pthread_t a_thread;
  11. pthread_attr_t thread_attr;
  12. int max_priority;
  13. int min_priority;
  14. struct sched_param scheduling_value;
  15. res = pthread_attr_init(&thread_attr);
  16. if (res != 0) {
  17. perror("Attribute creation failed");
  18. exit(EXIT_FAILURE);
  19. }
  20. res = pthread_attr_setschedpolicy(&thread_attr, SCHED_OTHER);
  21. if (res != 0) {
  22. perror("Setting schedpolicy failed");
  23. exit(EXIT_FAILURE);
  24. }
  25. res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
  26. if (res != 0) {
  27. perror("Setting detached attribute failed");
  28. exit(EXIT_FAILURE);
  29. }
  30. res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);
  31. if (res != 0) {
  32. perror("Thread creation failed");
  33. exit(EXIT_FAILURE);
  34. }
  35. max_priority = sched_get_priority_max(SCHED_OTHER);
  36. min_priority = sched_get_priority_min(SCHED_OTHER);
  37. scheduling_value.sched_priority = min_priority;
  38. res = pthread_attr_setschedparam(&thread_attr, &scheduling_value);
  39. if (res != 0) {
  40. perror("Setting schedpolicy failed");
  41. exit(EXIT_FAILURE);
  42. }
  43. (void)pthread_attr_destroy(&thread_attr);
  44. while(!thread_finished) {
  45. printf("Waiting for thread to say it"s finished...
  46. ");
  47. sleep(1);
  48. }
  49. printf("Other thread finished, bye!
  50. ");
  51. exit(EXIT_SUCCESS);
  52. }
  53. void *thread_function(void *arg) {
  54. printf("thread_function is running. Argument was %s
  55. ", (char *)arg);
  56. sleep(4);
  57. printf("Second thread setting finished flag, and exiting now
  58. ");
  59. thread_finished = 1;
  60. pthread_exit(NULL);
  61. }
取消线程

通过 int pthread_cancel(pthread_t thread); 来请求一个线程终止

通过 int pthread_setcancelstate(int state, int *oldstate) 来设置接受的进程是允许取消请求还是忽略它

通过 int pthread_setcanceltype(int type, int *oldtype) 来设置取消类型, PTHREAD_CANCEL_ASYCHRONOUS 代表接收到取消请求后立即行动, THREAD_CANCEL_DEFERRED 表示在接收到请求后,等待函数执行下述动作之一后才取消线程: pthread_join, pthread_cond_wait, pthread_cond_timeout, pthread_test_cancel, sem_wait, sigwait

代码如下:

</>复制代码

  1. #include
  2. #include
  3. #include
  4. #include
  5. void *thread_function(void *arg);
  6. int main () {
  7. int res;
  8. pthread_t a_thread;
  9. void *thread_result;
  10. res = pthread_create(&a_thread, NULL, thread_function, NULL);
  11. if (res != 0){
  12. perror("Thread creation failed");
  13. exit(EXIT_FAILURE);
  14. }
  15. sleep(3);
  16. printf("Caceling thread...
  17. ");
  18. res = pthread_cancel(a_thread);
  19. if (res != 0){
  20. perror("Thread cancelation failed");
  21. exit(EXIT_FAILURE);
  22. }
  23. printf("Waiting for thread to finish...
  24. ");
  25. res = pthread_join(a_thread, &thread_result);
  26. if (res != 0) {
  27. perror("Thread join failed");
  28. exit(EXIT_FAILURE);
  29. }
  30. exit(EXIT_SUCCESS);
  31. }
  32. void *thread_function(void *arg) {
  33. int i, res;
  34. res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  35. if (res != 0) {
  36. perror("Thread pthread_setcalcelstate failed");
  37. exit(EXIT_FAILURE);
  38. }
  39. res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  40. if (res != 0) {
  41. perror("Thread pthread_setcanceltype failed");
  42. exit(EXIT_FAILURE);
  43. }
  44. printf("thread_function is running
  45. ");
  46. for(i=0; i<10; i++) {
  47. printf("Thread is still running (%d)...
  48. ", i);
  49. sleep(1);
  50. }
  51. pthread_exit(0);
  52. }
主线程创建多个线程示例

代码如下:

</>复制代码

  1. #include
  2. #include
  3. #include
  4. #include
  5. #define NUM_THREADS 6
  6. void *thread_function(void *arg);
  7. int main() {
  8. int res;
  9. pthread_t a_thread[NUM_THREADS];
  10. void *thread_result;
  11. int lots_of_threads;
  12. for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) {
  13. res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function, (void *)&lots_of_threads);
  14. if (res != 0) {
  15. perror("Thread creation failed");
  16. exit(EXIT_FAILURE);
  17. }
  18. sleep(1);
  19. }
  20. printf("Waiting for threads to finish...
  21. ");
  22. for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--) {
  23. res = pthread_join(a_thread[lots_of_threads], &thread_result);
  24. if (res == 0) {
  25. printf("Picked up a thread
  26. ");
  27. }
  28. else {
  29. perror("pthread_join failed");
  30. }
  31. }
  32. printf("All done
  33. ");
  34. exit(EXIT_SUCCESS);
  35. }
  36. void *thread_function(void *arg) {
  37. int my_number = *(int *)arg;
  38. int rand_num;
  39. printf("thread_function is running. Argument was %d
  40. ", my_number);
  41. rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0));
  42. sleep(rand_num);
  43. printf("Bye from %d
  44. ", my_number);
  45. pthread_exit(NULL);
  46. }

运行结果如下:

</>复制代码

  1. thread_function is running. Argument was 0
  2. Bye from 0
  3. thread_function is running. Argument was 1
  4. thread_function is running. Argument was 2
  5. Bye from 1
  6. thread_function is running. Argument was 3
  7. thread_function is running. Argument was 4
  8. thread_function is running. Argument was 5
  9. Waiting for threads to finish...
  10. Bye from 5
  11. Picked up a thread
  12. Bye from 3
  13. Bye from 2
  14. Bye from 4
  15. Picked up a thread
  16. Picked up a thread
  17. Picked up a thread
  18. Picked up a thread
  19. Picked up a thread
  20. All done
了解更多

Posix多线程编程—线程属性

参考资料

《Linux 程序设计》

http://www.ibm.com/support/kn...

PS

不得不承认,我失败了。曾计划每天分享一篇python相关知识点但没有做到。失败的原因想找可以找很多比如最近在做一个小的项目、这几天出去聚会没有时间、工作出现问题加班到比较晚等等,然而总结起来不外乎在心里它的重要程度是怎么样的。我反思了下几乎做到一天一篇的这一个月过程做出改变,不再要求一天一篇,而是当我有好的素材要分享时才分享,这样与我与大家都是一件好事,我可以动态调度自己的精力和注意力,比如最近实现了一半的一个odoo项目依赖关系分析器可以尽快把它做完,对于读者来说也可以减少干扰看到更好的分享而不是像我之前有几篇那样划水的。但每周应该会有3-4篇Python的知识可以分享。另外我目前的工作是基于Odoo的,我计划尝试做一些基础的教程来分享这个我熟悉的框架,如果有进展一定会告知感兴趣的读者。感谢读者。

最后向漩涡鸣人致敬,朝他的“说到做到,这就是我的忍道”努力。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/38241.html

相关文章

  • 理解线程1 C语言示例的程序

    摘要:一个简单的语言实现的线程示例在看时,为了更好的理解线程的概念,书中列举了这样一个小例子将程序编译链接后运行,可以看到下面这样的结果这里使用创建新线程,的定义如下根据要求,只有一个指向的指针作为参数,返回的也是指向的指针。 一个简单的C语言实现的线程示例 在看《Beginning Linux Programming》时,为了更好的理解线程的概念,书中列举了这样一个小例子: #inclu...

    lncwwn 评论0 收藏0
  • [Java并发-2]Java如何解决可见性问题的

    摘要:诞生之处就支持多线程,所以自然有解决这些问题的办法,而且在编程语言领域处于领先地位。,线程规则这条是关于线程启动的。在语言里面,的语义本质上是一种可见性,意味着事件对事件来说是可见的,无论事件和事件是否发生在同一个线程里。 之前我们说了:1,可见性2,原子性3,有序性3个并发BUG的之源,这三个也是编程领域的共性问题。Java诞生之处就支持多线程,所以自然有解决这些问题的办法,而且在编...

    lk20150415 评论0 收藏0
  • JS高级入门教程

    摘要:解析首先简称是由欧洲计算机制造商协会制定的标准化脚本程序设计语言。级在年月份成为的提议,由核心与两个模块组成。通过引入统一方式载入和保存文档和文档验证方法对进行进一步扩展。其中表示的标记位正好是低三位都是。但提案被拒绝了。 JS高级入门教程 目录 本文章定位及介绍 JavaScript与ECMAScript的关系 DOM的本质及DOM级介绍 JS代码特性 基本类型与引用类型 JS的垃...

    zsy888 评论0 收藏0
  • 双重检查锁定与延迟初始化

    摘要:基于的双重检查锁定的解决方案对于前面的基于双重检查锁定来实现延迟初始化的方案指示例代码,我们只需要做一点小的修改把声明为型,就可以实现线程安全的延迟初始化。 双重检查锁定的由来 在java程序中,有时候可能需要推迟一些高开销的对象初始化操作,并且只有在使用这些对象时才进行初始化。此时程序员可能会采用延迟初始化。但要正确实现线程安全的延迟初始化需要一些技巧,否则很容易出现问题。比如,下...

    yvonne 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<