gpt4 book ai didi

c++ - 如何使用 Boost.Interprocess 将数据流式传输到其他应用程序?

转载 作者:可可西里 更新时间:2023-11-01 18:37:20 24 4
gpt4 key购买 nike

main 应用程序需要快速更新共享内存。

并且多个消费应用程序需要从共享内存中读取以更新流式数据。

mainconsuming 应用程序是不同的进程

如何使用 Boost.Interprocess 实现这个?

最佳答案

制作人:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/thread.hpp>
#include <iostream>

struct shared_data_t {
boost::uint32_t data;
boost::interprocess::interprocess_mutex mutex;
};

/***************************************************************************/

/* producer */
int main(int argc, char** argv) {
const char* shname = "_unique_object_name_";
boost::shared_ptr<const char> remover(
shname,
boost::interprocess::shared_memory_object::remove
);
try {
boost::interprocess::shared_memory_object shared_object(
boost::interprocess::create_only,
shname,
boost::interprocess::read_write
);
shared_object.truncate(sizeof(shared_data_t));
boost::interprocess::mapped_region region(
shared_object,
boost::interprocess::read_write
);
shared_data_t* data = new(region.get_address())shared_data_t;
assert(data);
const boost::uint32_t count = 0x1000;
for ( boost::uint32_t idx = 0; idx < count; ++idx ) {
{ boost::interprocess::scoped_lock<
boost::interprocess::interprocess_mutex
> lock(data->mutex);
data->data = idx;
}
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
} catch(boost::interprocess::interprocess_exception &e){
std::cout << e.what() << std::endl;
return 1;
}

return 0;
}

消费者:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/thread.hpp>

struct shared_data_t {
boost::uint32_t data;
boost::interprocess::interprocess_mutex mutex;
};

/***************************************************************************/

/* consumer */
int main(int argc, char** argv) {
try {
boost::interprocess::shared_memory_object shared_object(
boost::interprocess::open_only,
"_unique_object_name_",
boost::interprocess::read_only
);
shared_object.truncate(sizeof(shared_data_t));
boost::interprocess::mapped_region region(
shared_object,
boost::interprocess::read_only
);
shared_data_t* data = new(region.get_address())shared_data_t;
assert(data);
while ( true ) {
{ boost::interprocess::scoped_lock<
boost::interprocess::interprocess_mutex
> lock(data->mutex);
std::cout << "ping: " << data->data << std::endl;
}
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
}
} catch(boost::interprocess::interprocess_exception &e){
std::cout << e.what() << std::endl;
return 1;
}
return 0;
}

男人: http://www.boost.org/doc/libs/1_43_0/doc/html/interprocess/synchronization_mechanisms.html

关于c++ - 如何使用 Boost.Interprocess 将数据流式传输到其他应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3606051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com