dguco / shmqueue Goto Github PK
View Code? Open in Web Editor NEW基于c++内存池,共享内存和信号量实现高速的进程间通信队列,单进程读单进程写无需加锁,多进程读多进程写用信号量集实现读写锁保证读写安全
基于c++内存池,共享内存和信号量实现高速的进程间通信队列,单进程读单进程写无需加锁,多进程读多进程写用信号量集实现读写锁保证读写安全
你好,大神
我用您这个方法测试多进程通讯的时候发现了一个问题:
在多生产者,一个消费者的时候:比如原来一个生产者和一个消费者正在通讯(生产者每次生产+1的整数数据,生产者生产的快,消费者消费的慢),如果中途有一个新的生产者进程加入但是不生产,将会覆盖掉共享内存已有的数据,导致消费者读到的数据发生跳变。
如下,写了一个例程,消费者读的数据在新生产者创建时刻发生了跳变。
如图从34跳变到1294
通过代码发现可能是在每次创建新的生产者进程的时候都会调用
CMessageQueue::CreateInstance(SHAR_KEY_1, 10240, eQueueModel::ONE_READ_MUL_WRITE);
thread write_thread(remote_write_func, messQueue, 1, "remoteWrite");
进而调用
CMessageQueue::CMessageQueue(BYTE pCurrAddr, eQueueModel module, key_t shmKey, int shmId, size_t size)
{
m_pShm = (void) pCurrAddr;
m_pQueueAddr = pCurrAddr;
m_stMemTrunk = new (m_pQueueAddr) stMemTrunk();
m_pQueueAddr += sizeof(stMemTrunk);
m_stMemTrunk->m_iBegin = 0;
m_stMemTrunk->m_iEnd = 0;
m_stMemTrunk->m_iShmKey = shmKey;
m_stMemTrunk->m_iShmId = shmId;
m_stMemTrunk->m_iSize = (unsigned int)size;
m_stMemTrunk->m_eQueueModule = module;
InitLock();
}
此处将重置m_stMemTrunk->m_iBegin = 0; 和 m_stMemTrunk->m_iEnd = 0;
从而新创建进程的时候共享内存读写指针发生了重置,导致原本消费者未读的数据就没读到。
是这样的吗,我顺便向您的邮箱发了下我的测试代码,
您有空可以看下能帮忙解决下吗。
多谢
我在pc上运行没有问题,但是交叉编译会发生如下错误
arm-openwrt-linux-gnueabi-g++.bin: warning: environment variable 'STAGING_DIR' not defined
/tmp/cck7LQDM.s: Assembler messages:
/tmp/cck7LQDM.s:249: Error: bad instruction sfence' /tmp/cck7LQDM.s:759: Error: bad instruction
sfence'
CMakeFiles/shmqueue.dir/build.make:62: recipe for target 'CMakeFiles/shmqueue.dir/shmmqueue.cpp.o' failed
make[2]: *** [CMakeFiles/shmqueue.dir/shmmqueue.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/shmqueue.dir/all' failed
make[1]: *** [CMakeFiles/shmqueue.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
谢谢分享这么好的代码。
main里面的测试使用的是多线程。
可否添加多进程测试的用例?
如果用于多进程处理,还需要修改源码
进程间的通信,考虑互斥与激活,与线程之间共享队列等待与激活,不同。从你的main中write的count与read的count总和是不同的。
你这个写的很好,受你的这个启发,写了一个跨平台的,有兴趣可以交流一下
对应内容我放在知乎上了。
https://zhuanlan.zhihu.com/p/483000525
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.