Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing SAME text file using multiple threads 1

Status
Not open for further replies.

ece496

Programmer
Mar 12, 2004
3
0
0
CA
Hi,
My question is with regards to accessing the same text file from multiple threads. The code that each thread runs is in different files. Each thread has a write function. I have a main function (in a separate file) that initiates these threads. I want to open a file (perhaps using ofstream) in my main function, and want the threads I'm issuing to write to this SAME file when their write() function is called.

I have written a single-threaded application that writes to (using ofstream) and reads from (using ifstream) the same file, and then closes this file at the end. However, I'm having problems doing the above.

Any help on this would be highly appreciated.

Many many thanks for your help in advance.

Hoping to hear from you soon...
 
Hi again,
I forgot to mention that I'm doing this on Linux using C++.
Thanks again!
 
Alas, C++ Standard stream library totally ignores file sharing. It's impossible to deal with concurrent STL i/o stream access in all known (to me;) C++ implementations.
 
I'd suggest that you implement a writer thread to do the actual writing to the file. Client threads wanting to write to the file then send a message to the writer thread, which nicely serialises the problem.

The message queue does this, the writer thread just takes messages and writes data, client threads just write to the message queue.



--
 
Thanks ArkM and Salem for your response.

Salem, I like your suggestion. If you're familiar with C++ threads, would it be possible for you to give me a quick code sample? I'm really not sure how message passing and thread initiation works in C++.

Many thanks for your help in advance.
 
I recommend the wrox book linux programming for beginers. it covers the topics you need for this project.

after digesting this book you will be able to do what you want.

D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top