Reputation: 3647
I'm working on a Multi Threaded application programmed in C++. I uses some temporary files to pass data between my threads. One thread writes the data to be processed into files in a directory. Another thread scans the directory for work files and reads the files and process them further, then delete those files. I have to use these files , because if my app gets killed when , i have to retain the data which has not been processed yet.
But i hate to be using multiple files. I just want to use a single file. One thread continuously writing to a file and other thread reading the data and deleting the data which has been read. Like a vessel is filled from top and at bottom i can get and delete the data from vessel. How to do this efficiently in C++ , first is there a way ..?
Upvotes: 4
Views: 563
Reputation: 42
you can write data that was process line per line and delimeter for each line indicate if this record processing or not
Upvotes: 0
Reputation: 1822
As was suggested in the comments to your questions using a database like SQLite may be a very good solution. However if you insist on using a file then this is of course possible.
I did it myself once - created a persistent queue on disk using a file.
Here are the guidelines on how to achieve this:
And here is the schemes for reading and writing (probably with some small logical bugs but you should be able to take it from there):
READER
You may also want the reader to compact the file for you once in a while:
WRITER
Hope this sets you on the write track
Upvotes: 3
Reputation: 8587
The win32Api function CreateFileMapping()
enables processes to share data, multiple processes can use memory-mapped files that the system paging file stores.
A few good links:
http://msdn.microsoft.com/en-us/library/aa366551(VS.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspx
http://www.codeproject.com/Articles/34073/Inter-Process-Communication-IPC-Introduction-and-S
http://www.codeproject.com/Articles/19531/A-Wrapped-Class-of-Share-Memory
http://www.bogotobogo.com/cplusplus/multithreaded2C.php
Upvotes: 2