Multy Thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • [Yosi]

    Multy Thread

    I have an application which run more than one thread at the sametime.
    All those thread call same function (output function). How I lock this
    function for other thread while one thread already use it.
    in C# I done it by :
    void output (String ff)
    {
    lock(this)
    {
    ........
    }
    }

    How to do that in VC++.Net ?
  • William DePalo [MVP VC++]

    #2
    Re: Multy Thread

    "[Yosi]" <Yosi@discussio ns.microsoft.co m> wrote in message
    news:0BA2D4F9-AEE7-4BAC-AB52-4EAF74EF7DB1@mi crosoft.com...[color=blue]
    >I have an application which run more than one thread at the sametime.
    > All those thread call same function (output function). How I lock this
    > function for other thread while one thread already use it.
    > in C# I done it by :
    > void output (String ff)
    > {
    > lock(this)
    > {
    > ........
    > }
    > }
    >
    > How to do that in VC++.Net ?[/color]

    You need a critical section which is created with InitializeCritc alSection()
    and destroyed with DeleteCritcalSe ction(). On entry to your "locked" block
    you call EnterCriticalSe ction() and on exit LeaveCriticalSe ction().

    Regards,
    Will


    Comment

    • Marcus Heege

      #3
      Re: Multy Thread

      The answer to your question heavily depends on what use and you want to do.

      If you are using C++/CLI that comes with VS.NET 2005, and implement managed
      code, you can include <msclr\lock.h > and use the class msclr::lock. If you
      are using Managed C++, you have to call the static methode Enter/TryEnter
      and Exit of System::Threadi ng::Monitor. If you are writing native code, you
      have to use a Win32 API like the critical section api.

      Marcus Heege



      "[Yosi]" <Yosi@discussio ns.microsoft.co m> wrote in message
      news:0BA2D4F9-AEE7-4BAC-AB52-4EAF74EF7DB1@mi crosoft.com...[color=blue]
      >I have an application which run more than one thread at the sametime.
      > All those thread call same function (output function). How I lock this
      > function for other thread while one thread already use it.
      > in C# I done it by :
      > void output (String ff)
      > {
      > lock(this)
      > {
      > ........
      > }
      > }
      >
      > How to do that in VC++.Net ?[/color]


      Comment

      Working...