I have been thinking about that solution, but what if one of process that acquired acces to resources and created temp file crashed before it can delete this temp file? Other proces will be thinking, that resources are loocked.
I have been thinking about that solution, but what if one of process that acquired acces to resources and created temp file crashed before it can delete this temp file? Other proces will be thinking, that resources are loocked.
You can handle that by catching and handling SIGSEGV signal.
You can search in the forum there are several threads about it, or google it.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
SIGSEGV is a POSIX signal. I need multiplatform solution.
Well, then you will just have to implement the segmentation fault catching per system, and guard it with #ifdef <your OS>.
Just few more lines of code.
But any OS should have some sort of signal on segmentation fault, either POSIX or something else.
OR -
On windows, you can for example use exceptions, and raise a SIGSEGV your self, and then you need only one signal handler for POSIX and Windows.
http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
The SIGILL, SIGSEGV, and SIGTERM signals are not generated under Windows NT. They are included for ANSI compatibility. Thus you can set signal handlers for these signals with signal, and you can also explicitly generate these signals by calling raise.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
I rather write some os dependend code for mutex (CreateMutex() for Win and pthread for POSIX) than write segfaults handlers...
Create a protected resource that will determine if the resource you are after is available. Then regular lock() is fine because you only do the locking for a short moment.
Qt Code:
class Sync { public: dat = 0; if(mem.create(sizeof(bool))) dat = (bool*)mem.data(); else return; mem.lock(); *dat = true; mem.unlock(); } ~Sync() { if(dat) mem.detach(); } bool tryLock() { mem.lock(); if(*dat) { /* lock the real resource here */; *dat = false; mem.unlock(); return true; } else { mem.unlock(); return false; } } void lock() { mem.lock(); if(*dat) { /* lock the resource, etc. */ } else { mem.unlock(); // lock on a QSystemSemaphore } } // unlock in a similar fashion private: QSharedMemory mem; bool *dat; }To copy to clipboard, switch view to plain text mode
This is ALMOST foul proof.
The both are not related.I rather write some os dependend code for mutex (CreateMutex() for Win and pthread for POSIX) than write segfaults handlers...
You raised a concern that one application may crash while it holds a resource.
You need to handle that case, no mater what type of locking mechanism you are using, be it deleting a file, or something else.
Google for "inter process communication", and choose the one best fits your needs.
There is nothing in Qt I know of for that, except may DBus but that is not supported on all platforms.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Bookmarks