Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3666246
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:51:24+00:00 2026-05-19T01:51:24+00:00

Class Test{ int value; static void* thread_func(void* args){ value++; } void newthread(){ pthread_create(&thread_func,…); }

  • 0
Class Test{
    int value;
    static void* thread_func(void* args){
        value++;
    }
    void newthread(){
        pthread_create(&thread_func,...);
    }
}

I’m trying to create a thread in Class Test. Therefore compiler forces me to make thread_func static. However I cannot access the non-static member “value” anymore. It says:

invalid use of member 'Class::value' in static member function

Is there a way around it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:51:25+00:00Added an answer on May 19, 2026 at 1:51 am

    However I cannot access the non-static
    member “value” anymore.

    That is because static function in your class doesn’t have (and cannot have ) this pointer. All you need to pass the pointer to your Test object to pthread_create() function as fourth argument, and then do this:

    static void* thread_func(void* args)
    {
          Test *test = static_cast<Test*>(args);
          test->value++;
          //write return statement properly
    }
    

    However, if you’re doing too many things in thread_func() that require access to Test class members at many places, then I would suggest this design:

    //this design simplifies the syntax to access the class members!
    class Test
    {
        //code omitted for brevity
    
        static void* thread_func(void* args)
        {
              Test *test = static_cast<Test*>(args);
              test->run(); //call the member function!
              //write return statement properly
        }
        void run() //define a member function to run the thread!
        { 
              value++;//now you can do this, because it is same as 'this->value++;
              //you do have 'this' pointer here, as usual; 
              //so access other members like 'value++'.
        }
        //code omitted for brevity
      }
    

    Better design : define a reusable class!

    Even better would be to define a reusable class with pure virtual function run() to be implemented by the derived classes. Here is how it should be designed:

    //runnable is reusable class. All thread classes must derive from it! 
    class runnable
    {
    public:
        virtual ~runnable() {}
        static void run_thread(void *args)
        {
            runnable *prunnable = static_cast<runnable*>(args);
            prunnable->run();
        }
    protected:
        virtual void run() = 0; //derived class must implement this!
    };
    
    class Test : public runnable //derived from runnable!
    {
    public:
        void newthread()
        {
            //note &runnable::run_thread
            pthread_create(&runnable::run_thread,..., this);
        }
    protected:
        void run() //implementing the virtual function!
        {
            value++; // your thread function!
        }
    }
    

    Looks better?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Test { public static void main(String[] args) { } } class Outer
E,g class Test { public: void setVal(const std::string& str) { this.isVal = str; //This
I have the following code: public class Test { public static void Main() {
Consider following class class test { public: test(int x){ cout<< test \n; } };
Say I've got a class like this: class Test { int x; SomeClass s;
The following code using System.Threading; class Test { volatile int counter = 0; public
class test { public: test &operator=(const test & other){} // 1 const test &
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;
class Test { bool isVal() const { return isVal; } private: bool isVal; };
I have a class which looks something like this: public class Test { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.