Gibs
Gibs

Reputation: 774

Mutex declared as private member generates an error but not when it is global

Why am I getting an error when I try to place std::mutex mtx inside an object? When it is declared as global there is no error. Is there something wrong with my syntax?

The error says:

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

This is my code

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

Upvotes: 0

Views: 288

Answers (1)

Praetorian
Praetorian

Reputation: 109149

std::thread's constructor copies or moves its arguments. std::mutex is neither copyable nor moveable, so including that as a non-static data member of XHuman makes that class non-copyable and immoveable. That's the cause of the errors you're seeing.

You can get around it by passing a pointer or a reference to the XHuman instances.

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

Upvotes: 4

Related Questions