免费注册 查看新帖 |

Chinaunix

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

[C++] linux下c++的thread问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-13 15:29 |只看该作者 |倒序浏览

  1. #include <iostream>
  2. #include <pthread.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. using namespace std;

  6. int number = 1;

  7. void* thread1(void*)
  8. {
  9.   for(int i = 0; i < 10; i++)
  10.   {
  11.     cout << "this is thread1, number is " << number++;
  12.     cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
  13.   }
  14. }

  15. void* thread2(void*)
  16. {
  17.   for(int i = 0; i < 10; i++)
  18.   {
  19.     cout << "this is thread2, number is " << number++;
  20.     cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
  21.   }
  22. }

  23. int main()
  24. {
  25.   typedef void* (*thre)(void*);
  26.   pthread_t tid[2];
  27.   //thre thre1 = thread1;
  28.   
  29.   pthread_create(&tid[1], NULL, thread1, NULL);
  30.   pthread_create(&tid[2], NULL, thread2, NULL);
  31.   
  32.   cout << "tid1 is " << tid[1] << " tid2 is " << tid[2] << endl;
  33.   pthread_join(tid[1], NULL);
  34.   pthread_join(tid[2], NULL);

  35.   return 0;
  36. }
复制代码

上面的程序可以正常编译,但是下面的就不行了

  1. #include <iostream>
  2. #include <pthread.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. using namespace std;

  6. class Thread
  7. {
  8.   private:
  9.     int number;
  10.     pthread_t tid[2];

  11.   public:
  12.     Thread()
  13.     {
  14.       number = 1;
  15.       pthread_create(&tid[1], NULL, thread1, NULL);
  16.       pthread_create(&tid[2], NULL, thread2, NULL);
  17.     }

  18.     ~Thread()
  19.     {
  20.       cout << "tid1 is " << tid[1] << " tid2 is " << tid[2] << endl;
  21.       pthread_join(tid[1], NULL);
  22.       pthread_join(tid[2], NULL);
  23.     }

  24.     void* thread1(void*)
  25.     {
  26.       for(int i = 0; i < 10; i++)
  27.       {
  28.         cout << "this is thread1, number is " << number++;
  29.         cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
  30.       }
  31.     }

  32.     void* thread2(void*)
  33.     {
  34.       for(int i = 0; i < 10; i++)
  35.       {
  36.         cout << "this is thread2, number is " << number++;
  37.         cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
  38.       }
  39.     }
  40. };

  41. int main()
  42. {
  43.   Thread *th = new Thread();
  44.   delete(th);
  45.   
  46.   return 0;
  47. }
复制代码

出现的错误是


  1. thread.cpp: In constructor `Thread::Thread()':
  2. thread.cpp:17: no matches converting function `thread1' to type `
  3.    void*(*)(void*)'
  4. thread.cpp:29: candidates are: void* Thread::thread1(void*)
  5. thread.cpp:18: no matches converting function `thread2' to type `
  6.    void*(*)(void*)'
  7. thread.cpp:38: candidates are: void* Thread::thread2(void*)
复制代码

使用tepdef void*(*thre)(void*); thre ttt1 = thread1;作为参数还是不行
不晓得是甚么原因????
望高人指点

论坛徽章:
0
2 [报告]
发表于 2006-11-13 16:11 |只看该作者

回复 1楼 clyman 的帖子

似乎在那里看到,要把线程函数体定义在类外部,或者前加“static”

论坛徽章:
0
3 [报告]
发表于 2006-11-13 16:12 |只看该作者
但是他报的是类型转换....

论坛徽章:
0
4 [报告]
发表于 2006-11-13 17:07 |只看该作者
原帖由 king_wuhan 于 2006-11-13 16:11 发表
似乎在那里看到,要把线程函数体定义在类外部,或者前加“static”




我试了一下  好象不太行啊

论坛徽章:
0
5 [报告]
发表于 2006-11-14 10:18 |只看该作者
线程函数访问私有变量,可以分两步实现
(1)函数声明为静态函数;
(2)在调用函数的时候将实例的指针this以void *的形式传递进去;

#include <iostream>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;

class Thread
{
  private:
    int number;
    pthread_t tid[2];

  public:
    Thread()
    {
      number = 1;
      pthread_create(&tid[1], NULL, thread1, this);
      pthread_create(&tid[2], NULL, thread2, this);
    }

    ~Thread()
    {
      cout << "tid1 is " << tid[1] << " tid2 is " << tid[2] << endl;
      pthread_join(tid[1], NULL);
      pthread_join(tid[2], NULL);
    }

   static void* thread1(void* arg)
    {
      Thread* nthis=(Thread *)arg;
      for(int i = 0; i < 10; i++)
      {
        cout << "this is thread1, number is " << number++;
        cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
      }
    }

   static void* thread2(void* arg)
    {
      Thread* nthis=(Thread *)arg;
      for(int i = 0; i < 10; i++)
      {
        cout << "this is thread2, number is " << number++;
        cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
      }
    }
};

int main()
{
  Thread *th = new Thread();
  delete(th);
  
  return 0;
}

论坛徽章:
0
6 [报告]
发表于 2006-11-14 10:33 |只看该作者
原帖由 billzhou 于 2006-11-14 10:18 发表
线程函数访问私有变量,可以分两步实现
(1)函数声明为静态函数;
(2)在调用函数的时候将实例的指针this以void *的形式传递进去;

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



编译通不过啊,呵呵

论坛徽章:
0
7 [报告]
发表于 2006-11-14 11:15 |只看该作者
原帖由 billzhou 于 2006-11-14 10:18 发表
线程函数访问私有变量,可以分两步实现
(1)函数声明为静态函数;
(2)在调用函数的时候将实例的指针this以void *的形式传递进去;

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



忘了  函数体内改一下    nthis.number++就可以了

论坛徽章:
0
8 [报告]
发表于 2006-11-14 11:21 |只看该作者
把线程函数写成全局函数

论坛徽章:
0
9 [报告]
发表于 2006-11-14 19:17 |只看该作者
参见类成员函数的帖子,设置为static是对的

论坛徽章:
38
2017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之深圳
日期:2023-02-16 14:39:0220周年集字徽章-年
日期:2022-08-31 14:25:28黑曼巴
日期:2022-08-17 18:57:0919周年集字徽章-年
日期:2022-04-25 13:02:5920周年集字徽章-20	
日期:2022-03-29 11:10:4620周年集字徽章-年
日期:2022-03-14 22:35:1820周年集字徽章-周	
日期:2022-03-09 12:51:3220周年集字徽章-年
日期:2022-02-10 13:13:4420周年集字徽章-周	
日期:2022-02-03 12:09:4420周年集字徽章-20	
日期:2022-01-25 20:14:2720周年集字徽章-周	
日期:2022-01-13 15:12:33
10 [报告]
发表于 2006-11-14 19:35 |只看该作者
楼上两个对。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP