thread synchronization

C

cmrchs

Hi,

how can I synchronize 2 threads using the 'lock'-equivalent that is available in C# ?

thanks
Chris

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
C

Carl Daniel [VC++ MVP]

Chris said:
Hi,

how can I synchronize 2 threads using the 'lock'-equivalent that is
available in C# ?

Managed code or native?

If managed, you can use the same mechanism that C# uses: the
System.Threading.Monitor class - you just have to call the member functions
explicitly while the C# lock construct calls them for you.

If native, depending on what you need, you might use a CriticalSection,
Mutex, Semphore or Event.

http://msdn.microsoft.com/library/d...n-us/dllproc/base/synchronization_objects.asp

is a good jumping-off point for information about the kernel synchronization
objects (Mutex, Semaphore, Event), while

http://msdn.microsoft.com/library/d...n-us/dllproc/base/synchronization_objects.asp

covers CriticalSections (which are not kernel objects - they're implemented
by the win32 subsystem).

-cd
 
J

Jochen Kalmbach [MVP]

Hi Carl!
If managed, you can use the same mechanism that C# uses: the
System.Threading.Monitor class - you just have to call the member functions
explicitly while the C# lock construct calls them for you.

Just as an addition, here is a small code-sample (because this will be
mostly implemented without the __try-__finally):

C#:
lock(obj)
{
// Some code
}

managed C++:
System::Threading::Monitor::Enter(obj);
__try
{
// Some code
}
__finally
{
System::Threading::Monitor::Exit(obj);
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
G

Guest

The technique of try finally is grate but the following code is a more C++
style aproach:

#include <windows.h>

template<typename LOCK_TYPE>
class Guard
{
protected:
LOCK_TYPE * lock_;
public:
Guard(LOCK_TYPE & lock) : lock_(&lock)
{
this->lock_->aquire();
}

~Guard()
{
this->lock_->release();
}
};

class CriticalSection
{
private:
CRITICAL_SECTION cs_;

public:
CriticalSection()
{
::InitializeCriticalSection(&this->cs_);
}

~CriticalSection()
{
::DeleteCriticalSection(&this->cs_);
}

void aquire(void)
{
::EnterCriticalSection(&this->cs_);
}

void release(void)
{
::LeaveCriticalSection(&this->cs_);
}
};

class Mutex
{
// kind of the same as CritcalSection class but using CreateMutex,
OpenMutex, etc...
};

CriticalSection g_cs;
//Mutex g_mtx;

int do_something(void)
{
Guard<CriticalSection> guard(g_cs);
//Guard<Mutex> guard(g_mtx);

// do something usefull and dont't butter about releasing the critical
// section because the Guard's template destructor will do it for you
// as soon as you leave this function whatever it happens.

// it is pretty generic because you can use not only critical sections
// but mutexes as well.

return 0;
}

int main(int argc, char * argv[])
{
return do_something();
}

Ivan Mejia C++ Dev.
 
J

Jochen Kalmbach [MVP]

Hi Ivan!
The technique of try finally is grate but the following code is a more C++
style aproach:

You are right, but my intention was pure managed C++... and because of
the missing "using" keyword it is not possible to implement this in an
__gc class...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
G

Guest

Hi Jochen!
Ok and sorry but my intesion was the same as yours, to provide a soluction
but in this case using nothing but plane C++.

Take care!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top