Best way for interprocess communication in C++

前端 未结 5 370
悲&欢浪女
悲&欢浪女 2020-11-30 02:09

I have two processes one will query other for data.There will be huge amount of queries in a limited time (10000 per second) and data (>100 mb) will be transferred per seco

相关标签:
5条回答
  • 2020-11-30 02:45

    I'll second Marc's suggestion -- I'd not bother with boost unless you have a portability concern or want to do cool stuff like map standard container types over shared memory (in which case I'd definitely use boost).

    Otherwise, message queues and shared memory are pretty simple to deal with.

    0 讨论(0)
  • 2020-11-30 02:47

    If you do use shared memory you will have to decide whether or not to spin or not. I'd expect that if you use a semaphore for synchronization and storing data in shared memory you will not get much performance benefit compared to using message queues (at significant clarity degradation), but if you spin on an atomic variable for synchronization, then you have to suffer the consequences of that.

    0 讨论(0)
  • 2020-11-30 02:59

    One Word: Boost.InterProcess. If it really needs to be fast, shared memory is the way to go. You nearly have zero overhead as the operation system does the usual mapping between virtual and physical addresses and no copy is required for the data. You just have to lookout for concurrency issues.

    For actually sending commands like shutdown and query, I would use message queues. I previously used localhost network programming to do that, and used manual shared memory allocation, before i knew about boost. Damn if i would need to rewrite the app, I would immediately pick boost. Boost.InterProcess makes this more easy for you. Check it out.

    0 讨论(0)
  • 2020-11-30 03:05

    If your data consists of multiple types and/or you need things like mutex, use Boost. Else use a shared section of memory using #pragma data_seg or a memory mapped file.

    0 讨论(0)
  • 2020-11-30 03:07

    I would use shared memory to store the data, and message queues to send the queries.

    0 讨论(0)
提交回复
热议问题