免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6060 | 回复: 18
打印 上一主题 下一主题

[FreeBSD] 这个简单的多线程程序有问题么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-21 12:12 |只看该作者 |倒序浏览
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>


void *smp_line(void *args)
{
   char *s = (char *)args;
   int i;

   pthread_detach(pthread_self());
   for ( ; ; )
      printf("%s", s);

   return NULL;

}


int main()
{
   pthread_t t1, t2;
   char *s1 = "AAAAA";
   char *s2 = "BBBBB";

   if (pthread_create(&t1, NULL, smp_line(s1), NULL)) {
      fprintf(stderr, "Create thread t1 failed!\n");
      abort();
   }

   if (pthread_create(&t2, NULL, smp_line(s2), NULL)) {
      fprintf(stderr, "Create thread t2 failed!\n");
      abort();
   }


   exit(0);
}

在FreeBSD 6.1下编译运行,为什么会一直输出“AAAAAAAAAAAAAAAAAAAA....”呢,我指望它交错输出“AAAAA”和“BBBBB”呢。

[ 本帖最后由 xfsoul 于 2006-5-21 14:52 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-05-21 12:45 |只看该作者
本人也不怎么会,只是提两个小小疑问:
1,想得到预定的结果,线程是不是需要同步?
2, 线程共享进程的某些资源,而printf 输出到 stdout,是不是存在竞争关系?

论坛徽章:
0
3 [报告]
发表于 2006-05-21 12:48 |只看该作者
这两个线程并没有共享资源亚。

论坛徽章:
0
4 [报告]
发表于 2006-05-21 13:43 |只看该作者
把循环的次数弄的多一点看看。
你的pthread_create有语法问题吧。

论坛徽章:
0
5 [报告]
发表于 2006-05-22 17:16 |只看该作者
pthread_create没有问题啊,可以用函数名代替函数指针的嘛。
为什么就看不到两个线程并发执行的效果呢?

论坛徽章:
0
6 [报告]
发表于 2006-05-22 17:30 |只看该作者
smp_line(some_arg) is a function call expression.

smp_line is a primary expression.

论坛徽章:
0
7 [报告]
发表于 2006-05-22 17:32 |只看该作者
By the way, if smp_line doesn't contain a deadloop, pthread_create will report error or coredump.

论坛徽章:
0
8 [报告]
发表于 2006-05-22 18:37 |只看该作者
PTHREAD_CREATE(3)      FreeBSD Library Functions Manual      PTHREAD_CREATE(3)

NAME
     pthread_create -- create a new thread

LIBRARY
     Reentrant C Library (libc_r, -lc_r)
     POSIX Threads Library (libpthread, -lpthread)
     1:1 Threading Library (libthr, -lthr)

SYNOPSIS
     #include <pthread.h>

     int
     pthread_create(pthread_t *thread, const pthread_attr_t *attr,
         void *(*start_routine)(void *), void *arg);

你看man手册,第三个参数是一个回调函数啊,所以我传递一个函数指针给它,怎么会有问题呢?

论坛徽章:
0
9 [报告]
发表于 2006-05-22 18:39 |只看该作者
我把代码改了一下,运行几遍,终于看到运行输出会变化--也就是看到线程切换了吧。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sched.h>


#define NUM 100

int a = 0, b = 0;



void *thread1(void *args)
{

   int i;

   pthread_detach(pthread_self());

   for (i = 0; i < NUM; ++i) {
      ++a;
   }
   return NULL;

}


void *thread2(void *args)
{
   int i;

   pthread_detach(pthread_self());

   for (i = 0; i < NUM; ++i) {
      ++b;
   }

   return NULL;

}


int main()
{
   pthread_t t1, t2;
   int i;

   if (pthread_create(&t1, NULL, thread1, NULL)) {
      fprintf(stderr, "Create thread t1 failed!\n");
      abort();
   }

   if (pthread_create(&t2, NULL, thread2, NULL)) {
      fprintf(stderr, "Create thread t2 failed!\n");
      abort();
   }

   for (i = 0; i < NUM; ++i) {
      printf("Now a == %d; b == %d\n", a, b);
   }

   exit(0);
}

论坛徽章:
0
10 [报告]
发表于 2006-05-22 18:49 |只看该作者
现在看到三个线程并行的很好了:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sched.h>


#define NUM 100

int a = 0, b = 0;



void rand_cal()
{
   printf("In thread %d: (a, b) == (%d, %d)\n", (int)pthread_self(), a, b);
}


void *thread1(void *args)
{

   int i;

   pthread_detach(pthread_self());

   for (i = 0; i < NUM; ++i) {
      rand_cal();
      ++a;
   }
   return NULL;

}

void *thread2(void *args)
{  
   int i;
   
   pthread_detach(pthread_self());
   
   for (i = 0; i < NUM; ++i) {
      rand_cal();
      ++b;
   }
   
   return NULL;

}


int main()
{  
   pthread_t t1, t2;
   int i;
   
   if (pthread_create(&t1, NULL, thread1, NULL)) {
      fprintf(stderr, "Create thread t1 failed!\n");
      abort();
   }
   
   if (pthread_create(&t2, NULL, thread2, NULL)) {
      fprintf(stderr, "Create thread t2 failed!\n");
      abort();
   }
   
   for (i = 0; i < NUM; ++i) {
      rand_cal();
      printf("Now a == %d; b == %d\n", a, b);
   }
   
   exit(0);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP