How to get a pointer from another thread?

Let the following class definition hold:

CThread::CThread ()
{
    this->hThread       = NULL;
    this->hThreadId     = 0;
    this->hMainThread   = ::GetCurrentThread ();
    this->hMainThreadId     = ::GetCurrentThreadId ();
    this->Timeout       = 2000; //milliseconds
}

CThread::~CThread ()
{
    //waiting for the thread to terminate
    if (this->hThread) {
        if (::WaitForSingleObject (this->hThread, this->Timeout) == WAIT_TIMEOUT)
            ::TerminateThread (this->hThread, 1);

        ::CloseHandle (this->hThread);
    }
}

//*********************************************************
//working method
//*********************************************************
unsigned long CThread::Process (void* parameter)
{

    //a mechanism for terminating thread should be implemented
    //not allowing the method to be run from the main thread
    if (::GetCurrentThreadId () == this->hMainThreadId)
        return 0;
    else {
                m_pMyPointer = new MyClass(...);
                // my class successfully works here in another thread
        return 0;
    }

}

//*********************************************************
//creates the thread
//*********************************************************
bool CThread::CreateThread ()
{

    if (!this->IsCreated ()) {
        param*  this_param = new param;
        this_param->pThread = this;
        this->hThread = ::CreateThread (NULL, 0, (unsigned long (__stdcall *)(void *))this->runProcess, (void *)(this_param), 0, &this->hThreadId);
        return this->hThread ? true : false;
    }
    return false;

}

//*********************************************************
//creates the thread
//*********************************************************
int CThread::runProcess (void* Param)
{
    CThread*    thread;
    thread          = (CThread*)((param*)Param)->pThread;
    delete  ((param*)Param);
    return thread->Process (0);
}

MyClass* CThread::getMyPointer() {
    return m_pMyPointer;
}

In the main program, we have the following:

void main(void) {
  CThread thread;
  thread.CreateThread();

  MyClass* myPointer = thread.getMyPointer(); 
  myPointer->someMethod(); // CRASH, BOOM, BANG!!!!
}

MyPointer is currently in use (in the main thread), it is crashing. I do not know how to get a pointer that points to memory allocated in another thread. Is it possible?

+3
source share
3 answers

The amount of memory for your application is available for all threads. By default, any variable is visible to any thread regardless of context (the only exception will be declared variables __delcspec (thread))

- . , getMyPointer. . , , , .

+11

, . - . ?

C- CreateThread():

this->hThread = ::CreateThread (NULL, 0,&runProcess, (void *)(this_param), 0, &this->hThreadId);

, - ! ! , , ! ! ! , * ... , ! , .

Btw, in Process() , - :

assert(::GetCurrentThreadId() == hThreadId);

, CThread-, . , !

* , , , , !

0

- . , , .

:

MyClass* myPointer = thread.getMyPointer(); 

while (myPointer == 0) 
{
    ::Sleep(1000);
}

myPointer->someMethod(); // Working :)
-2

Source: https://habr.com/ru/post/1698350/


All Articles