boost之Interprocess库,进程间通讯库之condition/mutex/scoped_lock
来源:互联网 发布:mac打底唇膏 编辑:程序博客网 时间:2024/06/09 17:19
公共头文件doc_anonymous_condition_shared_data.h:
#include <boost/interprocess/sync/interprocess_mutex.hpp>#include <boost/interprocess/sync/interprocess_condition.hpp>struct trace_queue{enum { LineSize = 100 };trace_queue() : message_in(false) { }//boost::interprocess::interprocess_mutex mutex;boost::interprocess::interprocess_condition cond_empty;boost::interprocess::interprocess_condition cond_full;//char items[LineSize];bool message_in;};
写进程:
#include <iostream>#include <string>using namespace std;#include <boost/interprocess/shared_memory_object.hpp>#include <boost/interprocess/mapped_region.hpp>#include <boost/interprocess/sync/scoped_lock.hpp>#include "doc_anonymous_condition_shared_data.h"using namespace boost::interprocess;int main(int argc, char*argv[]){struct shm_remove{shm_remove() { shared_memory_object::remove("MySharedMemory"); }~shm_remove() { shared_memory_object::remove("MySharedMemory"); }};shared_memory_object shm(create_only, "MySharedMemory", read_write);shm.truncate(sizeof(trace_queue));mapped_region region(shm, read_write);void *addr = region.get_address();trace_queue *data = new (addr) trace_queue;for(int i=0; i<100; i++){scoped_lock<interprocess_mutex> lock(data->mutex);if(data->message_in)data->cond_full.wait(lock);if(99 == i) sprintf(data->items, "%s", "last message");else sprintf(data->items, "%s_%d", "my_trace", i);cout<<data->items<<endl;data->cond_empty.notify_one();data->message_in = true;}return 0;}
读进程:
#include <iostream>#include <string>using namespace std;#include <boost/interprocess/shared_memory_object.hpp>#include <boost/interprocess/mapped_region.hpp>#include <boost/interprocess/sync/scoped_lock.hpp>#include "doc_anonymous_condition_shared_data.h"using namespace boost::interprocess;void main(int argc, char *argv[]){shared_memory_object shm(open_only, "MySharedMemory", read_write);mapped_region region(shm, read_write);void *addr = region.get_address();trace_queue *data = static_cast<trace_queue*>(addr);bool end_loop = false;do {scoped_lock<interprocess_mutex> lock(data->mutex);if(!data->message_in)data->cond_empty.wait(lock);cout<<data->items<<endl;if(0 == strcmp(data->items, "last message")){end_loop = true;}else{data->message_in = false;data->cond_full.notify_one();}} while (!end_loop);shm.remove("MySharedMemory");}
- boost之Interprocess库,进程间通讯库之condition/mutex/scoped_lock
- boost之Interprocess库,进程间通讯库之共享内存
- Boost-Boost.Interprocess进程间通讯库的了解
- (Boost)mutex scoped_lock
- boost::mutex::scoped_lock
- boost多线程之scoped_lock理解
- boost: Mutex、scoped_lock 使用方式
- boost之路 八 进程间通讯
- boost interprocess之message queue
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- boost mutex condition
- boost interprocess 进程通信
- boost 进程间通讯
- 关于Boost.Interprocess 进程间通信的问题
- Java JNI的一个笔记
- ie下jquery.getJSON的缓存问题的处理方法
- Java synchronized详解
- C#程序设计--输入一个整数,按数字反顺序输出
- window.opener.location.reload() and href()的区别
- boost之Interprocess库,进程间通讯库之condition/mutex/scoped_lock
- 电脑ping权限的设置
- window mysql的sql文件到linux下执行,中文乱码
- web开发中文数据写入到数据库乱码问题
- iOS Client 与WebSocket 通信(一)
- jQuery源码学习
- Java中channel的应用总结
- mpi中利用自定义归约操作实现merge
- 自己总结线程并发的访问资源的解决方法