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 7745479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:01:58+00:00 2026-06-01T10:01:58+00:00

I need a static variable that points to itself to use when referencing the

  • 0

I need a static variable that points to itself to use when referencing the object in a thread method. I am trying to use the _beginthread method in process.h Many objects of this type are going to be using the thread method. Currently this is failing because the instance variable is shared across the whole class. I need the instance variable to be static to use in the threadLoop and need it to reference the object. Any suggestions?

Header: static Nodes *instance;

Implementation: Nodes *Nodes::instance = NULL;

main.cpp:

for(int c = 0; c < 7; c++)
{
    nodesVect.push_back(Nodes(c, c+10));
}

for(int c = 0; c < 7; c++) 
{
   nodesVect.at(c).init(); // init() {  instance = this;  }
}
  • 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-06-01T10:01:59+00:00Added an answer on June 1, 2026 at 10:01 am

    My _beginthreadex() usage is as follows;

    a cStartable base class

    virtual bool Start(int numberOfThreadsToSpawn);
    virtual bool Stop();
    virtural int Run(cThread &myThread) = 0;
    //the magic...
    friend unsigned __stdcall threadfunc(void *pvarg);
    void StartableMain();
    

    the majic is:

    unsigned __stdcall threadfunc(void *pvarg)
    {
        cStartable *pMe = reinterpret_cast<cStartable*>(pvarg);
        pMe->StartableMain();
    }
    void cStartable::StartableMain()
    {
       //Find my threadId in my threadMap
       cThread *pMyThread = mThreadMap.find( GetCurrentThreadId() );
       int rc = Run( pMyThread );
    }
    bool cStartable::Start()
    {
       cThread *pThread = new cThread();
       pThread->Init();
       mThreadMap.insert( tThreadMapData(pThread->mThreadId, pThread) );
    }
    

    and a utility cThread class.

    bool cThread::Init(cStartable *pStartable)
    {
        _beginthreadex( NULL, /*stack*/ 65535), &threadfunc, pStartable, /*initstate*/0, &mThreadId );
         // now cThread has a unique bit of info that can match itself up within the startable's run.   
    }
    

    things that need a thread inherit from startable and implement their Run.

    class Node : public cStartable {}
    

    I’ve edited my code quite a lot here. It is all very robust and quiet powerful to beable to spawn multiple thread instances all at once off a single object and have it be so clean at the subclass level.

    the point of all of this is that cNode::Run() gets passed a per thread instance object into which per thread instance heap data could be attached. otherwise all thread instances share their single class instance as their “memory space”. I like it 🙂
    If you need more details I’m happy to share.

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

Sidebar

Related Questions

I need to bea be able to use a static variable set in a
I want to declare static(or not static) variable inside Enum. I need this because
I need to mock a GrailsControllerClass interface. Instance should have a static variable defined.
I have a base class A with a constant static variable a. I need
I need to change a static property on an object in our web application.
I need to explain myself why I do not use static methods/propertis. For example,
I need to call a static function from an object using the Singleton design,
I created my array in a method and some variables that need to be
I need a way to have a single static variable for all kinds of
The problem is that I need to get static data from server prior the

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.