0

I would like to be able to create a thread of a non-static member function of a class from outside the class without having to call a static helper function. Is this possible? I know that non-static member functions usually have a 'this' parameter added implicitly but how would one be able to explicitly add 'this' to the function call within the pthread_create() call? This needs to be done using pthread. An example of what I would like to achieve is shown in the code below

class A
{
    void* nonStaticFunction()
    {
        cout<<"Hi"<<endl;
        return NULL;
    }
}

typedef void* (*THREADFUNCPTR)(void*);

int main()
{
    A* a = new A();
    pthread_t pthread;
    pthread_create(&pthread,NULL,(THREADFUNCPTR) &A::nonStaticFunction,a);
    return 0;
}
Keagansed
  • 183
  • 1
  • 1
  • 13
  • 1
    Use std::thread. –  Sep 02 '17 at 13:11
  • @manni66 I need to do this by using pthread. – Keagansed Sep 02 '17 at 13:12
  • 1
    Then you are restricted to the C interface => no member functions. –  Sep 02 '17 at 13:14
  • create a static member function which will receive pointer to instance as void* - cast it to pointer to class and call member function – Artemy Vysotsky Sep 02 '17 at 13:16
  • @ArtemyVysotsky I stated that I need to achieve this without the use of a static helper function. – Keagansed Sep 02 '17 at 13:18
  • @manni66 Correct, which is why I need to find a way to treat it as/cast it to a static function. ie. not bound to an instance. – Keagansed Sep 02 '17 at 13:19
  • You can't. You have to feed the function and this through the arg parameter. –  Sep 02 '17 at 13:22
  • use std::mem_fn – Artemy Vysotsky Sep 02 '17 at 13:22
  • This is not a duplicate. I explicitly state that I am unable to make use of a static helper function. I stated this in the question and in the comments and it still got marked as a duplicate!? @Jeremy Friesner – Keagansed Sep 02 '17 at 13:41
  • @Keagansed Please review the given previous answer more carefully. It says that you can _also_ use "... a plain ordinary function to bootstrap the class". A plain ordinary C++ function is not a static helper function, and is not a class method. You might review "man pthread_create" on Ubuntu, or google the topic , I found and tested at least one working example of the use of a plain ordinary C++ function for this. – 2785528 Sep 02 '17 at 16:43
  • @Keagansed - "which is why I need to find a way to treat it as/cast it to a static function...". No you do not. No cast is needed. – 2785528 Sep 02 '17 at 16:51
  • @DOUGLASO.MOEN Regardless, it is not the answer to my question. I admit I probably should of cleared up the fact that I do not want to wrap the function in any way. It's not that I did not review the answer carefully enough. It is still the answer to another question. You DO need to cast it if you want to accomplish what I stated. There is literally no other way with pthread. I have found a way and will post the answer shortly. – Keagansed Sep 02 '17 at 20:06
  • @Keagansed - Perhaps we have a disconnect about what is meant by "static helper function". I have no Java and very little C experience. In C++, I think of a "static function" as an attribute of the class. Please review " https://stackoverflow.com/a/34136711/2785528 " – 2785528 Sep 02 '17 at 20:51
  • @DOUGLASO.MOEN as far as I understand, a static function contained within a class does not belong to an instance of the class (an object) in any way other way than it having access to its private member variables. It "floats" in memory and is not completely associated with the class. Therefore it does not require a "this" which is handed to a non static function implicitly. Calling it as a thread using pthread is therefore easy because it is not associated with an object. Calling a non static member function is not so easy and does require casts. As I said I am going to write up an answer soon – Keagansed Sep 03 '17 at 08:20

0 Answers0