What is the conclusion?
Can I lock the file until my thread finish writing on it? and how?
What is the conclusion?
Can I lock the file until my thread finish writing on it? and how?
Sure, but the solution can be platform dependant depending on HOW you want to lock the file.
I work on Linux.
I need to lock the file until my thread finish writing on it. Then unlock it.
You still have not defined HOW you want to lock it.
For example, do you want to lock it to prevent your other threads from writing to the same file? Or writing and reading? Or prevent your other thread from even opening the file? Or prevent any application in the entire system from opening the file? Or perhaps allowing some apps to read the file but not write to it? etc...
Yes , this what I want to lock it for.do you want to lock it to prevent your other threads from writing to the same file?
I am sorry if I was not clear.
On Unix systems there is no mandatory locking which means in a general case you cannot prevent someone else from writing to the same file while you have it open. It requires cooperation of all involved sides and use of advisory locking (e.g. using flock). Some filesystems may implement mandatory locking (through fcntl calls) but others will not and on some systems (like Linux) mandatory locks are unreliable (see the man page for fcntl).
Malek (28th December 2010)
Bookmarks