草庐IT

thread_siblings_list

全部标签

c++ - std::forward_list::remove_if 谓词的要求

考虑这段代码:structT{boolstatus;UsefulDatadata;};std::forward_listlst;lst.remove_if([](T&x)->bool{returnx.status=!x.status;});即一次性切换状态和删除非事件元素。根据cppreference上面的代码似乎是未定义的行为(强调我的):templatevoidremove_if(UnaryPredicatep);p-unarypredicatewhichreturnstrueiftheelementshouldberemoved.Thesignatureofthepredicat

c++ - LibCds:Michael Hashmap 和 Split Order List

我正在使用libcds他们实现了MichaelHashMap和Splitorderlist。根据我从文档中收集到的信息,我是如何实现它们的:包括:#include#includeusingnamespacecds;代码:classTestDs{public:virtualboolcontainsKey(intkey)=0;virtualintget(intkey)=0;virtualintput(intkey,intvalue)=0;virtualintremove(intkey)=0;virtualintsize()=0;virtualconstchar*name()=0;virtu

c++ 编译 std::thread 示例与 scons

我无法让scons正确编译一个小型线程示例(在Linux上)。如果我运行scons,它会这样做:jarrett@jarrett-laptop:~/projects/c++_threads$sconsscons:ReadingSConscriptfiles...scons:donereadingSConscriptfiles.scons:Buildingtargets...g++-obuild/main.o-c-std=c++11-pthread-Wall-gsrc/main.cppg++-obuild/c++threadsbuild/main.oscons:donebuildingta

c++ - 如何使用 std::forward_list 在恒定时间内进行范围拼接?

我想拼接范围[first,last],包括两个端点。我有元素beforefirst和last的迭代器。我可以使用splice_after()来完成,但只能在线性时间内完成。我相信这个拼接可以在恒定时间内完成。我如何使用std::forward_list完成它?如果问题不清楚,这里是显示我的问题的示例代码:LiveWorkSpace上的代码#include#include#include#includeusingnamespacestd;intmain(){forward_listtrg{'a','b','c'};forward_listsrc{'1','2','3','4'};auto

c++ - 如何从 std::list 实现 O(1) 删除

问题是使用std::list实现O(1)列表项删除的推荐方法是什么?通常,当我选择一个双向链表时,我希望能够在O(1)时间内从一个列表中删除一个元素,然后在O(1)时间内将它移动到另一个列表中。如果该元素有自己的prev和next指针,那么就没有真正的技巧来完成这项工作。如果列表是双向链接的循环列表,则删除不一定需要知道包含该项目的列表。根据Iteratorinvalidationrules,std::list迭代器非常耐用。因此,在我看来,在我自己的项目上使用std::list时,我想要得到的行为是在我的类和包含列表中存储一个迭代器。classItem{typedefstd::sha

c++ - thread_local成员变量构造

我遇到了thread_local的一些奇怪行为,不确定是我做错了什么还是GCC错误。我有以下最小重现场景:#includeusingnamespacestd;structbar{structfoo{foo(){cerr输出上面的注释行如下所示:main0Ideone取消注释后,它变成了这样:mainfoofoo4242Ideone我是不是漏掉了什么蠢东西?$gcc-vUsingbuilt-inspecs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapperTarget:x86_6

c++ - 从括号内的 initializer_list 构造时调用了错误的重载

我一直认为当我使用初始化列表C++语法时:something({...});编译器总是很清楚我想调用采用std::initializer_list的重载,但对于MSVC2015似乎不太清楚。我测试了这个简单的代码:#include#includenamespacetesting{templatestructTest{Test(){printf("Test::Test()\n");}explicitTest(size_tcount){printf("Test::Test(int)\n");}Test(std::initializer_listinit){printf("Test::Tes

c++ - 将参数传递给 std::thread 的区别,C++

引自NikolaiJosuttis-StandardLibraryC++11:Detachedthreadscaneasilybecomeaproblemiftheyusenonlocalresources.Theproblemisthatyoulosecontrolofadetachedthreadandhavenoeasywaytofindoutwhetherandhowlongitruns.Thus,makesurethatadetachedthreaddoesnotaccessanyobjectsaftertheirlifetimehasended.Forthisreason,

c++ - 主线程中 block 作用域静态与命名空间作用域 thread_local 的初始化和销毁​​顺序

我正在尝试了解在主线程的上下文中使用静态存储持续时间和线程本地存储持续时间来初始化和销毁​​命名空间范围和block范围对象的顺序规则。考虑这两个类:structFoo{Foo(){std::cout除了它们的静态实例成员函数的实现之外,它们是相同的:thread_localFoot_foo;Foo&Foo::instance(){returnt_foo;}Bar&Bar::instance(){staticBars_bar;returns_bar;}Bar是一个Meyers单例,一个具有静态存储持续时间的block范围对象。Foo的实例是具有线程本地存储持续时间的namespace范

c++ - 运算符== 和 list::remove()

测试.h#ifndefTEST_H#defineTEST_H#includetemplatebooloperator==(conststd::weak_ptr&wp1,conststd::weak_ptr&wp2){std::shared_ptrsp1;if(!wp1.expired())sp1=wp1.lock();std::shared_ptrsp2;if(!wp2.expired())sp2=wp2.lock();returnsp1==sp2;}#endif测试.cpp#include"Test.h"#includeintmain(){typedefstd::list>intLi