0

I have a vector that is constantly being updated (about 5-20 times a second) with new information by a process that I wrote to gather the information. I want to write a loop that would be able to detect a change in the vector, and then read the information and do the appropriate analysis that I write for that event. However, I know that there are many issues that arise from multithreading like this, so I'm curious what the best ways to do something like this are.

The vector is stored in a public class and is being updated by a feed we get from a financial company (the information being updated is futures indices).

cmh
  • 10,612
  • 5
  • 30
  • 40
weskpga
  • 2,017
  • 7
  • 29
  • 43
  • To clear up some confusion, can you explain your architecture a little more? Is this a single process with multithreading, or multiple processes? – Rook Jul 03 '12 at 13:17
  • I'm sorry, it was multiple processes. That's my bad. – weskpga Jul 03 '12 at 13:20
  • 1
    It's usually (but not always) more trouble than it's worth to share data structures between processes. You might be better off for example having your gathering process write a series of changes to its own standard output, then your analysis process reads them. That way you turn a problem with interprocess data-sharing into a problem with ensuring that the gathering process never ends up blocked because its stdout buffer is full. Still not trivial, but usually an easier problem. – Steve Jessop Jul 03 '12 at 13:22
  • Perhaps you can update the feed data asynchronously using [boost::asio](http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio.html) – Gigi Jul 03 '12 at 13:25
  • Have you thought about how you would implement the IPC (inter-process communication) (shared mem, raw TCP/IP, named pipes, stdout, COM, CORBA, etc...)? This will most likely affect how you would implement your reading/writing the data to your shared structures. – Dan Jul 03 '12 at 14:43

2 Answers2

3

I don't see a way to do this in C++. If you used shared memory you could store the vector in it, but its components would still be pointing at local, non-shared memory chunks, preventing it from being shared (you might be able to work around with with allocator trickery but I'm not sure it would work).

boost has some shared memory capabilities but as I recall from a question I can't find it has some downsides. You'll most likely have to design your own shared memory data structure and provide a mechanism to store a condition variable there so clients can wait on it to determine when new data is available.

EDIT: I found the question asking about boost::interprocess:

Is boost::interprocess ready for prime time?

Community
  • 1
  • 1
Mark B
  • 95,107
  • 10
  • 109
  • 188
  • As usual, `boost` to the rescue ;) There are interprocess containers as well as synchronization primatives. http://www.boost.org/doc/libs/1_35_0/doc/html/boost/interprocess/vector.html – Chad Jul 03 '12 at 14:04
-1

Sorry, but in such a case, I would suggest changing the design: make the vector private, add a Setter function (wrapping push_back() or other) that will be used by your data feed, and youre done. Or did I miss something ?

kebs
  • 6,387
  • 4
  • 41
  • 70