小编Tra*_*pas的帖子

具有智能指针和析构函数的单例类被调用

我想创建一个单例类,这样当所有指向该类的指针都消失时,会调用析构函数。

#include <memory>
#include <iostream>

class MyClass {
public:
    uint8_t str[50]; //some random data
    MyClass() {LOG("constructor Called"); }
    ~MyClass() {LOG("destructor Called");}
    static std::shared_ptr<MyClass> &Get();

private:
    static std::shared_ptr<MyClass> instance;
};

std::shared_ptr<MyClass> MyClass::instance=NULL;


std::shared_ptr<MyClass> &MyClass::Get()
{
    if (instance == NULL)
    {
        instance= std::shared_ptr<MyClass>(new MyClass());
        return instance;
    }
    return instance;
}

int main()
{
    std::shared_ptr<MyClass> &p1 =MyClass::Get();

    printf("We have %" PRIu32, p1.use_count());
    if (1)
    {
        std::shared_ptr<MyClass> &p2 =MyClass::Get();//this should not  
                                                     //  create a new class
        printf("We have %" PRIu32, p1.use_count());  //this should be …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

5
推荐指数
1
解决办法
5540
查看次数

具有Cortex M3和M4的LDREX / STREX

我正在阅读LDREX和STREX以实现互斥。通过查看ARM参考手册:

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.100166_0001_00_zh/ric1417175928887.html

看起来LDREX / STREX仅存储地址粒度是整个内存空间,因此仅允许在最多一个32位寄存器上使用LDREX / STREX。

这是正确的还是我错过了什么?如果这样的话,会使LDREX / STREX非常有限。我的意思是您可以做一些映射的互斥锁,也许可以得到32个互斥锁。

是否有人在M3或M4上使用LDREX / STREX?如果是,他们如何使用它?

arm

1
推荐指数
1
解决办法
594
查看次数

标签 统计

arm ×1

c++ ×1

c++11 ×1