thread synchronization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmrchs@yahoo.com

    #1

    thread synchronization

    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...
  • Carl Daniel [VC++ MVP]

    #2
    Re: thread synchronization

    Chris C wrote:[color=blue]
    > Hi,
    >
    > how can I synchronize 2 threads using the 'lock'-equivalent that is
    > available in C# ?[/color]

    Managed code or native?

    If managed, you can use the same mechanism that C# uses: the
    System.Threadin g.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/de...on_objects.asp

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

    http://msdn.microsoft.com/library/de...on_objects.asp

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

    -cd


    Comment

    • Jochen Kalmbach [MVP]

      #3
      Re: thread synchronization

      Hi Carl!
      [color=blue][color=green]
      >>how can I synchronize 2 threads using the 'lock'-equivalent that is
      >>available in C# ?[/color]
      >
      > If managed, you can use the same mechanism that C# uses: the
      > System.Threadin g.Monitor class - you just have to call the member functions
      > explicitly while the C# lock construct calls them for you.[/color]

      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::Threadi ng::Monitor::En ter(obj);
      __try
      {
      // Some code
      }
      __finally
      {
      System::Threadi ng::Monitor::Ex it(obj);
      }

      --
      Greetings
      Jochen

      My blog about Win32 and .NET

      Comment

      • Ivan Mejia

        #4
        Re: thread synchronization

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

        #include <windows.h>

        template<typena me 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_SECTIO N cs_;

        public:
        CriticalSection ()
        {
        ::InitializeCri ticalSection(&t his->cs_);
        }

        ~CriticalSectio n()
        {
        ::DeleteCritica lSection(&this->cs_);
        }

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

        void release(void)
        {
        ::LeaveCritical Section(&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(vo id)
        {
        Guard<CriticalS ection> 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.

        "Jochen Kalmbach [MVP]" wrote:
        [color=blue]
        > Hi Carl!
        >[color=green][color=darkred]
        > >>how can I synchronize 2 threads using the 'lock'-equivalent that is
        > >>available in C# ?[/color]
        > >
        > > If managed, you can use the same mechanism that C# uses: the
        > > System.Threadin g.Monitor class - you just have to call the member functions
        > > explicitly while the C# lock construct calls them for you.[/color]
        >
        > 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::Threadi ng::Monitor::En ter(obj);
        > __try
        > {
        > // Some code
        > }
        > __finally
        > {
        > System::Threadi ng::Monitor::Ex it(obj);
        > }
        >
        > --
        > Greetings
        > Jochen
        >
        > My blog about Win32 and .NET
        > http://blog.kalmbachnet.de/
        >[/color]

        Comment

        • Jochen Kalmbach [MVP]

          #5
          Re: thread synchronization

          Hi Ivan![color=blue]
          > The technique of try finally is grate but the following code is a more C++
          > style aproach:[/color]

          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

          Comment

          • Ivan Mejia

            #6
            Re: thread synchronization

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

            #include <windows.h>

            template<typena me 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_SECTIO N cs_;

            public:
            CriticalSection ()
            {
            ::InitializeCri ticalSection(&t his->cs_);
            }

            ~CriticalSectio n()
            {
            ::DeleteCritica lSection(&this->cs_);
            }

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

            void release(void)
            {
            ::LeaveCritical Section(&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(vo id)
            {
            Guard<CriticalS ection> 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.

            "Jochen Kalmbach [MVP]" wrote:
            [color=blue]
            > Hi Carl!
            >[color=green][color=darkred]
            > >>how can I synchronize 2 threads using the 'lock'-equivalent that is
            > >>available in C# ?[/color]
            > >
            > > If managed, you can use the same mechanism that C# uses: the
            > > System.Threadin g.Monitor class - you just have to call the member functions
            > > explicitly while the C# lock construct calls them for you.[/color]
            >
            > 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::Threadi ng::Monitor::En ter(obj);
            > __try
            > {
            > // Some code
            > }
            > __finally
            > {
            > System::Threadi ng::Monitor::Ex it(obj);
            > }
            >
            > --
            > Greetings
            > Jochen
            >
            > My blog about Win32 and .NET
            > http://blog.kalmbachnet.de/
            >[/color]

            Comment

            • Jochen Kalmbach [MVP]

              #7
              Re: thread synchronization

              Hi Ivan![color=blue]
              > The technique of try finally is grate but the following code is a more C++
              > style aproach:[/color]

              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

              Comment

              • Ivan Mejia

                #8
                Re: thread synchronization

                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!

                "Jochen Kalmbach [MVP]" wrote:
                [color=blue]
                > Hi Ivan![color=green]
                > > The technique of try finally is grate but the following code is a more C++
                > > style aproach:[/color]
                >
                > 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/
                >[/color]

                Comment

                • Ivan Mejia

                  #9
                  Re: thread synchronization

                  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!

                  "Jochen Kalmbach [MVP]" wrote:
                  [color=blue]
                  > Hi Ivan![color=green]
                  > > The technique of try finally is grate but the following code is a more C++
                  > > style aproach:[/color]
                  >
                  > 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/
                  >[/color]

                  Comment

                  Working...