mikewei / shm_container Goto Github PK
View Code? Open in Web Editor NEWA collection of shared memory containers for high performance server
A collection of shared memory containers for high performance server
看了下ShmQueue 应该不是线程安全的,即使在单读单写下,问题出在你用一个volatile变量在控制一块内存,C++里面volatile只保证每次取值一定是去内存那边取,但是指令乱序没有保证的,在x86架构下,可能这个代码不会跑出问题,但是在其他(arm,power)这样的架构下很容易跑出问题,所以为了安全起见,还是用c++11的atomic吧。
shmc/shm_hash_map.h:288:24: error: cannot bind ‘const volatile std::__cxx11::basic_string’ lvalue to ‘std::__cxx11::basic_string&&’
每种数据结构的设计能够给个图不,代码很好。
if (buf_size_bytes < (kOverwriteBufferSize * 2) && buf_size_bytes > 0x800000000UL / 2^32 x 8B /) 这里 && 做判断值应该时存在BUG的,作者想对入参buf_size_bytes做校验,应该需要用 ||
请教下,一个进程去写:
shmc::ShmHashMap<const char *, shmc::POSIX> hash_map_;
shmc::POSIX alloc_;
hash_map_.InitForWrite(kShmKey, kKeyNum, kNodeSize, kNodeNum);
const char *key = "testkey";
while(1){
hash_map_.Replace(key, value.c_str());
sleep(1);
}
然后另外一个进程去读,
shmc::ShmHashMap<const char *, shmc::POSIX> hash_map_;
shmc::POSIX alloc_;
hash_map_.InitForRead(kShmKey);
const char *key = "testkey";
while(1){
std::string out;
if(hash_map_.Find(key, &out)){
printf("%s\n",out.c_str());
}
sleep(1);
}
但是为啥读的那个进程一直提示
ShmHashMap::Find: key not found
哪里出了问题呢?
hash_map 每次获取数据Find接口都是对原数据的一次拷贝,是否可设计成获取源数据,让使用者操作数据。比如我存入一个结构体,想修改里面的值,需要Find接口拷贝数据到std::sting 再通过指针转换成定义的结构体修改完数据后再replace 拷贝一次数据,这样是不是太麻烦了
you call InitForWrite in ShmQueue::InitForRead, i think this is a bug.
template <class Alloc>
bool ShmQueue<Alloc>::InitForRead(const std::string& shm_key) {
if (shm_.is_initialized()) {
SHMC_ERR_RET("ShmQueue::InitForRead: already initialized\n");
}
if (!shm_.InitForWrite(shm_key, sizeof(ShmHead), kNoCreate)) {
SHMC_ERR_RET("ShmQueue::InitForRead: shm_.InitForRead(%s, %lu) fail\n",
shm_key.c_str(), sizeof(ShmHead));
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.