草庐IT

run_in_thread

全部标签

c++ - 线程: Termination of infinite loop thread in c++

我试图编写一个线程,该线程将在我的主程序的后台运行并监视某事。在某个时候,主程序应该向线程发出信号以使其安全退出。这是一个最小示例,该示例以固定的时间间隔将本地时间写入命令行。#include#include#include#include#includeintfunc(bool&on){while(on){autot=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());std::coutfi=std::async(std::launch::async,func,on);std::this_thr

C++11 - 无法使用 std::thread 和 std::condition_variable 唤醒线程

当我试图通过另一个线程唤醒一个线程时遇到了一个问题。一个简单的生产者/消费者。代码下方。第85行是我不明白为什么它不起作用的地方。生产者线程填充std::queue并调用std::condition_variable.notify_one()而消费者线程正在等待NOTstd::queue.empty()。在此先感谢您的帮助#include#include#include#include#include#include//requestclassrequest:publicstd::mutex,publicstd::condition_variable,publicstd::queue{

c++ - STL 模板容器的 GDB 中的 "Cannot evaluate function -- may be in-lined"错误

我希望能够使用GDB从STL容器中获取地址并打印一对。例如,给定以下玩具程序:#includeintmain(){std::mapamap;amap.insert(std::make_pair(1,2));}我编译为:g++-ggdb3-O0-std=c++11-Wall-Wextra-pedantic-omain.outmain.cpp然后,当我尝试检查map的单个元素时,例如:pamap.begin()我得到:"Cannotevaluatefunction--maybein-lined"为什么会发生这种情况,我该如何解决?在Ubuntu20.04、GCC9.3.0、2.34中测试。

c++ - 在 C++11 智能指针中存储 std::thread

在C++11及更高版本中,像这样直接将std::thread存储为类的成员时,有什么优点或缺点:std::threadmy_thread;与像这样将std::shared_ptr或std::unique_ptr存储到线程相反:std::shared_ptrmy_thread_ptr;是否有任何代码选项比其他选项更好?或者没关系,只需2种不同的方式来处理线程对象。 最佳答案 使用指针(或智能指针)成员可能有一些不太常见的原因,但对于常见用法,似乎是std::thread要么不适用,要么本身就足够灵活:我们可能希望更好地控制对象的生命周

c++ - 如何在你的 C++ 项目中包含 boost::thread?

我需要做什么才能在我的项目中包含boost::thread?我已将整个线程文件夹复制到我的工作路径(我希望能够在多台计算机上运行它)并且我得到了fatalerrorC1083:Cannotopenincludefile:'boost/thread/detail/platform.hpp':Nosuchfileordirectory来自#include"thread/thread.hpp"行什么给了?编辑:即使我只是链接到安装预编译二进制文件的boost文件夹,并且我使用#include我明白了fatalerrorLNK1104:cannotopenfile'libboost_threa

c++ - 使用 std::shared_ptr 对象实例创建 boost::thread

我有以下两个代码段。第一个block按预期编译和工作。但是第二个block不编译。我的问题是,给定下面的代码,当尝试基于由shared_ptr代理的对象实例创建线程时,正确的语法是什么?#include#include#include#includestructfoo{voidboo(){}};intmain(){//Thisworks{foo*fptr=newfoo;boost::threadt(&foo::boo,fptr);t.join();deletefptr;}//Thisdoesn'twork{std::shared_ptrfptr(newfoo);boost::threa

C++ 错误 : ‘_mm_sin_ps’ was not declared in this scope

我正在尝试对将函数应用于数组的不同方法进行基准测试。为什么是https://software.intel.com/sites/landingpage/IntrinsicsGuide/#expand=3260,2124,4779,4779&cats=Trigonometry&text=_sin_mm_sin_ps在我的范围内未知,但_mm_sqrt_ps是?我如何让它为人所知?并编译无误。#include#include#include#include#include#include#include"immintrin.h"#includeintmain(){std::coutdis(-

C++ 标准 : default "const T& value" in vector constructor for type 'int'

explicitvector(size_typen,constT&value=T(),constAllocator&=Allocator());vectorvec(10);cout::const_iteratoriter=vec.begin();iter!=vec.end();++iter){coutVS2010的输出:vec.size:100000000000问题>:根据最新的C++标准,当我们使用vectorObject(size_type)定义一个vector对象时,默认的int值是多少?在这里你可以看到,VS2010输出0作为默认的int值。但我不知道这是否是C++标准所要求的

c++ - std::thread Visual Studio 2012 警告

我试图了解如何通过VisualStudio2012使用新的std::thread。我正在尝试编译以下代码。#include#includeclassscoped_thread{std::threadt_;public:explicitscoped_thread(std::thread&t):t_(std::move(t)){if(!t_.joinable())throwstd::logic_error("Nothread");}~scoped_thread(){t_.join();}private:scoped_thread(scoped_threadconst&);scoped_th

c++ - vector in struct 耗时吗?使用指针更好吗?

我在C++中有一个这样的结构:structMyStruct{someTypev1;someType2v2;someType3v3;someType4f1();std::vectormyVector;};它会经常以这样的形式使用://someprocess...afterwhichastd::vectorvec1isgeneratedMyStructmyStruct;myStruct.myVector=vec1;由于vec1比较大。我想知道通过执行分配myStruct.myVector=vec1;是否花费很多时间我是否应该在MyStruct中使用指向myVector的指针来使其更快?如何