473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

calling static member in pthread_create on freebsd fails

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. }
Aug 27 '07 #1
1 1865
RRick
463 Expert 256MB
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.
Aug 28 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
3
by: Rookie | last post by:
Hi, I was writing a simple multi-threaded program (code below). My intent is to invoke a member function of a class that creates two threads - each thread executes another member function of the...
4
by: Morgan Cheng | last post by:
I tried to build a CThread on linux which imitate the behavior of java Thread. I think this is still in the scope of C++. First, I tried this way #include <pthread.h> class CThread { public:...
12
by: Huskier | last post by:
Hi all: I want to pass a class-member function to pthread_create, and my code is in the following, but I don't know how to pass myobj.thread_function to pthread_create function. #include...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.