访问 class 个成员而不复制此变量或互斥变量
Access class members without copying this or mutex variable
我正在尝试创建一个简单的 Worker class,它将创建多个线程和一组线程任务 运行。这是有教育意义的,但我已经尝试询问 class 伙伴并寻找不同的答案,我认为这里缺少一些 "simple"/基础知识。
我在设置互斥量变量的位置时遇到了一些问题。我相信我希望我的每个 Worker 对象都有一个,因为每个对象创建的线程是完全不相关的。现在,我正在尝试将 this
传递给添加到线程集的 lambda 方法,但这会复制互斥量,后者会删除它。有没有办法独立复制我需要的每个 class 成员,或者我应该创建一个复制构造函数?我只是想将我需要的 class 变量传递给 lambda。这是我的代码:
// TDAT2004Task2.cpp : Defines the entry point for the application.
//
#include <functional>
#include <iostream>
#include <list>
#include <vector>
#include <mutex>
#include <thread>
using namespace std;
class Worker {
int threads;
mutable mutex tasks_mutex;
vector<thread> worker_threads;
list<function<void()>> tasks;
public:
Worker(int threadAmount) {
threads = threadAmount;
}
void start() {
for (int i = 0; i < threads; i++) {
//This is the place errors occur. I'm copying the this element, and thus deleting my
//mutex variable.
worker_threads.emplace_back([this] {
while (true) {
function<void()> task;
{
lock_guard<mutex> lock(tasks_mutex);
if (!tasks.empty()) {
task = *tasks.begin();
tasks.pop_front();
}
}
if (task) {
task();
}
}
});
}
for (auto& thread : worker_threads) {
thread.join();
}
}
void post(function<void()> function) {
lock_guard<mutex> lock(tasks_mutex);
tasks.emplace_back(function);
}
};
int main()
{
//This does not compile.
Worker worker_threads = Worker(4);
}
我在 VS 中的错误信息:
>------ Build All started: Project: TDAT2004Task2, Configuration: x64-Debug ------
[1/2] C:\PROGRA~2\MICROS~219\COMMUN~1\VC\Tools\MSVC24~1.283\bin\HostX64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /FoTDAT2004Task2\CMakeFiles\TDAT2004Task2.dir\TDAT2004Task2.cpp.obj /FdTDAT2004Task2\CMakeFiles\TDAT2004Task2.dir\ /FS -c ..\..\..\TDAT2004Task2\TDAT2004Task2.cpp
FAILED: TDAT2004Task2/CMakeFiles/TDAT2004Task2.dir/TDAT2004Task2.cpp.obj
C:\PROGRA~2\MICROS~219\COMMUN~1\VC\Tools\MSVC24~1.283\bin\HostX64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /FoTDAT2004Task2\CMakeFiles\TDAT2004Task2.dir\TDAT2004Task2.cpp.obj /FdTDAT2004Task2\CMakeFiles\TDAT2004Task2.dir\ /FS -c ..\..\..\TDAT2004Task2\TDAT2004Task2.cpp
C:\Users\Even\source\repos\TDAT2004Task2\TDAT2004Task2\TDAT2004Task2.cpp(74): error C2280: 'Worker::Worker(const Worker &)': attempting to reference a deleted function
..\..\..\TDAT2004Task2\TDAT2004Task2.cpp(69): note: compiler has generated 'Worker::Worker' here
..\..\..\TDAT2004Task2\TDAT2004Task2.cpp(69): note: 'Worker::Worker(const Worker &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::mutex::mutex(const std::mutex &)'
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.24.28314\include\mutex(92): note: 'std::mutex::mutex(const std::mutex &)': function was explicitly deleted
ninja: build stopped: subcommand failed.
控制台中来自 cpp 的错误消息:
C:\Users\Even\source\repos\TDAT2004Task2\TDAT2004Task2>gcc TDAT2004Task2.cpp
TDAT2004Task2.cpp: In function 'int main()':
TDAT2004Task2.cpp:74:34: error: use of deleted function 'Worker::Worker(Worker&&)'
Worker worker_threads = Worker(4);
^
TDAT2004Task2.cpp:18:7: note: 'Worker::Worker(Worker&&)' is implicitly deleted because the default definition would be ill-formed:
class Worker {
^~~~~~
TDAT2004Task2.cpp:18:7: error: use of deleted function 'std::mutex::mutex(const std::mutex&)'
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/mutex:43:0,
from TDAT2004Task2.cpp:13:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/std_mutex.h:97:5: note: declared here
mutex(const mutex&) = delete;
^~~~~
感谢您的任何回复!请指出我犯过的任何愚蠢错误,我只是想学习最佳实践。
问题是,您定义了一个复制构造函数。因此,您没有默认的移动构造函数。
我不知道你怎么能有一个正确的复制构造函数,因为 std::thread
不能被复制,所以 std::vector<std::thread>
不能被复制。
默认移动构造函数应该适合您的情况。
所以要明确添加行:
public:
Worker(const Worker&) = delete;
Worker& operator=(const Worker&) = delete;
Worker(Worker&&) = default;
Worker& operator=(Worker&&) = default;
致您的工作人员 class。
我正在尝试创建一个简单的 Worker class,它将创建多个线程和一组线程任务 运行。这是有教育意义的,但我已经尝试询问 class 伙伴并寻找不同的答案,我认为这里缺少一些 "simple"/基础知识。
我在设置互斥量变量的位置时遇到了一些问题。我相信我希望我的每个 Worker 对象都有一个,因为每个对象创建的线程是完全不相关的。现在,我正在尝试将 this
传递给添加到线程集的 lambda 方法,但这会复制互斥量,后者会删除它。有没有办法独立复制我需要的每个 class 成员,或者我应该创建一个复制构造函数?我只是想将我需要的 class 变量传递给 lambda。这是我的代码:
// TDAT2004Task2.cpp : Defines the entry point for the application.
//
#include <functional>
#include <iostream>
#include <list>
#include <vector>
#include <mutex>
#include <thread>
using namespace std;
class Worker {
int threads;
mutable mutex tasks_mutex;
vector<thread> worker_threads;
list<function<void()>> tasks;
public:
Worker(int threadAmount) {
threads = threadAmount;
}
void start() {
for (int i = 0; i < threads; i++) {
//This is the place errors occur. I'm copying the this element, and thus deleting my
//mutex variable.
worker_threads.emplace_back([this] {
while (true) {
function<void()> task;
{
lock_guard<mutex> lock(tasks_mutex);
if (!tasks.empty()) {
task = *tasks.begin();
tasks.pop_front();
}
}
if (task) {
task();
}
}
});
}
for (auto& thread : worker_threads) {
thread.join();
}
}
void post(function<void()> function) {
lock_guard<mutex> lock(tasks_mutex);
tasks.emplace_back(function);
}
};
int main()
{
//This does not compile.
Worker worker_threads = Worker(4);
}
我在 VS 中的错误信息:
>------ Build All started: Project: TDAT2004Task2, Configuration: x64-Debug ------
[1/2] C:\PROGRA~2\MICROS~219\COMMUN~1\VC\Tools\MSVC24~1.283\bin\HostX64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /FoTDAT2004Task2\CMakeFiles\TDAT2004Task2.dir\TDAT2004Task2.cpp.obj /FdTDAT2004Task2\CMakeFiles\TDAT2004Task2.dir\ /FS -c ..\..\..\TDAT2004Task2\TDAT2004Task2.cpp
FAILED: TDAT2004Task2/CMakeFiles/TDAT2004Task2.dir/TDAT2004Task2.cpp.obj
C:\PROGRA~2\MICROS~219\COMMUN~1\VC\Tools\MSVC24~1.283\bin\HostX64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /FoTDAT2004Task2\CMakeFiles\TDAT2004Task2.dir\TDAT2004Task2.cpp.obj /FdTDAT2004Task2\CMakeFiles\TDAT2004Task2.dir\ /FS -c ..\..\..\TDAT2004Task2\TDAT2004Task2.cpp
C:\Users\Even\source\repos\TDAT2004Task2\TDAT2004Task2\TDAT2004Task2.cpp(74): error C2280: 'Worker::Worker(const Worker &)': attempting to reference a deleted function
..\..\..\TDAT2004Task2\TDAT2004Task2.cpp(69): note: compiler has generated 'Worker::Worker' here
..\..\..\TDAT2004Task2\TDAT2004Task2.cpp(69): note: 'Worker::Worker(const Worker &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::mutex::mutex(const std::mutex &)'
C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.24.28314\include\mutex(92): note: 'std::mutex::mutex(const std::mutex &)': function was explicitly deleted
ninja: build stopped: subcommand failed.
控制台中来自 cpp 的错误消息:
C:\Users\Even\source\repos\TDAT2004Task2\TDAT2004Task2>gcc TDAT2004Task2.cpp
TDAT2004Task2.cpp: In function 'int main()':
TDAT2004Task2.cpp:74:34: error: use of deleted function 'Worker::Worker(Worker&&)'
Worker worker_threads = Worker(4);
^
TDAT2004Task2.cpp:18:7: note: 'Worker::Worker(Worker&&)' is implicitly deleted because the default definition would be ill-formed:
class Worker {
^~~~~~
TDAT2004Task2.cpp:18:7: error: use of deleted function 'std::mutex::mutex(const std::mutex&)'
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/mutex:43:0,
from TDAT2004Task2.cpp:13:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/std_mutex.h:97:5: note: declared here
mutex(const mutex&) = delete;
^~~~~
感谢您的任何回复!请指出我犯过的任何愚蠢错误,我只是想学习最佳实践。
问题是,您定义了一个复制构造函数。因此,您没有默认的移动构造函数。
我不知道你怎么能有一个正确的复制构造函数,因为 std::thread
不能被复制,所以 std::vector<std::thread>
不能被复制。
默认移动构造函数应该适合您的情况。
所以要明确添加行:
public:
Worker(const Worker&) = delete;
Worker& operator=(const Worker&) = delete;
Worker(Worker&&) = default;
Worker& operator=(Worker&&) = default;
致您的工作人员 class。