声明为私有成员的互斥锁会生成错误,但不会生成错误

时间:2017-08-16 05:39:47

标签: c++ c++11 mutex

当我尝试将std::mutex mtx置于对象中时,为什么会出现错误?当它被声明为全局时,没有错误。我的语法有问题吗?

错误说:

std::tuple<void (__thiscall XHuman::* )(int),XHuman,int>::tuple(std::tuple<void (__thiscall XHuman::* )(int),XHuman,int> &&)': cannot convert argument 1 from 'void (__thiscall XHuman::* )(int)' to 'std::allocator_arg_t

std::tuple<void (__thiscall XHuman::* )(int,int),XHuman,int,int>::tuple': no overloaded function takes 4 arguments

这是我的代码

#include "stdafx.h"
#include <vector>
#include <Windows.h>
#include <thread>
#include <mutex>


class XHuman
{
private:
    std::vector<int> m_coordinates;
    std::mutex mtx;

public:
    XHuman() {
        printf("Initialized XHuman\n");
        for (int i = 0; i < 5; ++i){
            m_coordinates.push_back(i);
        }
    }
    std::vector<int> Coordinates() { return m_coordinates; }
    void operator()() {
        printf("hello\n");
    }

    void addValues(int val, int multiple)
    {
        std::lock_guard<std::mutex> guard(mtx);
        for (int i = 0; i < multiple; ++i){
            m_coordinates.push_back(val);
            printf("pushed_back %d\n", val);
            Sleep(100);
        }
        printf("m_coordinates.size() = %d\n", m_coordinates.size());
    }

    void eraseValues(int multiple)
    {
        std::lock_guard<std::mutex> guard(mtx);
        for (int i = 0; i < multiple; ++i) {
            m_coordinates.pop_back();
            printf("m_coordinates.size() = %d\n", m_coordinates.size());
        }
    }
};

int main()
{
    std::thread th1(&XHuman::addValues, XHuman(), 1, 5);
    std::thread th2(&XHuman::eraseValues, XHuman(), 1);
    th1.join();
    th2.join();
    return 0;
}

1 个答案:

答案 0 :(得分:4)

std::thread&#39;} constructor复制或移动其论点。 std::mutex既不可复制也不可移动,因此包括XHuman的非静态数据成员使该类不可复制且不可移植。这就是您所看到错误的原因。

您可以通过将指针或引用传递给XHuman实例来解决它。

XHuman one, two;
std::thread th1(&XHuman::addValues, &one, 1, 5);
std::thread th2(&XHuman::eraseValues, std::ref(two), 1);