草庐IT

shared_mutex

全部标签

c++ - unique_ptr 与 shared_ptr 中的删除器类型

这个问题在这里已经有了答案:Whydoesunique_ptrtaketwotemplateparameterswhenshared_ptronlytakesone?(2个回答)关闭7年前。当我发现标准以两种完全不同的方式定义了std::unique_ptr和std::shared_ptr时,我觉得这非常奇怪可能拥有。这是来自cppreference::unique_ptr的声明和cppreference::shared_ptr:template>classunique_ptr;templateclassshared_ptr;如您所见,unique_ptr将Deleter对象的类型“保

c++ - 使用 mingw 和 g++ 4.7.2 让 std::thread/mutex 在 Win7 下工作

您好,我最近搬出我的unix避难所来测试一个所谓的跨平台网络库,结果发现mingw不喜欢吃c++11的东西。我认为我缺少所需的header,因为Win7不包含c++11支持。用VS2012编译就好了但g++拒绝。error:'thread'innamespace'std'doesnotnameatypeerror:'mutex'innamespace'std'doesnotnameatype问题是:如何在不使用VS2012安装提供的情况下获取c++11头文件/库的拷贝,即。附:#1我试过mingw-getupdate但还是找不到附言#2我也在使用-std=c++11真诚的,克里斯。

c++ - 使用 mingw 和 g++ 4.7.2 让 std::thread/mutex 在 Win7 下工作

您好,我最近搬出我的unix避难所来测试一个所谓的跨平台网络库,结果发现mingw不喜欢吃c++11的东西。我认为我缺少所需的header,因为Win7不包含c++11支持。用VS2012编译就好了但g++拒绝。error:'thread'innamespace'std'doesnotnameatypeerror:'mutex'innamespace'std'doesnotnameatype问题是:如何在不使用VS2012安装提供的情况下获取c++11头文件/库的拷贝,即。附:#1我试过mingw-getupdate但还是找不到附言#2我也在使用-std=c++11真诚的,克里斯。

c++ - 你能分配一个与 make_shared 等效的数组吗?

buffer=newchar[64];buffer=std::make_shared(char[64]);???你能用make_shared()为数组分配内存吗??我可以:buffer=std::make_shared(newchar[64]);但这仍然涉及调用new,据我了解make_shared更安全、更高效。 最佳答案 您需要共享分配的内存吗?您可以改用std::unique_ptr和std::make_unique在C++14上可用:autobuffer=std::make_unique(64);会有一个std::make_

c++ - 你能分配一个与 make_shared 等效的数组吗?

buffer=newchar[64];buffer=std::make_shared(char[64]);???你能用make_shared()为数组分配内存吗??我可以:buffer=std::make_shared(newchar[64]);但这仍然涉及调用new,据我了解make_shared更安全、更高效。 最佳答案 您需要共享分配的内存吗?您可以改用std::unique_ptr和std::make_unique在C++14上可用:autobuffer=std::make_unique(64);会有一个std::make_

c++ - 将带有自定义删除器的 unique_ptr 移动到 shared_ptr

我有一个函数可以创建一个带有自定义删除器的unique_ptr并返回它:autogive_unique_ptr(){autodeleter=[](int*pi){deletepi;};int*i=newint{1234};returnstd::unique_ptr(i,deleter);}在该函数的客户端代码中,我想将unique_ptr移动到shared_ptr中,但鉴于我不知道该怎么做在函数之外不知道我的自定义删除器的decltype。我猜它应该是这样的:autouniquePtr=give_unique_ptr();autosharedPtr=std::shared_ptr(st

c++ - 将带有自定义删除器的 unique_ptr 移动到 shared_ptr

我有一个函数可以创建一个带有自定义删除器的unique_ptr并返回它:autogive_unique_ptr(){autodeleter=[](int*pi){deletepi;};int*i=newint{1234};returnstd::unique_ptr(i,deleter);}在该函数的客户端代码中,我想将unique_ptr移动到shared_ptr中,但鉴于我不知道该怎么做在函数之外不知道我的自定义删除器的decltype。我猜它应该是这样的:autouniquePtr=give_unique_ptr();autosharedPtr=std::shared_ptr(st

c++ - 如何断言 std::mutex 是否被锁定?

使用GCC4.8.2(在Linux/Debian/Sid64位上)-或GCC4.9(如果可用)-在C++11中-我有一些互斥体std::mutexgmtx;实际上,它是某个类Foo中的static成员,其中包含alpha和beta方法如下。它被锁定在alpha之类的voidalpha(void){std::lock_guardg(gmtx);beta(void);//someotherwork}我想检查beta确实gmtx已锁定:voidbeta(void){assert(gmtx.is_locked());//somerealwork}(注意is_locked只在assert内部被调

c++ - 如何断言 std::mutex 是否被锁定?

使用GCC4.8.2(在Linux/Debian/Sid64位上)-或GCC4.9(如果可用)-在C++11中-我有一些互斥体std::mutexgmtx;实际上,它是某个类Foo中的static成员,其中包含alpha和beta方法如下。它被锁定在alpha之类的voidalpha(void){std::lock_guardg(gmtx);beta(void);//someotherwork}我想检查beta确实gmtx已锁定:voidbeta(void){assert(gmtx.is_locked());//somerealwork}(注意is_locked只在assert内部被调

c++ - shared_ptr 与非指针资源

在C++11中是否可以使用shared_ptr来控制非指针资源?可以使用unique_ptr来管理非指针资源。这是通过实现一个自定义删除器类来完成的,该类提供:一个typedef{TYPE}指针;其中{TYPE}是非指针资源类型operator()(pointer)释放受控资源...然后使用自定义删除器作为第二个模板参数实例化一个unique_ptr。例如,在Windows下,可以创建一个unique_ptr来管理servicecontrolhandle.这个句柄类型不是通过调用delete来释放的,而是通过调用CloseServiceHandle()来释放的。.这是执行此操作的示例代