Subj : Re: How to return a value before leaving critical section? To : comp.programming.threads From : David Schwartz Date : Tue Jul 19 2005 01:06 pm "MC" wrote in message news:aciqd1hcu4m0s7s44k3gakvd46k63udked@4ax.com... > Threading newbie here. This question has surely been answered 1000 > times. > I have a member variable that I want to share between two threads. I > make it private and I write a public accessor function, inside which, > I use a mutex--actually, I use a Windows CRITICAL_SECTION variable and > then call Enter/LeaveCriticalSection() but I'm hoping that's beside > the point. The dilemna, of course, is that both statements need to be > the last statement of the function: > return m_MyMember; > *and* > LeaveCriticalSection(m_pMyCrSection); > I could use an output parameter instead of a return value but then the > calling code gets uggggglyyy! Is this the only solution? In C++, you have a very elegant solution where you create a 'lock holder' class and an instance of that class. You can then have the destructor release the lock. In C, why not just create a copy and return the copy? For most types, 'return' returns a copy anyway. What type is 'm_MyMember'? DS .