使用std::future和valgrind进行一次事件通信



免责声明:这不是一个关于c++ 11线程库的误报的问题。

我写了一个简单的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 heren", num);
        /* Doing something useful. */
        printf("Worker number %d is donen", 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用户别名。

最新更新