Chinaunix

标题: linux下c++的thread问题 [打印本页]

作者: clyman    时间: 2006-11-13 15:29
标题: linux下c++的thread问题

  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;作为参数还是不行
不晓得是甚么原因????
望高人指点
作者: king_wuhan    时间: 2006-11-13 16:11
标题: 回复 1楼 clyman 的帖子
似乎在那里看到,要把线程函数体定义在类外部,或者前加“static”
作者: clyman    时间: 2006-11-13 16:12
但是他报的是类型转换....
作者: billzhou    时间: 2006-11-13 17:07
原帖由 king_wuhan 于 2006-11-13 16:11 发表
似乎在那里看到,要把线程函数体定义在类外部,或者前加“static”




我试了一下  好象不太行啊
作者: billzhou    时间: 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;
}
作者: aloneme_live    时间: 2006-11-14 10:33
原帖由 billzhou 于 2006-11-14 10:18 发表
线程函数访问私有变量,可以分两步实现
(1)函数声明为静态函数;
(2)在调用函数的时候将实例的指针this以void *的形式传递进去;

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



编译通不过啊,呵呵
作者: billzhou    时间: 2006-11-14 11:15
原帖由 billzhou 于 2006-11-14 10:18 发表
线程函数访问私有变量,可以分两步实现
(1)函数声明为静态函数;
(2)在调用函数的时候将实例的指针this以void *的形式传递进去;

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



忘了  函数体内改一下    nthis.number++就可以了
作者: x.jc    时间: 2006-11-14 11:21
把线程函数写成全局函数
作者: converse    时间: 2006-11-14 19:17
参见类成员函数的帖子,设置为static是对的
作者: 醉卧水云间    时间: 2006-11-14 19:35
楼上两个对。
作者: yingen2006    时间: 2006-11-15 09:36
加static 是对的。
作者: clyman    时间: 2006-11-15 17:38
从csdn上找来的答案,问题答案一样,解决办法不一样
楼上面的答案还是没编译过去,唉~~

#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_ex, this);
      pthread_create(&tid[2], NULL, thread2_ex, 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_ex(void* pVoid)
    {
      Thread* p = (Thread*)pVoid;
      p->thread1();
      return p;
    }

    static void* thread2_ex(void* pVoid)
    {
      Thread* p = (Thread*)pVoid;
      p->thread2();
      return p;
    }

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

    void* thread2()
    {
      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;
}
作者: clyman    时间: 2006-11-15 17:41
原帖由 billzhou 于 2006-11-14 11:15 发表



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

nthis->number++
作者: chzht001    时间: 2006-11-16 09:24
原帖由 clyman 于 2006-11-15 17:38 发表
从csdn上找来的答案,问题答案一样,解决办法不一样
楼上面的答案还是没编译过去,唉~~

#include <iostream>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
...


用g++-4.1编译过去但运行有问题
试想一下这种做法还是存在一定的问题,线程去访问一个对象,而这个对象在析构后消亡,
程序存在根本性错误,他以为通过纯种调用类的函数,但是没注意到调中类中函数和 C 中函数是不同的
调用类中函数需有类对象这个实体存在,而 C 中则不需要,因在 C 中函数只是一段代码的标识而已

无疑这种做法风险很大

[ 本帖最后由 chzht001 于 2006-11-16 09:31 编辑 ]
作者: billzhou    时间: 2006-11-16 09:27
原帖由 clyman 于 2006-11-15 17:41 发表

nthis->number++


这样是对的 呵呵 马虎了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2