带有可可对象的线程中产卵线程
Spawning threads in a thread with callable object
我已经多次看到了这个问题,而且看来它发生在Windoes(Visual Studio(和Linux(GCC(中。这是它的简化版本:
class noncopyable
{
public:
noncopyable(int n);
~noncopyable();
noncopyable(const noncopyable&) = delete;
noncopyable& operator=(const noncopyable&) = delete;
noncopyable(noncopyable&&);
int& setvalue();
private:
int* number;
};
class thread_starter
{
public:
template<typename callable>
bool start(callable & o);
};
template<typename callable>
inline bool thread_starter::start(callable & o)
{
std::thread t(
[&]() {
int i = 10;
while (i-- > 0)
{
noncopyable m(i);
std::thread child(o, std::move(m));
child.detach();
}
});
return true;
}
class callable
{
public:
virtual void operator()(noncopyable &m);
};
void callable::operator()(noncopyable & m) { m.setvalue()++; }
int main()
{
thread_starter ts;
callable o;
ts.start(o);
}
代码似乎足够合法,但它不编译。
在 Visual Studio 中,它将给出:
error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
在 gcc 中,它将给出:
error: no type named ‘type’ in ‘class std::result_of<callable(int)>’....
我认为我知道问题是一种复制或引用机制的一种形式,但是所有语法似乎都合适。
我缺少什么?
我已经更改了示例,为混乱而道歉。我正在尝试尽可能纯净地重新创建问题,但我自己也不完全理解。
std::thread
的调用会创建一个元组。元组不能用参考初始化。因此,您必须使用伪造参考,std::ref(i)
将其编译并使用Int-Refs int&
。
template <typename callable>
bool thread_starter::start(callable &o)
{
// Nonsense
std::thread t(
[&]() {
int i = 10;
while (i-- > 0)
{
std::thread child(o, std::ref(i));
child.detach();
}
});
return true;
}
但是,结果代码没有意义。产卵线与时循环竞争。循环减少索引i
,而线程则尝试将其递增。不能保证这些事情何时发生。增量和减少不是原子。lambda完成后,线程可能会尝试增加索引。
您实际想做什么?
我制作了一个类似的程序来弄清楚发生了什么。这就是外观:
class mynoncopy
{
public:
mynoncopy(int resource)
: resource(resource)
{
}
mynoncopy(mynoncopy&& other)
: resource(other.resource)
{
other.resource = 0;
}
mynoncopy(const mynoncopy& other) = delete;
mynoncopy& operator =(const mynoncopy& other) = delete;
public:
void useResource() {}
private:
int resource;
};
class mycallablevaluearg
{
public:
void operator ()(mynoncopy noncopyablething)
{
noncopyablething.useResource();
}
};
class mycallableconstrefarg
{
public:
void operator ()(const mynoncopy& noncopyablething)
{
//noncopyablething.useResource(); // can't do this becuase of const :(
}
};
class mycallablerefarg
{
public:
void operator ()(mynoncopy& noncopyablething)
{
noncopyablething.useResource();
}
};
class mycallablervaluerefarg
{
public:
void operator ()(mynoncopy&& noncopyablething)
{
noncopyablething.useResource();
}
};
class mycallabletemplatearg
{
public:
template<typename T>
void operator ()(T&& noncopyablething)
{
noncopyablething.useResource();
}
};
当您发出std::thread(callable, std::move(thenoncopyableinstance))
时,这两件事将使用模板魔术在内部发生:
使用您的可呼叫和所有args创建一个元组。
std::tuple<mycallablerefarg, mynoncopy> thetuple(callable, std::move(thenoncopyableinstance));
在这种情况下将复制可呼叫。std::invoke()
用于调用可可,使用移动语义从元组传递给它。
std::invoke(std::move(std::get<0>(thetuple)), std::move(std::get<1>(thetuple)));
由于使用了移动语义,因此可以期望可召唤作为参数接收RVALUE参考(在我们的情况下为mynoncopy&&
(。这将我们限制为以下参数签名:
-
mynoncopy&&
-
const mynoncopy&
-
T&&
其中t是模板参数 -
mynoncopy
不是参考(这将称为Move-Constructor(
这些是使用不同类型的可可的汇编结果:
mynoncopy testthing(1337);
std::thread t(mycallablerefarg(), std::move(testthing)); // Fails, because it can not match the arguments. This is your case.
std::thread t(mycallablevaluearg(), std::move(testthing)); // OK, because the move semantics will be used to construct it so it will basically work as your solution
std::thread t(mycallableconstrefarg(), std::move(testthing)); // OK, because the argument is const reference
std::thread t(mycallablervaluerefarg(), std::move(testthing)); // OK, because the argument is rvalue reference
std::thread t(mycallabletemplatearg(), std::move(testthing)); // OK, because template deduction kicks in and gives you noncopyablething&&
std::thread t(std::bind(mycallablerefarg(), std::move(testthing))); // OK, gives you a little bit of call overhead but works. Because bind() does not seem to use move semantics when invoking the callable
std::thread t(std::bind(mycallablevalue(), std::move(testthing))); // Fails, because bind() does not use move semantics when it invokes the callable so it will need to copy the value, which it can't.
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- 带有可可对象的线程中产卵线程