0

I am trying to use an API which sets a value of a variable based on an HTTP call. The function in which I can set the variable which will be set upon an HTTP Call is of type T&&. I would like to access this variable on a different thread. I tried to simplify the problem and represent it in the following code, as two threads, accessing a variable the same way.

#include <iostream>
#include <thread>
#include <chrono>

class Values
{
public:
    int i;
    std::string s;
};


template<typename T>
void WriteCycle(T&& i)
{
    using namespace std::chrono_literals;
    while (true)
    {
        i++;
        std::cout << i << std::endl;
        std::this_thread::sleep_for(500ms);
    }

}
template<typename T>
void ReadCycle(T&& i)
{
    using namespace std::chrono_literals;

    while (true)
    {
        std::cout << i << std::endl;
        std::this_thread::sleep_for(500ms);
    }

}

int main() {


    auto v = new Values();

    std::thread t1(WriteCycle<int>, v->i);
    std::thread t2(ReadCycle<int>, v->i);


    t1.join();
    t2.join();
}

Currently the read thread does not see any change in the variable. I read up an perfect forwarding, move semnatics and forwardin references, but I did not get it (I mostly program dotnet, my c++ knowledge is pre C++11). I tried all combinations of std::move, std::ref and std::forward but I cannot get the read thread to see the change of the write thread. Is there a way to solve this, without changing the T&& input type of the functions (since that is part of the API I am trying to use)? How to solve this in a thread-safe way?

5
  • 1
    Yes, there's an extra twist with thread functions that wrecks pass-by-reference semantics. However, fixing that won't be enough. None of the accesses to the shared counter are properly synchronized using mutexes, so even if the pass-by-reference semantics is fixed, there will still be undefined behavior. Solving this requires employing a mutex, a bunch of additional code, too. Finally, the templates are instantiated here. So there's no universal references at all, in the end. Commented Apr 24, 2022 at 21:24
  • @SamVarshavchik good news! Thanks! Any change you could give some pointers on how to do this, or how to get closer to an answer? I know I will need mutexes, but until I can access the same thing on multiple threads, there is no use for the mutex.
    – 50k4
    Commented Apr 24, 2022 at 21:28
  • A Google search on site:stackoverflow.com for some likely keywords, like "pass references to threads C++", something like that, should find plenty of questions and answers on this topic. Commented Apr 24, 2022 at 21:30
  • I tried passing the parameters to the thread with std::ref, but that leads to an error: rror: attempt to use a deleted function _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    – 50k4
    Commented Apr 24, 2022 at 21:43
  • I don't know what "the thread with std::ref" means. I can only look at actual code and comment on it, instead of on its generic description. Commented Apr 24, 2022 at 21:45

1 Answer 1

1

Such notation:

template<typename T>
void WriteCycle(T&& i)

Doesn't really mean an rvalue reference, it means a universal reference, which could be an lvalue reference or rvalue reference depending on what kind of data you pass.

In your case it turns into just an lvalue reference, so it has nothing to do with move semantic. The problem is that thread constructor is not quite friendly with references and you may want just to use std::ref to get it round:

auto myRef = std::ref(v->i);
std::thread t1(WriteCycle<&int>, myRef);
std::thread t2(ReadCycle<&int>, myRef);

However it still won't be perfect, because you want to synchronize between threads with use of mutexes or atomic values

3
  • I tried passing std::thread t1(WriteCycle<int>, std::ref(v->i)), and also the combination you provided. but I get an error, rror: attempt to use a deleted function _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    – 50k4
    Commented Apr 24, 2022 at 21:42
  • 2
    @50k4 It is the wrong template specialization. It should be WriteCycle<int&> and ReadCycle<int&> if you want to pass by lvalue reference (and rvalue reference doesn't really make sense here). Commented Apr 24, 2022 at 21:47
  • 1
    @50k4 my bad and user17732522 is right, you have to make it explicit that you want to use a reference Commented Apr 24, 2022 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.