LinuxSir.cn,穿越时空的Linuxsir!

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

help!关于pthread_create所创建的线程入口函数。

[复制链接]
发表于 2008-2-21 15:08:36 | 显示全部楼层 |阅读模式
void *foo(void *arg)
{
cout<<"########"<<endl;
return NULL;
}

for (i = 0; i < 5; i++)
{
      if (pthread_create (&pid, ((void *)0),foo , (void *) &thread_arg))
    {
    printf ("create thread error!\n");
    exit (1);
  }
}
 楼主| 发表于 2008-2-21 15:10:06 | 显示全部楼层

help!关于pthread_create所创建的线程入口函数。

void *foo(void *arg)
{
cout<<"########"<<endl;
return NULL;
}

for (i = 0; i < 5; i++)
{
      if (pthread_create (&pid, ((void *)0),foo , (void *) &thread_arg))
       {
        printf ("create thread error!\n");
        exit (1);
        }
}
为何void *foo(void *arg)没有被执行,可能是哪些原因?
谢谢!
回复 支持 反对

使用道具 举报

发表于 2008-2-22 23:57:53 | 显示全部楼层
try replace ((void*)0) by NULL in pthread_create func-call
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-2-24 21:13:44 | 显示全部楼层
改为NULL之后问题依然存在。
恳请楼上继续指点。谢谢!
回复 支持 反对

使用道具 举报

发表于 2008-2-25 15:06:26 | 显示全部楼层

  1. #include <iostream>
  2. #include <pthread.h>

  3. using namespace std;

  4. static void* foo(void* arg);

  5. static void* foo(void* arg)
  6. {
  7.   cout <<"########" << endl;
  8.   return NULL;
  9. }

  10. int main()
  11. {
  12.   pthread_t pid[5];
  13.   int i;

  14.   for (i = 0; i < 5; i++)
  15.   {
  16.     if (pthread_create(&pid[i], NULL, foo, (void*) i))
  17.     {
  18.       cout << "create thread error!" << endl;
  19.       return -1;
  20.     }
  21.   }
  22.   for (i = 0; i < 5; i++)
  23.   {
  24.     pthread_join(pid[i], NULL);
  25.   }

  26.   return 0;
  27. }
复制代码


编译的时候要-lpthread
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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