Re: Locking Resources



Coder wrote:
hi, may be this is not the right place to ask this question.
In my MFC application i need to lock a resource, i dont now what kind of mechanism to use exactly in microsoft environment .


Let the resource be a STL vector. this is a shared vector in a multithreaded application.

i need somehting like;
<code>

mutex.lock()
  // vector operations ..
mutex.unlock()

</code>

each thread thats wants to access this vector must sleep if resource is locked.

Best Regards...



MFC provides CCriticalSection for this, but its design and documentation are obscure. I suggest the simpler Win32 CRITICAL_SECTION object. Initialize the object with InitializeCriticalSection, then do this in each thread that accesses the shared data...


EnterCriticalSection(&cs);
   // vector operations ..
LeaveCriticalSection(&cs);

--
Scott McPhillips [VC++ MVP]

.



Relevant Pages

  • Re: 2.6.28-rc2 hates my e1000e
    ... Things change a bit with this patch, but the e1000e still fails to ... The problem is that the e820 resource handling doesn't insert ... Which means that it all ends up being very busy, ...
    (Linux-Kernel)
  • Re: should RTS init in serial core be tied to CRTSCTS
    ... thinking in the resource might be a good idea, ... A default setting needs to be set, so when things initialize, and proc ... More majordomo info at http://vger.kernel.org/majordomo-info.html ...
    (Linux-Kernel)
  • Re: HP Pavilion ze4420 - no resources available for wifi card
    ... If you have disabled PnP in the BIOS, ... computer is telling you that it cannot find an available resource ... in order to initialize the card. ... can be remunerated and then your card can be initialized. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Again. Resource deallocation in Common Lisp.
    ... >> you deallocate them, regardless of how the containing object is ... the resource management protocol for the objects that it received. ... But it's generally probably better to initialize the resources as ...
    (comp.lang.lisp)