How to make a function thread safe in mc++?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BartMan

    #1

    How to make a function thread safe in mc++?

    Greetings,

    I was looking at the C#'s "lock()" statement, and I was wondering if there
    was an equivalent or recommended way of doing this in MC++.

    I have a thread which I am calling a function from mc++, and I was wondering
    what
    the recommended ways of making it thread safe within the mc++ world.

    Thanks in advance for any suggestions.
  • BartMan

    #2
    RE: How to make a function thread safe in mc++?

    Right after I made the post I stumbled accross something that I could use for
    thread safety, so I thought I would post in case someone else needs it for a
    solution.

    Solution:
    void Foo()
    {
    // At the beginning of your function.
    System::Threadi ng::Monitor::En ter(this);

    .... Do some code work

    // Add the end of your function (this has to be called)
    System::Threadi ng::Monitor::Ex it(this);
    }

    "BartMan" wrote:
    [color=blue]
    > Greetings,
    >
    > I was looking at the C#'s "lock()" statement, and I was wondering if there
    > was an equivalent or recommended way of doing this in MC++.
    >
    > I have a thread which I am calling a function from mc++, and I was wondering
    > what
    > the recommended ways of making it thread safe within the mc++ world.
    >
    > Thanks in advance for any suggestions.[/color]

    Comment

    • Carl Daniel [VC++ MVP]

      #3
      Re: How to make a function thread safe in mc++?

      BartMan wrote:[color=blue]
      > Right after I made the post I stumbled accross something that I could
      > use for thread safety, so I thought I would post in case someone else
      > needs it for a solution.
      >
      > Solution:
      > void Foo()
      > {
      > // At the beginning of your function.
      > System::Threadi ng::Monitor::En ter(this);
      >
      > ... Do some code work
      >
      > // Add the end of your function (this has to be called)
      > System::Threadi ng::Monitor::Ex it(this);
      > }[/color]

      That's precisely what the C# 'lock' construct does.

      -cd


      Comment

      • Nemanja Trifunovic

        #4
        Re: How to make a function thread safe in mc++?

        I think it would be pretty easy to wrap this into a RAII __nogc class
        with a little help from gcroot.

        Comment

        • BartMan

          #5
          Re: How to make a function thread safe in mc++?

          Hello Nemanja,

          Thanks for the great idea, I didn't know about gcroot which is really
          usefull. :)
          Here is my attempt.

          public __gc class lock
          {
          public:
          lock(System::Ob ject* obj)
          {
          threadObject = obj;
          System::Threadi ng::Monitor::En ter(threadObjec t);
          }
          ~lock()
          {
          System::Threadi ng::Monitor::Ex it(threadObject );
          }
          protected:
          gcroot<System:: Object*> threadObject;
          };


          "Nemanja Trifunovic" wrote:
          [color=blue]
          > I think it would be pretty easy to wrap this into a RAII __nogc class
          > with a little help from gcroot.
          >
          >[/color]

          Comment

          • Nemanja Trifunovic

            #6
            Re: How to make a function thread safe in mc++?

            I hope you mean __nogc lock.

            __gc classes don't have deterministic destructors. Take a look at an
            article I wrote a while ago on the topic:



            Comment

            • BartMan

              #7
              Re: How to make a function thread safe in mc++?

              Oops, you are right.
              Very good article, thanks for the post!



              "Nemanja Trifunovic" wrote:
              [color=blue]
              > I hope you mean __nogc lock.
              >
              > __gc classes don't have deterministic destructors. Take a look at an
              > article I wrote a while ago on the topic:
              >
              > http://www.codeproject.com/managedcpp/managedraii.asp
              >
              >[/color]

              Comment

              • Jochen Kalmbach [MVP]

                #8
                Re: How to make a function thread safe in mc++?

                Hi Carl!
                [color=blue][color=green]
                >>Right after I made the post I stumbled accross something that I could
                >>use for thread safety, so I thought I would post in case someone else
                >>needs it for a solution.
                >>
                >>Solution:
                >>void Foo()
                >>{
                >>// At the beginning of your function.
                >>System::Threa ding::Monitor:: Enter(this);
                >>
                >>... Do some code work
                >>
                >>// Add the end of your function (this has to be called)
                >>System::Threa ding::Monitor:: Exit(this);
                >>}[/color]
                >
                > That's precisely what the C# 'lock' construct does.[/color]

                Almost... the C# also does an try-finally construct..

                System::Threadi ng::Monitor::En ter(this);
                __try
                {
                // Do some code work
                }
                __finally
                {
                System::Threadi ng::Monitor::Ex it(this);
                }

                --
                Greetings
                Jochen

                My blog about Win32 and .NET

                Comment

                Working...