Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Shareable objects...

Shareable objects...

Scheduled Pinned Locked Moved C / C++ / MFC
databasequestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BlackSmith
    wrote on last edited by
    #1

    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.

    P C 2 Replies Last reply
    0
    • B BlackSmith

      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.

      P Offline
      P Offline
      palbano
      wrote on last edited by
      #2

      can u specify why/how the mutex does not solve ur problem?

      "No matter where you go, there your are..." - Buckaoo Banzi

      -pete

      B 1 Reply Last reply
      0
      • P palbano

        can u specify why/how the mutex does not solve ur problem?

        "No matter where you go, there your are..." - Buckaoo Banzi

        -pete

        B Offline
        B Offline
        BlackSmith
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • B BlackSmith

          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.

          C Offline
          C Offline
          Chris Richardson
          wrote on last edited by
          #4

          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[^]

          1 Reply Last reply
          0
          • B BlackSmith

            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.

            P Offline
            P Offline
            palbano
            wrote on last edited by
            #5

            >> 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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups