多线程测试似乎不接受指针

问题描述 投票:0回答:1

我正在尝试运行一个多线程程序作为测试,它在一个线程上打印一些内容,在另一个线程上打印其他内容,但是它们从同一地址读取一个值,这会减慢打印和递增的速度。我知道这样做没有意义,但这是我的学习方式。

这是代码:

#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;
}

我收到的错误消息是:

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()()
      |         ^~~~~~~~

任何有关如何实现这一目标的帮助将不胜感激。

我尝试更改指针值并发送随机值,但没有任何解决办法。

c++ multithreading ubuntu pointers
1个回答
0
投票

编译错误是因为您的线程函数需要对 int (

int &
) 的引用,但实际上您向它们传递了一个指向 int (
int *
) 的指针,这是不兼容

对于需要

int&
的普通函数,您可以简单地传递
i
,但是线程构造函数将复制参数,而您无法复制引用。
为了解决这个问题,您可以使用
std::ref
创建一个
std::reference_wrapper
,其中:

将引用包装在可复制、可分配的对象中。

即将线程构造行更改为:

//------------------------------vvvvvvvv-----
std::thread firstThread (test1, std::ref(i));
std::thread secondThread(test2, std::ref(i));

这将使您的代码编译。

但是-
正如@IgorTandetni 评论,您当前的程序将调用未定义行为,因为两个线程都会访问

i
,导致数据争用

© www.soinside.com 2019 - 2024. All rights reserved.