具有模板化类成员函数的多线程 [英] Multithreading with templated class member function

查看:264
本文介绍了具有模板化类成员函数的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我对STL提供的C ++ 11并发编程功能还很陌生,并且正在使用以下代码:

So, I'm fairly new to the C++11 concurrent programming functionality provided by the STL and I was playing around with the following code:

    #include <iostream>
    #include <thread>
    #include <mutex>
    #include <list>

    using namespace std;

    template <typename T>
    class Container
    {
        private:
            mutex mu;
            list<T> myList;

        public:
            void add(T element)
            {
                lock_guard<mutex> lock1(mu);
                myList.emplace_back(element);
            }
            void remove()
            {
                lock_guard<mutex>lock2(mu);
                myList.pop_back();
            }
            void print()
            {
                for(const auto & element : myList)
                {
                    cout << element << endl;
                }
            }
    };

    int main()
    {
        Container<int> c;

        thread t1(&Container<int>::add, c, 5); //ERROR
        thread t2(&Container<int>::add, c, 10); //ERROR

        thread t4(&Container<int>::remove, c); //ERROR
        thread t5(&Container<int>::remove, c); //ERROR

        t1.join();
        t2.join();
        t4.join();
        t5.join();

        c.print();
    }

当我尝试编译代码时,我标记为"ERROR"的行使编译器告诉我:

When I try to compile my code, the lines I've marked "ERROR" caused the compiler to tell me:

error: call to implicitly-deleted copy constructor of
  'typename decay<Container<int> &>::type' (aka 'Container<int>')
return _VSTD::forward<_Tp>(__t);

error: no matching constructor for initialization of
  '__tuple_leaf<1UL, Container<int> >'
        __tuple_leaf<_Uf, _Tf>(_VSTD::forward<_Up>(__u))...,

error: no matching function for call to '__decay_copy'
                            __decay_copy(_VSTD::forward<_Args>(__args))...));
                            ^~~~~~~~~~~~

现在,我已经查看了这个问题和这个问题,但我仍然缺少一些小细节.如果有人可以提供帮助,那将是很棒的.谢谢!

Now, I've looked at this question and this question while I was writing my code, but I'm still missing some small detail. If someone could provide some help, that would be brilliant. Thanks!

推荐答案

thread需要复制其所有参数,并且您的Container是不可复制的.它是不可复制的,因为其成员之一(std::mutex)是不可复制的.解决方案不是直接给thread c,而是给它可以复制的内容.

A thread needs to make a copy of all of its arguments and your Container is non copyable. It is non-copyable because one of its members (the std::mutex) is non-copyable. The solution to this is rather than give the thread c directly is to give it something that it can make a copy of.

也就是说:

    thread t1(&Container<int>::add, &c, 5);

以下内容也应正常工作,但可能不起作用(请参阅

The following should work as well, but may not (see T.C.'s comment):

    thread t2(&Container<int>::add, std::ref(c), 10);

请注意,这没有为您编译是一件好事,因为否则您的线程将在容器的各种副本上执行工作-而不是您可能期望的那样.

Note that it's a good thing that this didn't compile for you, because otherwise your threads would be performing work on various copies of your container - rather than just the one as you likely expected.

这篇关于具有模板化类成员函数的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆