gpt4 book ai didi

multithreading - 使用 std::future 和 valgrind 的一次性事件通信

转载 作者:行者123 更新时间:2023-12-04 10:19:07 33 4
gpt4 key购买 nike

免责声明:这不是关于 C++11 线程库上的 helgrind 误报的问题。

我编写了一个简单的 C++11/14 程序,旨在展示使用 std::promisestd::future 类的事件队列实现。

#include <cstdio>
#include <future>
#include <thread>
#include <vector>

static const auto thread_max = 4;

class Worker
{
public:
explicit Worker(int num_)
: prm(), fut(), num(num_)
{}

Worker(int num_, std::future<bool> fut_)
: prm(), fut(std::move(fut_)), num(num_)
{}

Worker(Worker &&other)
: prm(std::move(other.prm)), fut(std::move(other.fut)), num(other.num)
{}

std::future<bool> get_future() { return prm.get_future(); }

void operator()()
{
if (fut.valid()) fut.get();
printf("Worker number %d over here\n", num);
/* Doing something useful. */
printf("Worker number %d is done\n", num);
prm.set_value(false);
}

private:
Worker(const Worker &other) = delete;
Worker &operator=(Worker &&other) = delete;
Worker &operator=(const Worker &other) = delete;

std::promise<bool> prm;
std::future<bool> fut;
int num;
};

int
main()
{
std::vector<std::thread> threads;

std::future<bool> last;
for (auto i = 0; i < thread_max; ++i) {
auto worker = last.valid()
? Worker(i, std::move(last))
: Worker(i);
last = worker.get_future();
threads.emplace_back(std::move(worker));
}

for (auto &thread : threads)
if (thread.joinable())
thread.join();

return 0;
}

valgrind 的输出是。

Worker number 0 over here
==15770== Thread 3:
==15770== Syscall param futex(utime) contains uninitialised byte(s)
==15770== at 0x433A347: syscall (in /usr/lib/libc-2.22.so)
==15770== by 0x410E615: std::__atomic_futex_unsigned_base::_M_futex_wait_until(unsigned int*, unsigned int, bool, std::chrono::duration<long long, std::ratio<1ll, 1ll> >, std::chrono::duration<long long, std::ratio<1ll, 1000000000ll> >) (futex.cc:55)
==15770== by 0x804C04C: std::__atomic_futex_unsigned<2147483648u>::_M_load_and_test_until(unsigned int, unsigned int, bool, std::memory_order, bool, std::chrono::duration<long long, std::ratio<1ll, 1ll> >, std::chrono::duration<long long, std::ratio<1ll, 1000000000ll> >) (in /home/whatever/a.out)
==15770== by 0x804B4AC: std::__atomic_futex_unsigned<2147483648u>::_M_load_and_test(unsigned int, unsigned int, bool, std::memory_order) (in /home/whatever/a.out)
==15770== by 0x8049EF7: std::__future_base::_State_baseV2::wait() (in /home/whatever/a.out)
==15770== by 0x804BBB7: std::__basic_future<bool>::_M_get_result() const (in /home/whatever/a.out)
==15770== by 0x804AD95: std::future<bool>::get() (in /home/whatever/a.out)
==15770== by 0x804A488: Worker::operator()() (in /home/whatever/a.out)
==15770== by 0x804E6AF: void std::_Bind_simple<Worker ()>::_M_invoke<>(std::_Index_tuple<>) (in /home/whatever/a.out)
==15770== by 0x804E5FF: std::_Bind_simple<Worker ()>::operator()() (in /home/whatever/a.out)
==15770== by 0x804E3D0: std::thread::_Impl<std::_Bind_simple<Worker ()> >::_M_run() (in /home/whatever/a.out)
==15770== by 0x4110AED: execute_native_thread_routine (thread.cc:84)
==15770==
Worker number 0 is done
Worker number 1 over here
Worker number 1 is done
Worker number 2 over here
Worker number 2 is done
Worker number 3 over here
Worker number 3 is done

我的配置是。

[whatever@whatever build]$ gcc --version
gcc (GCC) 5.2.0
[whatever@whatever build]$ valgrind --version
valgrind-3.10.1
[whatever@whatever build]$ ldd a.out
linux-gate.so.1 (0xb7718000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0xb76d5000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb755f000)
libm.so.6 => /usr/lib/libm.so.6 (0xb7511000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0xb74f4000)
libc.so.6 => /usr/lib/libc.so.6 (0xb733a000)
/lib/ld-linux.so.2 (0xb7719000)

上面的代码是不是有什么问题,还是应该算是其他地方的bug?

最佳答案

Valgrind 基本上告诉您系统调用“futex”传递了一个未初始化的参数“utime”。

现在这可能有两个原因:

  1. std::__atomic_futex_unsigned()确实传递了一个未初始化的参数
  2. Valgrind 系统调用机制过于严格,错误地将系统调用参数 utime 标记为未初始化

您可以尝试使用 --trace-syscalls=yes 参数运行 Valgrind,并最终将您的查询发布到 Valgrind 用户别名。

关于multithreading - 使用 std::future 和 valgrind 的一次性事件通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32170031/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com