QNX Object Oriented Streams in C ++

I want to create a parallel object oriented system in QNX using C ++ and threads. How to do it?

I tried:

pthread_t our_thread_id;
pthread_create(&our_thread_id, NULL, &functionA ,NULL);

      

with function A being a function pointer:

void *functionA()
{ //do something
}

      

However, this feature only works in C, not C ++. How can I make it work in C ++?

0


source to share


3 answers


See How do I pass a function pointer to an element to a signal handler, X event callback, system call that starts a thread / task, etc.? ...

In short, you are passing a static function ("trampoline") as a function pointer. You are passing "this" as a user-defined parameter. The static function then bounces the call back to the real object.

For example:



class Thread {
public:
    int Create()
    {
        return pthread_create(&m_id, NULL, start_routine_trampoline, this);
    }

protected:
    virtual void *start_routine() = 0;

private:
    static void *start_routine_trampoline(void *p)
    {
        Thread *pThis = (Thread *)p;
        return pThis->start_routine();
    }
};

      

And you need to make sure the C ++ function has the same calling convention as expected by pthread_create.

+2


source


Yours is functionA

not a function pointer, it is a function that returns void*

. The function is also expected to be void *. This argument is used to pass a pointer to the data required in the stream.

If you replace

void* functionA() {

      



from

void functionA(void* threadData) {

      

I expect it to work in both C and C ++.

+1


source


I think you need to declare functionA

as extern "C"

(and give it the correct signature, see the documentation for pthreads in QNX ). This function takes user this

intance as a parameter :

extern "C"
{
   void* DoSomethingInAThread(void* pGenericThis)
   {
     YourClass* pThis = reinterpret_cast<YourClass*>(pGenericThis);
     pThis->DoSomethingInAThread();
   }
}

int main()
{
  YourClass* pSomeInstance = new YourClass();
  pthread_t our_thread_id;
  pthread_create(&our_thread_id, NULL, &DoSomethingInAThread, pSomeInstance);
}

      

0


source







All Articles