LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 612|回复: 9

关于线程的基本问题

[复制链接]
发表于 2004-8-2 16:44:06 | 显示全部楼层 |阅读模式
一个进程产生了一些线程以后,那么这个主进程和线程之间是什么关系?怎样联系?

请看下列这段代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>

  4. void task1(int *counter);
  5. void task2(int *counter);
  6. void cleanup(int counter1, int counter2);

  7. int g1 = 0;
  8. int g2 = 0;

  9. int main(int argc, char *argv[])
  10. {
  11.         pthread_t thrd1, thrd2;
  12.         int ret;

  13.         ret = pthread_create(&thrd1, NULL, (void *)task1, (void *)&g1);
  14.         if (ret) {
  15.                 perror("pthread_create: task1");
  16.                 exit(EXIT_FAILURE);
  17.         }
  18.         ret = pthread_create(&thrd2, NULL, (void *)task2, (void *)&g2);
  19.         if (ret) {
  20.                 perror("pthread_create: task2");
  21.                 exit(EXIT_FAILURE);
  22.         }

  23.         pthread_join(thrd2, NULL);
  24.         pthread_join(thrd1, NULL);

  25.         cleanup(g1, g2);

  26.         exit(EXIT_SUCCESS);
  27. }

  28. void task1(int *counter)
  29. {
  30.         while (*counter < 5) {
  31.                 printf("task1 count: %d\n", *counter);
  32.                 (*counter)++;
  33.                 sleep(1);
  34.         }
  35. }

  36. void task2(int *counter)
  37. {
  38.         while (*counter < 5) {
  39.                 printf("task2 count: %d\n", *counter);
  40.                 (*counter)++;
  41.         }
  42. }

  43. void cleanup(int counter1, int counter2)
  44. {
  45.         printf("total iterations: %d\n", counter1 + counter2);
  46. }
复制代码


其中调用了两次pthread_join使指定的线程挂起。因此可以看到线程一执行完后,线程2才开始执行。但如果我去掉这两句,那么线程1(执行task1的线程)输出一行后,便是线程2输出。通过多次的运行,线程2的输出次数不定。而线程一肯定是只输出一次,并且一定是在第一行输出。那么请问这种现象是怎么回事?
发表于 2004-8-2 18:08:36 | 显示全部楼层
pthread_join不是挂起指定的线程,而是调用线程挂起以等待指定的线程执行结束
当有这两句时,我这里输出结果是
task1 count: 0
task2 count: 0
task2 count: 1
task2 count: 2
task2 count: 3
task2 count: 4
task1 count: 1
task1 count: 2
task1 count: 3
task1 count: 4
total iterations: 10
因为线程1输出后调用了sleep(1)睡了一秒
而线程2连续输出,所以出现上述情况

而当没有这两句时,我这里输出是
task1 count: 0
task2 count: 0
task2 count: 1
task2 count: 2
task2 count: 3
task2 count: 4
total iterations: 6
这是因为主线程不等待子线程执行结束就计算total并退出了
在主线程退出前,线程1没有来得及输出后四次(因为sleep)
发表于 2004-8-2 18:12:50 | 显示全部楼层
你那里线程2的输出次数不定是因为线程2有时来得及而有时来不及完成任务就被迫终止了
这正体现了进程调度的不确定性
 楼主| 发表于 2004-8-2 18:49:39 | 显示全部楼层
哦!去掉两个语句的时候没竟然没注意task1的sleep语句。谢谢!
我还有一点不明白,当第一次调用pthread_join的时候,线程1(执行task1的线程)被挂起,线程2执行完后线程1继续执行。代码明明是在主进程中,为什么挂起的是线程1?如果我有三个以上线程时执行这个调用,那么它挂起哪个线程?

PS:不好意思,家里没有装linux,所以无法自己去检验
发表于 2004-8-3 09:04:45 | 显示全部楼层
挂起的不是线程1, 是主线程
发表于 2004-8-3 09:06:02 | 显示全部楼层
挂起的不是线程1, 是主线程

学编程一定要找地方上机,只看书会留下一些错误的印象
发表于 2004-8-3 10:00:26 | 显示全部楼层
呵呵,看书仔细也就没问题了。明明man写的是thread_join  suspends the execution of the calling thread until ...知道calling thread什么意思的话,怎么也不会理解错了啊。
 楼主| 发表于 2004-8-3 10:15:24 | 显示全部楼层
最初由 doubleelec 发表
挂起的不是线程1, 是主线程


原始进程就成了主线程,同时它也是线程1?要不为什么挂起主线程的时候线程1被延迟执行
发表于 2004-8-3 10:43:47 | 显示全部楼层
你说的线程1指什么?
现在有三个线程:main, task1, task2
在main中等待task1和task2执行结束后才cleanup(g1, g2);
task1被延迟是因为sleep(1)
 楼主| 发表于 2004-8-3 10:58:10 | 显示全部楼层
哦,明白,谢谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表