可能重复:
何时使用volatile多线程?
我有两个引用相同的线程boost::shared_ptr:
boost::shared_ptr<Widget> shared;
Run Code Online (Sandbox Code Playgroud)
在线程正在旋转,等待另一个线程重置boost::shared_ptr:
while(shared)
boost::thread::yield();
Run Code Online (Sandbox Code Playgroud)
在某些时候,另一个线程会调用:
shared.reset();
Run Code Online (Sandbox Code Playgroud)
我的问题是我是否需要声明共享指针volatile以防止编译器优化shared.operator bool()对循环的调用并且从不检测更改?我知道如果我只是循环变量,等待它达到0我需要volatile,但我不确定是否boost::shared_ptr以这样的方式实现它是不必要的.
编辑:我完全清楚条件变量可以用来以不同的方式解决这个问题.但在这种情况下,繁忙的循环非常罕见,并且争用条件变量的锁定是我们宁愿不会产生的开销.
以下是否有任何陷阱;
if (someCondition)
throw boost::shared_ptr<SomeException>( new SomeException( "foo!" ) );
...
catch( const boost::shared_ptr<SomeException>& expRef )
{
}
Run Code Online (Sandbox Code Playgroud) 以下是我正在阅读的C++书中的一些代码,展示了如何创建自己的智能指针:
template <typename T>
class SuperSmartPointer
{
public:
explicit SuperSmartPointer(T* inPtr);
virtual ~SuperSmartPointer();
SuperSmartPointer(const SuperSmartPointer<T>& src);
SuperSmartPointer<T>& operator=(const SuperSmartPointer<T>& rhs);
const T& operator*() const;
const T* operator->() const;
T& operator*();
T* operator->();
operator void*() const { return mPtr; }
protected:
T* mPtr;
static std::map<T*, int> sRefCountMap;
void finalizePointer();
void initPointer(T* inPtr);
};
template <typename T>
std::map<T*, int> SuperSmartPointer<T>::sRefCountMap;
template <typename T>
SuperSmartPointer<T>::SuperSmartPointer(T* inPtr)
{
initPointer(inPtr);
}
template <typename T> SuperSmartPointer<T>::SuperSmartPointer(const SuperSmartPointer<T>& src)
{
initPointer(src.mPtr);
}
template <typename T>
SuperSmartPointer<T>& SuperSmartPointer<T>::operator=(const …Run Code Online (Sandbox Code Playgroud) 在开发树状数据结构时,我写了这样的内容:
#include <memory>
class Node: public std::enable_shared_from_this<Node> {
public:
void set_root(Node & n);
private:
std::shared_ptr<Node> root;
std::shared_ptr<Node> leaf;
};
void Node::set_root(Node & n) {
root = n.shared_from_this();
n.leaf = shared_from_this();
}
int main() {
Node n1, n2;
n1.set_root(n2);
}
Run Code Online (Sandbox Code Playgroud)
代码使用clang编译,但会中断运行时间(“ libc ++ abi.dylib:以类型为std :: ____ :: bad_weak_ptr:bad_weak_ptr的未捕获异常终止)”为什么?
编辑 因此,基于答案,我想出了一个似乎可行的版本:
#include <memory>
class Node;
typedef std::shared_ptr<Node> Node_SP;
class Node: public std::enable_shared_from_this<Node> {
public:
void set_root(Node & n);
private:
std::shared_ptr<Node> root;
std::shared_ptr<Node> leaf;
};
void Node::set_root(Node & n) …Run Code Online (Sandbox Code Playgroud) 当使用工厂创建对象时,例如在下面的示例中,在某些情况下,shared_ptr显然被返回过程中被删除的对象被删除(在调试期间,对象被创建好,但是当它被分配给this->xs异常时被抛出) .当我更改工厂方法以返回原始指针时,作为代码的Link::xs成员unique_ptr运行正常.幕后发生了什么shared_ptr导致它以这种方式行事?它与shared_ptr<CrossSection>包裹Circular物体的事实有关吗?使用MS Visual C++ 2012进行了测试.
class Link
{
private:
std::shared_ptr<xs::CrossSection> xs;
public:
void parseXsection(const std::vector<std::string>& parts);
std::shared_ptr<xs::CrossSection> getXs() { return this->xs; }
};
void Link::parseXsection(const std::vector<std::string>& parts)
{
this->xs = xs::Factory::create(parts[1]);
}
namespace xs
{
class CrossSection
{
};
class Circular : public CrossSection
{
};
class Dummy : public CrossSection
{
};
class Factory
{
public:
static std::shared_ptr<CrossSection> create(const std::string& type);
};
std::shared_ptr<CrossSection> Factory::create(const std::string& …Run Code Online (Sandbox Code Playgroud) 我正在尝试从我的vector中弹出我的shared_pointer并转换为unique_ptr.不幸的是,它给出了一个奇怪的编译信息.
IFCCB.cpp:
std::unique_ptr<IFC> IFCCCB::getElementVectorIFC()
{
return (std::unique_ptr<IFC>(make_unique<IFC>(m_shpVectorIFC.pop_back())));
}
Run Code Online (Sandbox Code Playgroud)
IFCCB.h:
public:
unique_ptr<IFC> getElementVectorIFC();
Run Code Online (Sandbox Code Playgroud)
编译错误:
错误C2784:'enable_if :: value,std :: unique_ptr <_Ty,std :: default_delete <_Ty >>> :: type std :: make_unique(_Types && ...)':无法推断'_Types &&的模板参数'from'void'
据我所知,我正在做我在别处看到的事情.
我查看了make_unique信息,但它没有给出一个非常好的示例,并且使用了unique_ptr.有任何想法吗?
我正在制作一个粒子系统,我正在努力构建我的代码.这个想法是用户可以创建一个或多个ParticleEmitter通过ParticleManager对象传递给对象的ofxCurlNoise对象.
现在,我希望当用户更新ParticleEmitters对象时,ParticleManager对象会看到所做的更改.所以我使用了共享指针,但是我在不同的时间都有分段错误,无论是使用一个ParticleEmitter(程序启动时的分段错误)还是vector<ParticleEmitter>(程序退出时的分段错误).
这有什么问题?做我正在做的事情的设计模式是什么?
ofApp.h
#include "ofxCurlNoise.h"
class ofApp : public ofBaseApp{
// ParticleEmitter particleEmitter;
vector<ParticleEmitter> particleEmitters;
ofxCurlNoise curlNoise;
public:
void setup();
};
Run Code Online (Sandbox Code Playgroud)
ofApp.cpp
#include "ofApp.h"
void ofApp::setup(){
// This produces a segfault as soon as the program starts
// particleEmitter.setup();
// curlNoise.setup(particleEmitter, 1024*256);
// This produces a segfault when the program exits
ParticleEmitter emitter;
emitter.setup();
particleEmitters.push_back(emitter);
curlNoise.setup(particleEmitters, 1024*256);
}
Run Code Online (Sandbox Code Playgroud)
ofxCurlNoise.h
#include "ParticleManager.h"
class ofxCurlNoise {
ParticleManager …Run Code Online (Sandbox Code Playgroud) 标题几乎传达了所有相关信息,但这里是一个最小的重复:
#include <atomic>
#include <cstdio>
#include <memory>
int main() {
auto ptr = std::make_shared<int>(0);
bool is_lockless = std::atomic_is_lock_free(&ptr);
printf("shared_ptr is lockless: %d\n", is_lockless);
}
Run Code Online (Sandbox Code Playgroud)
使用以下编译器选项进行编译会产生无锁shared_ptr实现:
g++ -std=c++11 -march=native main.cpp
Run Code Online (Sandbox Code Playgroud)
虽然这不是:
g++ -std=c++11 -march=native -pthread main.cpp
Run Code Online (Sandbox Code Playgroud)
GCCversion :( 5.3.0在Linux上,使用libstdc++),在多台机器上进行测试,这些机器应具有必要的原子指令才能使其工作.
有没有办法强制实现无锁实现(我需要无锁版本,无论性能如何)?
我需要一个实现共享数据语义的类,并且可能std::shared_ptr是一个很好的起点.我认为这种类的典型实现可以使用私有shared_ptr的共享数据,然后至少实现复制构造函数和operator=.
就像是:
class SharedDataClass {
public:
SharedDataClass(const SharedDataClass& other)
{
data_ = other.data_;
};
SharedDataClass& operator=(const SharedDataClass& other)
{
data_ = other.data_;
return *this;
}
private:
std::shared_ptr<DataType> data_;
};
Run Code Online (Sandbox Code Playgroud)
我想问一下是否有人批评上述实施.是否有任何其他成员/运营商应该实施以保持一致性?
在C++ move11/14中,对象可以通过或者smark指针传输.
(1)这是一个例子move:
class MoveClass {
private:
int *tab_;
int alloc_;
void Reset() {
tab_ = nullptr;
alloc_ = 0;
}
void Release() {
if (tab_) delete[] tab_;
tab_ = nullptr;
alloc_ = 0;
}
public:
MoveClass() : tab_(nullptr), alloc_(0) {}
~MoveClass() {
Release();
}
MoveClass(MoveClass && other) : tab_( other.tab_ ), alloc_( other.alloc_ ) {
other.Reset();
}
MoveClass & operator=(MoveClass && other) {
if (this == &other) return *this;
std::swap(tab_, other.tab_);
std::swap(alloc_, other.alloc_);
return *this; …Run Code Online (Sandbox Code Playgroud) c++ ×10
shared-ptr ×10
c++11 ×4
atomic ×1
boost ×1
lock-free ×1
move ×1
overloading ×1
pointers ×1
polymorphism ×1
unique-ptr ×1
vector ×1
visual-c++ ×1
volatile ×1