Concurrent updates of the same file

J

John Smith

I have a C++ program which appends new text to a file. Multiple
instances of this program may run concurrently, hence there is a
possiblity that 2 or more instances (say C1 and C2) of this C++ program
will be modifying the file at the same time.

I want to make sure that C1 can finish appending it's changes to the
file, before C2 starts to append its changes.

How do I handle this concurrently update issue so that the changes made
by C1 and C2 are not interpersed with each other's?
 
B

Ben Pope

John said:
I have a C++ program which appends new text to a file. Multiple
instances of this program may run concurrently, hence there is a
possiblity that 2 or more instances (say C1 and C2) of this C++ program
will be modifying the file at the same time.

I want to make sure that C1 can finish appending it's changes to the
file, before C2 starts to append its changes.

How do I handle this concurrently update issue so that the changes made
by C1 and C2 are not interpersed with each other's?

I don't think you can, using C++ alone. The word "concurrent" gives it
away, since C++ does not support concurrency or threads.

You'll have to use OS specific means to lock the file during the write.
There should be a suitable group to ask that question in. It would be
off topic here.

Ben Pope
 
R

roberts.noah

Ben said:
I don't think you can, using C++ alone. The word "concurrent" gives it
away, since C++ does not support concurrency or threads.

You'll have to use OS specific means to lock the file during the write.
There should be a suitable group to ask that question in. It would be
off topic here.

Well, you can always create a lock file and do such checking yourself.
 
J

John Smith

when C2 recognizes the lock file created by C1, how can I make C2
"busy-wait" until the lock file is removed by C1?
how about using an "infinite" while loop?
 
D

Default User

Well, you can always create a lock file and do such checking yourself.



Please explain how you eliminate race conditions with standard C++
mechanisms.



Brian
 
J

Jim Langston

John Smith said:
I have a C++ program which appends new text to a file. Multiple
instances of this program may run concurrently, hence there is a
possiblity that 2 or more instances (say C1 and C2) of this C++ program
will be modifying the file at the same time.

I want to make sure that C1 can finish appending it's changes to the
file, before C2 starts to append its changes.

How do I handle this concurrently update issue so that the changes made
by C1 and C2 are not interpersed with each other's?

Normally this is done by some type of file locking.

One way would be to use mutexes that are recognizable between processes
(windows has them, not sure if *nix does).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top