0

Inside the class I declared the thread function. I used static keyword because with out static keyword it is not working with class.

But if the type of the function is static I could not able to access the member function and public variables of the class

#include <iostream>
#include <pthread.h>
using namespace std;

class Base{

private:
    static  void * fpga_read(void*); // Thread function
    void foo_2();
public:
    /* member variables */
    void foo(void);

protected:
    int b;
};


void Base::foo(void)
{
    pthread_t id;
    pthread_create(&id, NULL,fpga_read,NULL);
    cout << "\nInside base class" << endl;
}
void * Base::fpga_read(void *p)
{
    cout << "\nInside thread function " << endl;
    // error: invalid use of member ‘Base::b’ in static member function
    cout << "Value of B inside thread class" << b;

    int b;
}

int main()
{
    Base a;
    a.foo();

    pthread_exit(NULL);
    return 0;
}

Any-body tell me how to use thread function with-out static keyword. so i can able to access all class variables.

jww
  • 97,681
  • 90
  • 411
  • 885
Gagandeep
  • 33
  • 1
  • 2
  • 6
  • Do you have to use pthreads directly or are you allowed to use any C++11 class, like std::thread? – Maikel May 30 '19 at 10:38
  • Yeah this method cannot be static if you want use class members, try use *pointer to method* from this: https://stackoverflow.com/questions/1485983/calling-c-class-methods-via-a-function-pointer – Igor Galczak May 30 '19 at 10:41
  • 1
    Have you looked at any [documentation of `pthread_create`](http://man7.org/linux/man-pages/man3/pthread_create.3.html) to see what its arguments are and if you could perhaps use them? – Angew is no longer proud of SO May 30 '19 at 10:42

2 Answers2

0

pthread_create, like all OS-specific thread creation APIs (CreateThread etc in Windows) has a "void*" parameter to pass to the thread function.

You can use this to pass a pointer to your class

class A
{
    void ThreadToUse() {}
    static void Thread2(void* p) 
    {
        A* a = (A*)p;
        p->ThreadToUse();
    }

    void foo()
    {
        pthread_create(&A::Thread2,(void*)this);
    }
};

That said, you can use C++11 std::thread with the same functionality as well in a standard way:

void foo()
{
    std::thread t(&A::Thread2,this);
}
jww
  • 97,681
  • 90
  • 411
  • 885
Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
0

You do not need any static member function. You can use the argument parameter of pthread_create and that stateless lambda functions decay to normal function pointers to make code which almost looks like what you actually wrote:

Godbolt link: https://godbolt.org/z/QIGNUX


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

class Base {
public:
    Base(int state) noexcept : b{state} {}

    void foo();

private:
    int b;

    void fpga_read() {
        std::cout << "Value of B inside thread class" << b;
    }
};

void Base::foo()
{
    pthread_t thread;
    pthread_create(&thread, nullptr, [](void* that) -> void* {
        Base* this_ = static_cast<Base*>(that);
        this_->fpga_read();
        return nullptr;
    }, static_cast<void*>(this));
    pthread_join(thread, nullptr);
}
Maikel
  • 1,204
  • 7
  • 19
  • thread_2.cpp:6: error: expected ‘;’ before ‘noexcept’ thread_2.cpp:6: error: expected `;' before ‘{’ token thread_2.cpp:6: error: expected unqualified-id before ‘{’ token thread_2.cpp: In member function ‘void Base::foo()’: thread_2.cpp:21: error: ‘nullptr’ was not declared in this scope thread_2.cpp:21: error: expected primary-expression before ‘[’ token thread_2.cpp:21: error: expected primary-expression before ‘]’ token thread_2.cpp:21: error: expected primary-expression before ‘void’ thread_2.cpp:21: error: expected unqualified-id before ‘void’ – Gagandeep May 30 '19 at 16:07
  • Sounds like you're trying to compile to `c++03` standard. Try `-std=c++11` (or the equivalent for your compiler). – G.M. May 30 '19 at 16:25