Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

thread synchronization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cmrchs@yahoo.com - 01 Jul 2005 19:12 GMT
Hi,

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

thanks
Chris
Carl Daniel [VC++ MVP] - 01 Jul 2005 19:23 GMT
Chris C wrote:
> 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/default.asp?url=/library/en-us/dllproc/base/sy
nchronization_objects.asp


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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/sy
nchronization_objects.asp


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

-cd
Jochen Kalmbach [MVP] - 01 Jul 2005 20:19 GMT
Hi Carl!

>>how can I synchronize 2 threads using the 'lock'-equivalent that is
>>available in C# ?
>
> 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);
}

Signature

Greetings
  Jochen

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

Ivan Mejia - 01 Jul 2005 22:55 GMT
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.

> Hi Carl!
>
[quoted text clipped - 24 lines]
>    System::Threading::Monitor::Exit(obj);
> }
Jochen Kalmbach [MVP] - 02 Jul 2005 09:06 GMT
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...

Signature

Greetings
  Jochen

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

Ivan Mejia - 05 Jul 2005 04:49 GMT
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!

> Hi Ivan!
> > The technique of try finally is grate but the following code is a more C++
[quoted text clipped - 3 lines]
> the missing "using" keyword it is not possible to implement this in an
> __gc class...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.