简体   繁体   中英

Communicating information between threads

I have a thread that reads the memory of another program. The information read is stored in a global variable. I, then, create another thread that acts on this information.

I have initialized the variable to be 0. After the first thread is created (the one that reads the memory of another program), it prints to console the value it read (it successfully reads the value correctly). In my main function, though, it still prints out to be zero.

I've tried std::atomic, but that won't work for this instance. How can I get my variables to have changes accepted despite not being in the same thread?

Here is how I create threads:

std::thread tTest( [ & ]
{
    TestFunction( );
} );
vecThreads.push_back( move( tTest ) );

std::thread tTest2( [ & ]
{
    OtherTestFunction( );
} );
vecThreads.push_back( move( tTest2 ) );

Here is an example of what I do in main:

int bob = 0;

int main( )
{
    CreateThreads( );
    if ( bob )
    {
        std::cout << "T2 bob: " << bob << std::endl;
    }
    else
    {
        std::cout << "T2 bob: uninitialized" << std::endl;
    }
}

My test functions:

void TestFunction( )
{
    ReadProcessMemory( hProcess, lpBaseAddress, &bob, sizeof(bob), 0 );
    std::cout << "T1 bob: " << bob << std::endl;
}

void OtherTestFunction( )
{
    if ( bob )
    {
        std::cout << "T2 bob: " << bob << std::endl;
    }
    else
    {
        std::cout << "T2 bob: uninitialized" << std::endl;
    }
}

Thanks!

You need to use some sort of thread synchronization. There is no guarantee that TestFunction will run before OtherTestFunction ; it all depends on how the OS schedules threads. And in this case, ReadProcessMemory is not a quick function. OtherTestFunction will usually complete running before TestFunction gets a value stored into bob .

For example, you could use async (from <future> ) to create your first thread (that runs TestFunction ). Pass (via move) the future you get from async to your second thread ( OtherTestFunction ), which would wait on the future for the first thread to finish executing. Then it could access the value of bob .

You could also look at std::promise , or you could use the CreateEvent and related functions in the Windows API.

A very simple and elegant solution to your problem is this:

First declare a global variableL std::mutex mut;

Then in your functions do this:

void TestFunction( )
{

    mu.lock();
    ReadProcessMemory( hProcess, lpBaseAddress, &bob, sizeof(bob), 0 );
    std::cout << "T1 bob: " << bob << std::endl;
    mu.unlock()
}

void OtherTestFunction( )
{
    mu.lock();
    if ( bob )
    {
        std::cout << "T2 bob: " << bob << std::endl;
    }
    else
    {
        std::cout << "T2 bob: uninitialized" << std::endl;
    }
    mu.unlock();
}

If you need to make sure one function runs before the other create another global variable like this: bool firstRan .

Then in your methods do this:

void TestFunction( )
{

    mu.lock();
    ReadProcessMemory( hProcess, lpBaseAddress, &bob, sizeof(bob), 0 );
    std::cout << "T1 bob: " << bob << std::endl;
    firstRan = true;
    mu.unlock()
}

void OtherTestFunction( )
{
    while(!firstRan) {}
    mu.lock();
    if ( bob )
    {
        std::cout << "T2 bob: " << bob << std::endl;
    }
    else
    {
        std::cout << "T2 bob: uninitialized" << std::endl;
    }
    mu.unlock();
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM