Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 7069

std::thread constructor doesn't seem to accept pointers

$
0
0

I am trying to run a multi-threaded program as a test where it prints something on one thread and something else on the other, but they read a value from the same address which slows their printing and incrementing. I know this doesn't make sense to do but its my way of learning.

Here is the code:

#include <iostream>#include <thread>#include <chrono>using namespace std::chrono_literals;void test1(int &i){    while(i<10){        if (i%2 == 0){            std::cout << "Thread 1 " << i << std::endl;            std::this_thread::sleep_for(2000ms);            i++;        }    }}void test2(int &i){    while(i<10){        if (i%2 != 0) {            std::cout << "Thread 2 " << i << std::endl;            std::this_thread::sleep_for(2000ms);            i++;        }    }}int main(){    int i = 0;    int *pI = &i;    std::thread firstThread(test1, pI);    std::thread secondThread(test2, pI);    firstThread.join();    secondThread.join();    return 0;}

The error message that I am getting is:

In file included from /usr/include/c++/13/thread:45,                 from cppSocketTest/Linux/test.cpp:2:/usr/include/c++/13/bits/std_thread.h: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(int&); _Args = {int&}; <template-parameter-1-3> = void]’:cppSocketTest/Linux/test.cpp:29:37:   required from here/usr/include/c++/13/bits/std_thread.h:157:72: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues  157 |                                       typename decay<_Args>::type...>::value,      |                                                                        ^~~~~/usr/include/c++/13/bits/std_thread.h:157:72: note: ‘std::integral_constant<bool, false>::value’ evaluates to false/usr/include/c++/13/bits/std_thread.h: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(int&), int> >’:/usr/include/c++/13/bits/std_thread.h:236:13:   required from ‘struct std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(int&), int> > >’/usr/include/c++/13/bits/std_thread.h:164:29:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(int&); _Args = {int&}; <template-parameter-1-3> = void]’cppSocketTest/Linux/test.cpp:29:37:   required from here/usr/include/c++/13/bits/std_thread.h:291:11: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(int&), int> >::__result<std::tuple<void (*)(int&), int> >’  291 |           _M_invoke(_Index_tuple<_Ind...>)      |           ^~~~~~~~~/usr/include/c++/13/bits/std_thread.h:295:9: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(int&), int> >::__result<std::tuple<void (*)(int&), int> >’  295 |         operator()()      |         ^~~~~~~~

Any help on how to get this going would be appreciated.

I tried changing pointer values and sending random values but nothing fixes it.


Viewing all articles
Browse latest Browse all 7069

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>