我有一个循环缓冲区,支持文件映射内存(缓冲区的大小范围为8GB-512GB).
我从开始到结束以顺序的方式写入(8个实例)此内存,此时它循环回到开头.
它工作正常,直到它需要执行两个文件映射并循环内存,此时IO性能完全被破坏并且无法恢复(即使在几分钟后).我无法弄明白.
using namespace boost::interprocess;
class mapping
{
public:
mapping()
{
}
mapping(file_mapping& file, mode_t mode, std::size_t file_size, std::size_t offset, std::size_t size)
: offset_(offset)
, mode_(mode)
{
const auto aligned_size = page_ceil(size + page_size());
const auto aligned_file_size = page_floor(file_size);
const auto aligned_file_offset = page_floor(offset % aligned_file_size);
const auto region1_size = std::min(aligned_size, aligned_file_size - aligned_file_offset);
const auto region2_size = aligned_size - region1_size;
if (region2_size)
{
const auto region1_address = mapped_region(file, read_only, 0, (region1_size + region2_size) * 2).get_address();
const auto region2_address …
Run Code Online (Sandbox Code Playgroud) 据我所知,我处理流错误的两个选项是perror
流异常.两种选择都是不可取的 原因如下:
PERROR
std::strerror
返回实现定义的消息.此消息在所有系统上并不总是有用
std::strerror
不是线程安全的
返回的指针std::strerror
可以无效
C库在技术上在C++中被"弃用".几乎总是有一个C++ - 等同于.当有潜在的C++等价物时,我没有理由不得不依赖POSIX和C.
流异常
例外并不适合每个计划
虽然std::strerror
有时可以提供有用的错误消息,但流异常从不提供有用的错误消息.随着f.exceptions(f.failbit)
,对于未能打开文件或无法提取,抛出的异常是std::ios_base::failure
和what()
是" basic_ios::clear
".
系统错误
更换std::ios_base::failure
与std::system_error
产生完全相同的结果.如果我们看看N2769: Detailed Reporting for Input/Output Library Errors (Revision 2)
,我们可以看到原因:
在抛出
ios_base::failure
异常时,鼓励实现提供ec
标识失败的具体原因的值.[ 注意 - 操作系统产生的错误通常会报告为system_category
错误,错误值为操作系统报告的错误编号.流库中产生的错误通常会报告为error_code(ioerrc::stream, iostream_category
) - 结束注释 ].上述措辞仅为规范性鼓励实施者做正确的事情,因此依赖于市场力量和实施者的良好内涵来产生对用户有用的结果.任何更强大的东西(例如改变"鼓励"到"应该")都需要更多的额外规范,并且超出了LWG可以为C++ 0x实际处理的范围.
另一种选择是可能依赖于庞大的第三方库(我正在看你Boost)或者手动滚动它(除非你知道你正在做什么,否则你永远不想做.)我正在寻找C++标准库的方式来做到这一点.有没有?
我如何正则表达式转义动态输入的字符串.我想用实际的正则表达式代码包围然后做匹配,但我需要输入转义中的所有正则表达式特殊字符.
我想反序列化这个json:
json1 = {
"age" : "22",
"name" : "Bob",
"lastname" : "Andrew",
"contactList" : [
{ "friend" : "Alice"},
{ "friend" : "John"}
]}
Run Code Online (Sandbox Code Playgroud)
我创建了以下类(我不想创建任何模型,因为我对将它们保存在数据库中不感兴趣):
class Friend(object):
def __init__(self, friend):
self.friend = friend
class Person(object):
def __init__(self, age , name , lastname, contactList):
self.age=age
self.name = name
self.lastname = lastname
self.contactList= [] #possible error here
Run Code Online (Sandbox Code Playgroud)
以及以下序列化程序:
class FriendSeriliazer(serializers.Serializer):
friend = serializers.CharField()
def create(self, validated_data):
return Friend(**validated_data)
class PersonSerializer(serializers.Serializer):
age = serializers.CharField()
name = serializers.CharField()
lastname = serializers.CharField()
contactList = …
Run Code Online (Sandbox Code Playgroud) 我的操作系统是:Mac OSX 10.11
当我执行命令时,我正在尝试在我的Mac上安装Phabricator
phabricator cpopt$ ./bin/phd start
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
"ERROR: The PHP extension 'pcntl' is not installed. You must install it to run daemons on this machine."
Run Code Online (Sandbox Code Playgroud)
我在谷歌搜索这个问题,我有很多解决方案,但它们都不适合我,我几乎绝望.
我怎样才能做到这一点?
我想知道是否偶然指向绑定到已销毁堆栈变量的const引用的指针可以正常工作.
我读取const引用生命周期是在rvalues上扩展,所以这是"正常"的const引用工作,但是在存储引用的ctor的末尾应该被销毁,不是吗?
const引用生命周期是否也被扩展,因为我在指针中检索它的地址或者这是纯粹的运气?
#include <iostream>
class Storage
{
public:
Storage(const int& ref)
{
p = &ref;
}
const int* Get() const
{
return p;
}
private:
const int* p;
};
int main()
{
Storage* s = nullptr;
{
int someValue = 42;
std::cout << &someValue << std::endl;
s = new Storage(someValue);
}
const int* p = s->Get();
std::cout << p << std::endl;
std::cout << *p << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它也使用struct Live示例
#include <iostream>
struct Dummy
{ …
Run Code Online (Sandbox Code Playgroud) 我需要在我的类层次结构中插入克隆并创建成员函数
class Base
{
protected:
const int x_;
public:
Base() : x_(0) {}
Base(int x) : x_(x) {}
};
Run Code Online (Sandbox Code Playgroud)
我认为 CRTP 可能是节省一些打字并避免错误的方法。
template <typename Derived>
class CRTP_Iface : public Base
{
public:
virtual Base *create() const { return new Derived(); }
virtual Base *clone() const { return new Derived(static_cast<Derived const&>(*this)); }
};
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法访问基类构造函数来初始化 const 成员。
class D1 : public CRTP_Iface<D1>
{
public:
D1() : Base() {}
D1(int x) : Base(x) {}
};
class D2 : public CRTP_Iface<D2>
{
public:
D2() …
Run Code Online (Sandbox Code Playgroud) 为什么我们需要的组合版本std::min_element
,并std::max_element
为std::minmax_element
?它只是用于保存比较或其背后的其他好处吗?
我写下面的代码,我不明白为什么复制构造函数被调用.
#include <iostream>
using namespace std;
class abc
{
public:
abc()
{
cout << "in Construcutor" << (this) << endl;
};
~abc()
{
cout << "in Destrucutor" << (this) << endl;
};
abc(const abc &obj)
{
cout << "in copy constructor" << (this) << endl;
cout << "in copy constructor src " << &obj << endl;
}
abc& operator=(const abc &obj)
{
cout << "in operator =" << (this) << endl;
cout << "in operator = src " << &obj …
Run Code Online (Sandbox Code Playgroud) c++ ×7
c++11 ×2
boost ×1
crtp ×1
django ×1
file-mapping ×1
interprocess ×1
noexcept ×1
pcntl ×1
phabricator ×1
php ×1
python ×1
reference ×1
regex ×1
windows ×1