boost::thread not updating global variable

Related searches

I am using a wrapper function in an external software to start a new thread, which updates a global variable, but yet this seems invisible to the main thread. I cant call join(), not to block the main thread and crash the software. boost::async, boost::thread and boost::packaged_task all behave the same way.

uint32 *Dval;

bool hosttask1()
{

        while(*Dval<10)
        {
            ++*Dval;
            PlugIn::gResultOut << " within thread global value: " << *Dval << std::endl;    
            Sleep(500);
        }


return false;
}



void SU_HostThread1(uint32 *value)
{

Dval = value; 
*Dval = 2;
PlugIn::gResultOut << " before thread: " << *value <<  " before thread global: " << *Dval << std::endl;

    auto myFuture = boost::async(boost::launch::async,&hosttask1);

    //boost::thread thread21 = boost::thread(&hosttask1);
    //boost::packaged_task<bool> pt(&hosttask1);
    //boost::thread thread21 = boost::thread(boost::move(pt)); 
}

When I call the function:

number a=0 
su_hostthread1(a)
sleep(2) //seconds
result(" function returned "+a+"  \n")

OUTPUT:

before thread value: 2 before thread global value: 2
 within thread global value: 3
 within thread global value: 4
 within thread global value: 5
 within thread global value: 6
 function returned 2  
 within thread global value: 7
 within thread global value: 8
 within thread global value: 9
 within thread global value: 10

Any ideas? Thanks in advance!

If you share data between threads, you must syncronize access to that data. The two possible ways are a mutex protecting said data and atomic operations. The simple reason is that caches and read/write reordering (both by CPU and compiler) exist. This is a complex topic though and it's nothing that can be explained in an answer here, but there are a few good books out there and also a bunch of code that gets it right.

Paralleism and multithreading, Threads share all global variables; the memory space where global variables are not twice; one of the updates was lost (this is called the “lost update problem”). In C++, threads are represented as instances of the class boost::thread . PIC16f777 Global Variable Not Updating Hello, I have written some code for a thermometer that uses an LCD screen and a small matrix keypad. I am not using any libraries. My issue is that my global variable, calData, is not staying changed after calibrate() changes its value. Thermometer() still sees calData as it was initially assigned.

The following code correctly reproduces what I intend to do. Mainly, the thread updates a global variable which the main thread correctly observes.

#include "stdafx.h"
#include <iostream>

#include <boost/thread.hpp>
#include <boost/chrono.hpp>

unsigned long *dataR;

bool hosttask1()
{
    bool done = false;
    std::cout << "In thread global value: " << *dataR << "\n"; //*value11 <<  *dataL << 
    unsigned long cc = 0;
    boost::mutex m;

        while (!done)
        {
            m.lock();
            *dataR = cc;
            m.unlock();
            cc++;
            std::cout <<  "In thread loop global value: "<< *dataR << "\n";
            if (cc==5) done = true;
        }


return done;
}

void SU_HostThread1(unsigned long *value)
{
    dataR = value;
    std::cout << "Before thread value: " << *value << " Before thread global value: " << *dataR << "\n"; //*value11 <<  *dataL << 
    auto myFuture = boost::async(boost::launch::async, &hosttask1);
    return;
}

int main()
{
    unsigned long value =1;
    unsigned long *value11;
    value11 = &value;

    SU_HostThread1(value11);

    boost::this_thread::sleep(boost::posix_time::seconds(1));
    std::cout << "done with end value: " << *value11 << "\n";

    return 0;
}

output:

Before thread value: 1 Before thread global value: 1
In thread global value: 1
In thread loop global value: 0
In thread loop global value: 1
In thread loop global value: 2
In thread loop global value: 3
In thread loop global value: 4
done with end value: 4

Yet when I copy this exactly to the SDK of the external software, the main thread does not update global value. Any ideas how this is so? Thanks

output in external software:

before thread value: 1 before thread global value: 1
In thread global value: 1
In thread loop global value: 0
In thread loop global value: 1
In thread loop global value: 2
In thread loop global value: 3
In thread loop global value: 4
done with end value: 1 

while loop not updating global variable in cpp, so i came across while loop not updating global variable in cpp int distx = op1 + 100; ROS_INFO("Distance range:[%d]",distx); while(ros::ok())� Just global variables and equations, all within my part file. And no warnings about variables already existing. When I update a global variable in the Equation Editor window, all equations referencing it are updated within this window, but the actual dimensions in my sketches / model are not.

Likely this is because the compiler doesn't generally think about multithreading when optimising your code. If has seen you code checks a value repeatedly, and it knows that in single threading that value cannot change, so it just omitted the check.

If you declare the variable as volatile, then it will probably generate less efficient code that checks more often.

Of course you have to also understand that when a value is written, there are circumstances when it may not all be written in one go, so if you are unlucky enough to read it back when it is half-written, then you get back a garbage value. The fix for that is to declare it as std::atomic (which is automatically considered volatile by the optimiser), and then even more complex code will be emitted to ensure that the write and the read cannot intersect (or different processor primitives might be used for small objects such as integers)

most variables are not shared between threads, and when they are it is up to the programmer to identify those and balance optimisation against the thread synchronisation needs during design.

Thread Local Storage - 1.65.0, Where a single-threaded application would use static or global data, this could lead to One example is the C errno variable, used for storing the error code related to for each thread, in order to avoid different threads competing to read or update the value. Portable thread-local storage with boost:: thread_specific_ptr. If you do not want your variable shared, then do not use a global variable (you probably mean static in Java). Create a new field with a new object initialized when your thread starts. Example: If you really need to access your objects locally to your thread across different pieces of code, then use ThreadLocal.

Python : How to use global variables in a function ? – thispointer.com, Boost Date Time Library � Boost String Algorithms Library Global variable is accessible in any function and local variable has scope only in modified 'total' variable inside the function then it was not reflected outside the function. value) � C++11 : How to use std::thread as a member variable in class ? I have a C++ program which declares some global variables. After that it splits up into several threads to do several tasks. Those threads read and write some of these global variables. Will there be an app-crash if two threads are reading the same variable?

[PDF] Automatic Handling of Global Variables for Multi-threaded MPI , global variables to ensure thread-safety for an MPI program. INTEGER :: var ! global variable scheme, the overhead does not increase as the number of. Boost.Thread supplies recursive and non-recursive mutexes with exclusive ownership semantics, along with a shared ownership (multiple-reader / single-writer) mutex. Boost.Thread supports four basic concepts for lockable objects: Lockable, TimedLockable, SharedLockable and UpgradeLockable. Each mutex type implements one or more of these concepts

Global variables can be used for many of the same purposes as linked values (also referred to as shared values or linked dimensions). Global variables are much easier to find, change, and manage than linked values.

Comments
  • This is a textbook data race on Dval, and the behavior is undefined.
  • Your example code contains errors, making it useless to discuss. Please change it so it provides a minimal reproducible example. As a new user here, please also take the tour and read How to Ask.
  • As the SU_hostthread() function argument has to be a pointer, how would I assign it into a thread safe global atomic variable, to be used inside the thread? e.g. first define std::atomic <unsigned long*> atomicvalue, as global. Then in the SU_hostthread() function re-assign it as std::atomic_exchange(&atomicvalue, value). The "atomicvalue" should point to the same address as "value", so that the external software can read it correctly.
  • The answer probably is that the main() function gets properly optimised. Again it doesn't know that you are multithreading. It "knows" you assigned the value 1 to a variable value you did NOT declare as volatile or atomic, and it inlined SU_HostThread1, and saw that you did not modify the value pointed to. So it just didn't bother to read it again. If you looked at the assembler it probably just printed a constant 1.
  • No, volatile is not the answer for multithreading synchronization. It may work in many places, but it's not the right tool and its use doesn't offer the guarantees that the right tools (atomics or mutexes) offer.
  • @UlrichEckhardt Indeed, but it is a step towards the correct answer, which is why i discuss it. but atomic and proper fences and mutexes are the only practical way forward. that is a much longer discussion you are welcome to post about.