我试图在收到SIGINT信号时正确地终止我的多线程C++ 11应用程序(^C它是),但由于某种原因它不会传播到子线程,尽管主线程很好地响应它.
对于instnce(如下面的代码示例中所示),如果我们在线程内部有一些阻塞函数(例如sleep()),^C如果你使用比如安装任何SIGNT钩子,它将取消你的所有控制权sigaction().
有没有办法解决或解决这种行为?有没有办法将主接收信号传播到子线程?
编辑:sleep()用C++ 11 替换POSIXstd::this_thread::sleep_for()
#include <iostream>
#include <thread>
#include <chrono>
#include <signal.h> // for sigaction() function
static int signaled = 0;
void threaded_foo() {
while (!signaled) {
// pressing ^C now will not lead to correct termination, because we are
// sleeping for a long time (100500 seconds) and do not respond to
// SIGINT for some tricky reason i am looking a bypassage for. …Run Code Online (Sandbox Code Playgroud) 我只是想知道C++ 11中是否有任何锁定策略可以防止线程饥饿.
我有一堆线程正在竞争一个互斥锁.现在,我的问题是,离开临界区的线程立即开始竞争相同的互斥锁,并且大部分时间都获胜.因此,等待互斥锁的其他线程正在挨饿.
我不想让线程,留下一个关键部分,睡眠一段时间,让其他线程有机会锁定互斥锁.
我认为必须有一些参数可以为等待互斥锁的线程启用公平锁定,但我无法找到任何合适的解决方案.
好吧,我找到了std :: this_thread :: yield()函数,它假设重新调度线程执行的顺序,但它只提示调度程序线程,如果重新调度线程,则依赖于调度程序线程实现.
有没有办法如何为在C++ 11中等待相同互斥锁的线程提供公平锁定策略?通常的策略是什么?
谢谢
我的gcc版本4.8.0中的以下代码失败:
#include <thread>
#include <cassert>
int main() {
std::thread::id nobody;
assert( nobody != std::this_thread::get_id() );
};
Run Code Online (Sandbox Code Playgroud)
这种行为是否正确?
C++ 11添加了别名模板,例如:
template<typename T> using identity = T;
template<bool b, typename T = void> using EnableIf = typename std::enable_if<b, T>::type;
Run Code Online (Sandbox Code Playgroud)
这些比template在::type字段中提供返回值的旧类型映射更容易使用,因为即使您的类型参数依赖于本地上下文,您也不需要通知编译器结果是类型.
实际上,您将typename使用位置提升到using别名.
有没有什么可以用来摆脱生产无关的templates?
假设您有一个元函数,其输出是类或别名模板而不是类型.目前的方法是这样的:
template<typename T>
struct my_meta {
template<typename U>
using Template = identity<U>;
};
template<typename T>
struct my_meta {
template<typename U>
using Template = int;
};
Run Code Online (Sandbox Code Playgroud)
我们可以这样使用:
template<typename T, typename U>
typename my_meta<T>::template Template<U>
do_stuff( U&& ) { return {}; }
Run Code Online (Sandbox Code Playgroud)
template返回类型中的额外关键字存在以消除我的元函数的返回值的歧义是我想要消除的.
有没有办法向编译器指示metacomputation的结果是C++ 11或C++ 1y中的另一个别名或类模板,而不template在调用位置使用关键字? …
我已经使用C++几年了,但我还没有找到解决我经常遇到的问题的方法.知道如何解决它会很棒.
我现在所拥有的是:
// Client code:
switch(currentEnumValue)
{
case MyEnum::kValue01:
processData<MyEnum::kValue01>(data);
break;
case MyEnum::kValue02:
processData<MyEnum::kValue02>(data);
break;
default:
LOG("Invalid command");
break;
}
// Declarations
enum class MyEnum {kValue01, kValue02};
class MyClass
{
// code
template <MyEnum> void processData(char*); /* Implemented somewhere else */
}
template <> void MyClass::processData<MyEnum::kValue01>(char* data); /* Implemented somewhere else */
MyClass <> void MyClass::processData<MyEnum::kValue02>(char* data); /* Implemented somewhere else */
Run Code Online (Sandbox Code Playgroud)
我想删除开关因为很多原因.而不是我需要的东西,如:processData<runtime-decltype(currentEnumValue)>(data);
我知道关于typeid以及没有将编译时间和运行时混合在一起......但尽管如此,我还是希望找到一些解决方案,最好不包括宏.
这是代码,我希望解释我想要实现的目标.
vector<int> ints;
vector<double> doubles;
struct Arg {
enum Type {
Int,
Double
};
Type type;
int index;
};
template <typename F>
void Call(const F& f, const vector<Arg>& args) {
// TODO:
// - First assert that count and types or arguments of <f> agree with <args>.
// - Call "f(args)"
}
// Example:
void copy(int a, double& b) {
b = a;
}
int test() {
Call(copy, {{Int, 3}, {Double, 2}}); // copy(ints[3], double[2]);
}
Run Code Online (Sandbox Code Playgroud)
这可以在C++ 11中完成吗?
如果是,可以在C++ 14中简化解决方案吗?
编辑原始问题,因为我想问问题std::map,而不是std::vector.我的错.抱歉.
我的数据实际上是2 std::map秒.我想将两张地图合并到第三张地图中.
我的第一张和第二张地图包含具有相同键的条目.所以我想将这些键下的数据合并到我生成的第3张地图中.
所以如果我使用std::merge,我会丢失第一个或第二个地图常用条目中的数据吗?正如我所提到的,两张地图都有共同的数据(值)?
我了解到SFINAE可用于确定成员函数是否存在于类中.例如,以下代码可用于检查方法hello是否存在于类中.
struct has_method_hello {
using yes = char[1];
using no = char[2];
template <typename U, typename C>
static constexpr yes& test(decltype(&U::hello));
template <typename>
static constexpr no& test(...);
static constexpr bool value = (sizeof(yes) == sizeof(test<T>(nullptr)));
};
struct Foo {
void hello() {}
}
std::cout << has_method_hello <Foo> :: value << std::endl; // 1
Run Code Online (Sandbox Code Playgroud)
但是,假设hello是模板化的,我该如何修改技巧以使它仍能正常运行?
struct Foo {
template <typename T>
void hello(T&) {...}
}
Run Code Online (Sandbox Code Playgroud) Visual Studio 2015更新3改进了对C++ 11的支持,但我有一个奇怪的问题,我正在寻找解决方法.
当使用MSVC为模板类型参数("完全定义的类型")编译可变参数模板代码时,一切都很好,但是如果我想使用模板模板参数("部分定义的类型"),则结果变得不正确.
#include <iostream>
using namespace std;
template <template<typename> class... TS>
struct PARTIAL {
static void test(std::ostream& out)
{
out << "PARTIAL-PROBLEM" << endl;
}
};
template <template<typename> class T>
struct PARTIAL<T>{
static void test(std::ostream& out)
{out << "PARTIAL-OK-END" << endl;}
};
template <template<typename> class T, template<typename> class... TS>
struct PARTIAL<T, TS...>{
static void test(std::ostream& out)
{
out << "PARTIAL-OK" << endl;
PARTIAL<TS...>::test(out);
}
};
template <class... TS>
struct FULL {
static void test(std::ostream& out)
{
out …Run Code Online (Sandbox Code Playgroud) 我有一个包含部分的qt项目C++14.
最近,我改变了我的ubuntu发行版.现在我有16.04 LTS,我安装了Qt creator 4.02(建于Jun 13).
为了启用C++14编译,我输入了项目文件:
QMAKE_CXXFLAGS += -std=c++14
Run Code Online (Sandbox Code Playgroud)
但是,在构建项目时,IDE会生成以下命令:
g++ -c -pipe -std=c++14 -g -O0 -g -std=gnu++11 -Wall -W -D_REENTRANT ...
Run Code Online (Sandbox Code Playgroud)
如图所示,生成makefile的标志-std=gnu++11会覆盖标志C++14.我之前的发行版(LTS 12.04,同样的qt创建者版本)没有发生这种情况.
我试过了
CONFIG += -std=c++14
Run Code Online (Sandbox Code Playgroud)
但行为是一样的.
有人能给出任何线索吗?