Shareable objects...
-
I want to create some 'Global' flag (expalantion...): Iv'e tried the kernel sync objs but I can't query it with 'IsLocked' kind of functions (no such???) They must support naming, have boolean behaviour - means that they can be queried and set with true/false values (or 'valid addrr'/'NULL'), must be accessable from any process/thread, mustn't be physicals (like files). Is there someone with an answer or am I going CRaZzZzY?!?!?! Thanks gals... :laugh:**
--BlackSmith--
**/*The roof is on fire, we don't need no water, let the MF burn*/. BHG.
-
I want to create some 'Global' flag (expalantion...): Iv'e tried the kernel sync objs but I can't query it with 'IsLocked' kind of functions (no such???) They must support naming, have boolean behaviour - means that they can be queried and set with true/false values (or 'valid addrr'/'NULL'), must be accessable from any process/thread, mustn't be physicals (like files). Is there someone with an answer or am I going CRaZzZzY?!?!?! Thanks gals... :laugh:**
--BlackSmith--
**/*The roof is on fire, we don't need no water, let the MF burn*/. BHG.
-
can u specify why/how the mutex does not solve ur problem?
"No matter where you go, there your are..." - Buckaoo Banzi
-pete
I don't need any kind of a waitable lock. On a mutex I can't ask 'IsLocked' and get an ansewer right away without waiting. I only want a 'flag' that I can 'set and get' ( True/False ) from any process/thread ( Naming is required for locating it of course:rolleyes: ). This comes any clearer? damn I hope so...:~**
--BlackSmith--
**/*The roof is on fire, we don't need no water, let the MF burn*/. BHG.
-
I want to create some 'Global' flag (expalantion...): Iv'e tried the kernel sync objs but I can't query it with 'IsLocked' kind of functions (no such???) They must support naming, have boolean behaviour - means that they can be queried and set with true/false values (or 'valid addrr'/'NULL'), must be accessable from any process/thread, mustn't be physicals (like files). Is there someone with an answer or am I going CRaZzZzY?!?!?! Thanks gals... :laugh:**
--BlackSmith--
**/*The roof is on fire, we don't need no water, let the MF burn*/. BHG.
Indeed, some kernel objects can be queried using WaitForSingleObject (events, semaphores, mutexes ...). Here's a sample that I didn't bother to build or test, but it should be pretty close to what you need:
class CSharableObject
{
public:
CSharableObject( const TCHAR * p_pszName ) :
c_hMutex( NULL )
{
c_hMutex = OpenMutex( SYNCHRONIZE, FALSE, p_pszName );
if( c_hMutex == NULL )
{
c_hMutex = CreateMutex( NULL, FALSE, p_pszName );
if( c_hMutex == NULL )
{
// Handle the error somehow...
}
}
}~CSharableObject()
{
if( c_hMutex != NULL )
{
CloseHandle( c_hMutex );
c_hMutex = NULL;
}
}bool IsLocked()
{
// Test if some other thread has the mutex already...
DWORD a_dwReason = WaitForSingleObject( c_hMutex, 0 );
if( a_dwReason == WAIT_TIMEOUT )
return true;
return false;
}unsigned long Lock( unsigned long p_ulTimeout = INFINITE )
{
// Wait for another thread to give up the mutex.
return WaitForSingleObject( c_hMutex, p_ulTimeout );
}bool Unlock()
{
// Give up the mutex.
return ReleaseMutex( c_hMutex ) != 0;
}protected:
HANDLE c_hMutex;
};Now, whereever you need to use the shared resource:
CSharableObject a_oLocker( _T("SomeUniqueName") );
// Lock the object.
a_oLocker.Lock();// ... use the precious resource here...
// And when we are done we unlock it.
a_oLocker.Unlock();// OR
if( !a_oLocker.IsLocked() )
{
// Take one course of action.
}
else
{
// Take another course of action.
}It would be a good idea to make this more robust etc, but it's a start. Chris Richardson C/C++ Include Finder[^]
-
I don't need any kind of a waitable lock. On a mutex I can't ask 'IsLocked' and get an ansewer right away without waiting. I only want a 'flag' that I can 'set and get' ( True/False ) from any process/thread ( Naming is required for locating it of course:rolleyes: ). This comes any clearer? damn I hope so...:~**
--BlackSmith--
**/*The roof is on fire, we don't need no water, let the MF burn*/. BHG.
>> This comes any clearer? no i'm afraid not. >> I don't need any kind of a waitable lock it sounds like u do from ur description: >> I only want a 'flag' that I can 'set and get' ( True/False ) also >> On a mutex I can't ask 'IsLocked' and get an ansewer right away without waiting. not true if( WAIT_TIMEOUT == WaitForSingleObject( mymutex, 0)) // it's not signaled else // it's signaled -pete
"No matter where you go, there your are..." - Buckaoo Banzi
-pete