Regarding Locking The File
Page |
1 |
Hi All
I have created a DLL in QT which contains two functions.
1.)Read File
2.)Write File
This DLL is used by more than 1 application.
So the problem Iam facing is all applications are trying to write file at same time.
So is there any way by which we can lock or allow one application at a time to write or read.
Give Sample Code.
Regards
Indrajeet
38 replies
QSystemSemaphore [doc.qt.nokia.com]
I guess you find the answer in this sequence of text taken from the detailed description
QSystemSemaphore [doc.qt.nokia.com]
- “A semaphore is a generalization of a mutex. While a mutex can be locked only once, a semaphore can be acquired multiple times. Typically, a semaphore is used to protect a certain number of identical resources.
Like its lighter counterpart QSemaphore, a QSystemSemaphore can be accessed from multiple threads. Unlike QSemaphore, a QSystemSemaphore can also be accessed from multiple processes. This means QSystemSemaphore is a much heavier class, so if your application doesn’t need to access your semaphores across multiple processes, you will probably want to use QSemaphore.”
QMutex is more light-weight. It is used for synchronization between different threads.
2 Apps running on 2 different machines access same file located at some location.
the following recommendation is then the method.
You could place a lock file somewhere, where both machines can access it.
However, I doubt that Qt will have something for you. Probably, you have to make the mechanism your self. Is the access from different machines on a frequent basis? If not, you may use a simple technique such as an additional file indicating taht the main file is locked. But it looks like a lot of traps.
You don’t have to exchange data between more processes. Just use aquire [doc.qt.nokia.com] to lock some memory block and release [doc.qt.nokia.com] to unlock that memory block.
What’s the help?
The idea is to share a file somewhere. Two applications are acessing the file, but since the applications are not hosted on the same machine, they do not share memory.
What’s the help?
The idea is to share a file somewhere. Two applications are acessing the file, but since the applications are not hosted on the same machine, they do not share memory.
Sorry, I did not see: “2 Apps running on 2 different machines”
In first post @Indrajeet not mention this situation.
What about setPermissions [doc.qt.nokia.com] ? You can set QFile::ReadOwner | QFile::WriteOwner permission when you have access and QFile::ReadOther | QFile::WriteOther when you finish to read / write.
You must log in to post a reply. Not a member yet? Register here!




