Connecting Tech Pros Worldwide Forums | Help | Site Map

calling static member in pthread_create on freebsd fails

Newbie
 
Join Date: Aug 2007
Posts: 1
#1: Aug 27 '07
Hello,


I implemented a simple c++ thread wrapper; it works in Linux, but fails in
BSD.

Here is the code. The idea is that an other class will inherit and implement the virtual methods Setup and Execute. The thread is created in Start,
pthread_create returns 0 indicating that no error occured, the pthread_t variable is set. But the code found in Setup and Execute is never run.

Thanks, in advance

.h

Code:
Expand|Select|Wrap|Line Numbers
  1. class OC_Thread
  2. {
  3.    public:
  4.       OC_Thread();
  5.      virtual ~OC_Thread();
  6.       int Start(void * arg);
  7.      int Join();
  8.    protected:
  9.       int Run(void * arg);
  10.      int Sleep(unsigned int time);
  11.       static void * EntryPoint(void*);
  12.      virtual void Setup(){};
  13.      virtual void Execute(void*){};
  14.       void * Arg() const {return Arg_;}
  15.       void Arg(void* a){Arg_ = a;}
  16.    private:
  17.       pthread_t m_thread_id;
  18.       void * Arg_;
  19.      bool m_joinable;
  20.  
  21. };
  22.  
.cpp

Code:
Expand|Select|Wrap|Line Numbers
  1. #include "Thread.h"
  2.  
  3. //extern "C" void* thread_func(void*)
  4. //{
  5. //   printf("Hey from thread funv\n");
  6. //}
  7.  
  8. OC_Thread::OC_Thread()
  9. {
  10.    printf("Hey from My OC_Thread\n");
  11. }
  12.  
  13. OC_Thread::~OC_Thread()
  14. {
  15.    printf("delete from OC_Thread\n");
  16.  
  17.    if (m_joinable)
  18.     {
  19.         pthread_detach(m_thread_id);
  20.     }
  21. }
  22.  
  23. int OC_Thread::Start(void * arg)
  24. {
  25.    Arg(arg); // store user data   
  26.    int code =0;
  27.    code = pthread_create(&m_thread_id, NULL, OC_Thread::EntryPoint, (void*)this);
  28.  
  29.    printf("Hey from start code is %d and id is %d\n", code, (int)m_thread_id);
  30.  
  31.    return code;
  32. }
  33.  
  34. int OC_Thread::Run(void * arg)
  35. {
  36.    printf("Hey from run\n");
  37.  
  38.    Setup();
  39.    Execute( arg );
  40.  
  41.    return 0;
  42. }
  43.  
  44. int OC_Thread::Sleep(unsigned int time)
  45. {
  46.    sleep(time);
  47.  
  48.    return 0;
  49. }
  50.  
  51. int OC_Thread::Join()
  52. {   
  53.    void *res;
  54.  
  55.    pthread_join(m_thread_id, &res );
  56.  
  57.    return 0;
  58. }
  59.  
  60. /*static */
  61. void* OC_Thread::EntryPoint(void * pthis)
  62. {
  63.    printf("Hey from EntryPoint\n");
  64.  
  65.    fflush(stdout);
  66.  
  67.    OC_Thread* pt = (OC_Thread*)pthis;
  68.    pt->Run( pt->Arg() );
  69.  
  70.    return NULL;
  71. }

RRick's Avatar
Expert
 
Join Date: Feb 2007
Posts: 430
#2: Aug 28 '07

re: calling static member in pthread_create on freebsd fails


How do EntryPoint and Run do? Do you get the log info from them?

If not, then you have problems with Pthread.

If so, then I suspect you are not passing a derived class but are passing the base class instead. You might want to put a debug statement in the base class Setup and Execute, to see if they are called instead.
Reply