草庐IT

atomic_shared_ptr

全部标签

c++ - "pseudo-atomic"C++ 操作

所以我知道在C++中没有什么是原子的。但我试图弄清楚是否有任何我可以做出的“伪原子”假设。原因是我想避免在一些我只需要非常弱的保证的简单情况下使用互斥锁。1)假设我已经全局定义了volatileboolb,它最初我设置为true。然后我启动一个执行循环的线程while(b)doSomething();同时,在另一个线程中,我执行b=true。我可以假设第一个线程会继续执行吗?换句话说,如果b开始时为真,并且第一个线程在第二个线程分配b=true的同时检查b的值,我是否可以假设第一个线程将b的值读取为真?或者是否有可能在赋值b=true的某个中间点,b的值可能被读取为false?2)现在

c++ - 分配给 std::shared_ptr 成员变量

我有一个类foo,与成员bar类型std::shared_ptr:classfoo{std::shared_ptrbar;/*otherstuffhere*/};在那个类(class)我想分配一个newint至bar.但是我不会写bar=newint();因为指针没有公共(public)赋值运算符。应该如何我这样做?我可以std::move或std::swap但这些似乎都不对。 最佳答案 bar=std::make_shared();是一种方法,特别是如果您希望保留赋值运算符的易处理性。

c++ - 分配给 std::shared_ptr 成员变量

我有一个类foo,与成员bar类型std::shared_ptr:classfoo{std::shared_ptrbar;/*otherstuffhere*/};在那个类(class)我想分配一个newint至bar.但是我不会写bar=newint();因为指针没有公共(public)赋值运算符。应该如何我这样做?我可以std::move或std::swap但这些似乎都不对。 最佳答案 bar=std::make_shared();是一种方法,特别是如果您希望保留赋值运算符的易处理性。

c++ - g++编译错误: `.rodata' can not be used when making a shared object; recompile with -fPIC

我正在使用命令:g++--std=c++11-fPIC-Iincludesparser.cpplib/main-parser.olib/lib.a在Debian9上编译C++程序。但我收到以下错误消息:/usr/bin/ld:lib/lib.a(csdocument.o):重定位R_X86_64_32反对'.rodata'制作共享对象时不能使用;使用-fPIC重新编译/usr/bin/ld:最终链接失败:输出中不可表示的部分collect2:错误:ld返回1个退出状态我已经看到了线程:Compilationfailswith"relocationR_X86_64_32against`.

c++ - g++编译错误: `.rodata' can not be used when making a shared object; recompile with -fPIC

我正在使用命令:g++--std=c++11-fPIC-Iincludesparser.cpplib/main-parser.olib/lib.a在Debian9上编译C++程序。但我收到以下错误消息:/usr/bin/ld:lib/lib.a(csdocument.o):重定位R_X86_64_32反对'.rodata'制作共享对象时不能使用;使用-fPIC重新编译/usr/bin/ld:最终链接失败:输出中不可表示的部分collect2:错误:ld返回1个退出状态我已经看到了线程:Compilationfailswith"relocationR_X86_64_32against`.

c++ - 将 std::shared_ptr 与 clang++ 和 libstdc++ 一起使用

我正在尝试使用libstdc++(4.6.1)在clang++(clang版本3.1(trunk143100))中使用std::shared_ptr。我有一个小演示程序:#includeintmain(){std::shared_ptrsome(newint);std::shared_ptrother(some);return0;}可以使用:clang++-std=c++0x-omainmain.cpp并给出以下错误输出:main.cpp:6:23:error:calltodeletedconstructorof'std::shared_ptr'std::shared_ptrother

c++ - 将 std::shared_ptr 与 clang++ 和 libstdc++ 一起使用

我正在尝试使用libstdc++(4.6.1)在clang++(clang版本3.1(trunk143100))中使用std::shared_ptr。我有一个小演示程序:#includeintmain(){std::shared_ptrsome(newint);std::shared_ptrother(some);return0;}可以使用:clang++-std=c++0x-omainmain.cpp并给出以下错误输出:main.cpp:6:23:error:calltodeletedconstructorof'std::shared_ptr'std::shared_ptrother

c++ - 插入 std::vector 时 std::unique_ptr 的正确用法是什么

这个问题在这里已经有了答案:WhycanInotpush_backaunique_ptrintoavector?(2个回答)关闭6年前。我想在我的类中有一个指向对象的指针vector。为了避免为它创建析构函数,我想使用std::unique_ptr,因为对象是在我的类中创建/拥有/销毁的,但是我有一个我无法理解的编译器错误。下一个代码将作为我的问题的简短示例:std::unique_ptrcreatePtr(intvalue){std::unique_ptrptr(newint(value));returnptr;};intmain(){std::vector>vec;vec.push

c++ - 插入 std::vector 时 std::unique_ptr 的正确用法是什么

这个问题在这里已经有了答案:WhycanInotpush_backaunique_ptrintoavector?(2个回答)关闭6年前。我想在我的类中有一个指向对象的指针vector。为了避免为它创建析构函数,我想使用std::unique_ptr,因为对象是在我的类中创建/拥有/销毁的,但是我有一个我无法理解的编译器错误。下一个代码将作为我的问题的简短示例:std::unique_ptrcreatePtr(intvalue){std::unique_ptrptr(newint(value));returnptr;};intmain(){std::vector>vec;vec.push

c++ - 如何使用 std::make_shared 避免大内存分配

假设我有一些任意的类,A:classA{//...stuff};我想调用一个外部API,它接收指向某种类型的共享指针,就像这样(我无法更改此接口(interface))://...muchlatervoidfoo(std::shared_ptr_a){//operateon_aasashared_ptr}但是,在我正在使用的(遗留)代码中,我正在使用的类A实例被分配在堆栈上(我无法绕过):Aa;//...somestuffona//Nowtimetocallfoo除此之外,A类的实例非常大,每个实例大约1GB。我知道我可以打电话foo(std::make_shareda);但这会为A的