这是我的代码:
#include <thread>
#include <chrono>
using namespace std::literals::chrono_literals;
#include <iostream>
void f(int n)
{
for (int cnt = 0; cnt < n; ++cnt) {
std::this_thread::sleep_for(1s);
std::cout << "." << std::flush;
}
std::cout << std::endl;
}
int main()
{
std::thread t1 = std::thread(f, 5);
//t1.join();
t1 = std::thread(f, 5); // <- should abort if t1.join() is not done
t1.join();
}
Run Code Online (Sandbox Code Playgroud)
对于该网站,我使用 gcc9.2 执行器来查看未加入线程被破坏时会发生什么,但这是编译器输出选项卡的内容:
Could not execute the program
Compiler returned: 1
Compiler stderr
/tmp/ccjlEO57.o: In function `std::thread::thread<void (&)(int), int&, void>(void (&)(int), …Run Code Online (Sandbox Code Playgroud) 下面是一个程序,完全展示了我所看到的问题.
首先,我从一个使用其他类型的分组定义的对象开始,我开始使用std :: tuple <>来管理分组.
template <typename> class object;
template <typename... Rs> class object<std::tuple<Rs...> > {
};
Run Code Online (Sandbox Code Playgroud)
我打算这些对象能够将类型void分散在"包"中.我已经意识到无法"实例化"这种类型的元组(请参阅std :: tuple中的Void类型)
我想传递这些对象,也许复制/移动它们......它们的数据成员都不是这些类型的元组.事实上,我可以使用上面的空对象定义重现问题.
我可以使用以下内容使其工作:
template <typename... Rs> struct TGrp {};
template <typename> class object;
template <typename... Rs> class object<TGrp<Rs...> > {
};
Run Code Online (Sandbox Code Playgroud)
这些类型的"分组"结构在可变递归中经常使用,并且它们意味着永远不会被创建/使用.只是为了分组模板args.
但是,我"希望""对象"的签名由"用户期望的"类型/名称组成.
基本上,当我习惯于"分组" 时,我正在尝试传递其中一个对象的任何可能的方法std::tuple,并且只能找到一种方法:自动lambdas.
谁能解释一下:
为什么"汽车"lambda可以为此工作?
关于延迟模板演绎的事情?比如diff b/w"auto"和"decltype(auto)"?
如何"设计"一个函数参数来接受其中一个对象.
- 感谢大家对这种奇怪的见解
例:
#include <tuple>
#include <iostream>
#define GRP std::tuple // IF 'tuple' used: compile error where …Run Code Online (Sandbox Code Playgroud)