CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    Join Date
    Apr 2002
    Location
    Ibague, Colombia S.A.
    Posts
    54

    [RESOLVED] variable shared between threads

    I need to set a flag in a secondary thread started in a form and read it into a method of another form.

    How do i could make it?

  2. #2
    Join Date
    Nov 2003
    Posts
    2,185

    Re: variable shared between threads

    Use events and a custom delegate with a special parameter (your value).

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: variable shared between threads

    You can use common property of a class. I suggest you to create your own class for that purpose. If you want to be sure that it is thread safe, use the lock keyword.

    For example:
    Code:
    public class Model
    {
      private object obj;
      
      public object Value
      {
         get { lock (this) return this.obj; }
         set { lock (this) this.obj = value; }
       }
    }
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: variable shared between threads

    Quote Originally Posted by boudino
    For example:
    Code:
    public class Model
    {
      private object obj;
      
      public object Value
      {
         get { lock (this) return this.obj; }
         set { lock (this) this.obj = value; }
       }
    }
    In general, locking on 'this' is *not* recommended simply because it's too easy to create deadlocks. Also, that's not really threadsafe. It's what you *do* with the 'Value' that matters.

    It should be
    Code:
    Model m = new Model();    // All threads can access this model
    object locker = new object();  // all threads can access this lock object
    public void ThreadedMethod()
    {
        DoStuff();
        lock(locker)
        {
            DoOperation(m.Value);
            DoOtherOperation(m.Value);
        }
        DoMoreStuff();
    }
    If the above example, you can have 1,000 threads running the 'ThreadedMethod' but access to the 'Model' object declared at the top is completely threadsafe. The previous example provides no threadsafety in the use of the object.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: variable shared between threads

    Maybe you are true, but can you provide a more verbose explanation, please? I don't fully understood you. If I read your sample well, you are locking outside the model, so the m.Value can be still accessed thread unsafe from other code than ThreadedMethod().
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: variable shared between threads

    In your example, if i call:

    object o = m.Value

    and then i start using 'o' to do work, every thread can be using 'o' to do work simultaneously. That's the definition of non-threadsafe access If it's ok to be using o in a non-threadsafe way, then that's fine. However since the point of the example was to show how to have threadsafe access, it's not a good example.

    If I read your sample well, you are locking outside the model, so the m.Value can be still accessed thread unsafe from other code than ThreadedMethod().
    Welcome to the world of threaded programming. Every time you want to safely acces m.Value you need to make sure you take out the lock on the same object that other threads will be using.

    Lets assume m.Value returns a database connection and it can only support 1 simultaenous query at a time. In that case, your code (which just protects the getter and setter) would break horribly as every thread would just use the connection immediately. In my code, every time any thread wants to use the database connection, it must take out the lock on the object, and then it uses the connection. No breakage
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Apr 2002
    Location
    Ibague, Colombia S.A.
    Posts
    54

    Re: variable shared between threads

    If I make this:

    Code:
        class MyThreadLink
        {
            
            private static Boolean bNewState= false;
            object locker = new object();
    
            public void SetNewState(Boolean State)
            {
                lock (locker)
                {
                    bNewSate= State;
                }
             }
            public Boolean ReadState()
            {
                lock (locker)
                {
                    return bNewSate;
                }
            }            
        }
    I Could to instance this class into differents threads and It will work safe?


    I have not understood this instruction:

    Code:
    Model m = new Model();    // All threads can access this model
    What is the namespace for "Model" ?

    Thanks,

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: variable shared between threads

    That's not threadsafe.

    Go read up on threading and thread safety and read a few tutorials (and do them too). It'll help.

    I was demonstrating an example of how you control access to an object, not writing 100% functioning code. In 90%+ of cases you need to control access to the physical object, not controlling access to the getters and setters. Read my previous posts....
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: variable shared between threads

    Yes, I fell I understand you now. You have solved slightly different task than me, maybe more complex. For clarification: if m.Value would be immutable, than my solution is completely thread-safe, is not it?
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  10. #10
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: variable shared between threads

    or you can simply declare the flag variable as volatile...

    Code:
    public partial class Form1 : Form
    {
        privat volatile bool _flag = false;
    
        private void ThreadProc() {
           _flag = true; // set flag
        }
    
        private void SomeMethod() {
           while (!_flag) {
               // blah blah blah
            }
        }
    }

    by the way, what are you trying to accomplish here, if you don't mind? if you're just going to wait for a thread to finish you may use Join().
    Busy

  11. #11
    Join Date
    Nov 2004
    Posts
    105

    Re: variable shared between threads

    I used this concept to solve my purpose, Here I handled more than 100 threads

    Code:
    ///
     private object locker = new object();
     public delegate void EventTable(object sender, clsEventTableArgs e);
     public event EventTable evtEventLog;
    ///
    
    for(int l=0;l<100;l++)
    {
     Thread tt = new Thread(ThreadedMethod);
                    tt.Name = l.ToString();
                    tt.IsBackground = true;
                    tt.Start(l);
    }
    
    public void ThreadedMethod(object lTemp)
    {
         lock(locker)
        {
            DoStuff();
            DoOperation(m.Value);
            DoOtherOperation(m.Value);
            DoMoreStuff();
    
            Monitor.PulseAll(locker);
    
    //Here I called raised the custom event to send this thred data to Form,I caught this data in form
           clsEventTableArgs args = new clsEventTableArgs(m.Value, (int)lTemp);
           evtEventLog(this, args);
        }
    }
    In Form side using InvokeRequired and Invoke I handled It is working fine. May be it will solve the multiple thread problem
    Ravi.Battula

  12. #12
    Join Date
    May 2007
    Posts
    1,546

    Re: variable shared between threads

    Quote Originally Posted by boudino
    Yes, I fell I understand you now. You have solved slightly different task than me, maybe more complex. For clarification: if m.Value would be immutable, than my solution is completely thread-safe, is not it?
    If m.Value is completely immutable, then you don't need 'thead safety' as your object *cannot* change so it is by definition threadsafe.

    As for using the volatile keyword, it doesn't make things thread-safe. Thead safety is a hell of a lot more than just protecting access to variables and marking them as volatile.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  13. #13
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: variable shared between threads

    Quote Originally Posted by Mutant_Fruit
    If m.Value is completely immutable, then you don't need 'thead safety' as your object *cannot* change so it is by definition threadsafe.
    I didn't think m.Value referene, but the object referenced/returned by m.Value. E.g. if m.Value would be value type rather than reference type. I'll try it explain by example:

    Code:
    Model<int> m = new Model<int>(); // model is implemented the way I've used before
    ...
    m.Value = 20;
    // start other thread which is using m and can modify it
    // do other job in this thread
    int i = m.Value;
    In my code, i is local and everything I want is to be sure that it has the value which the another thread could set.

    Am I still wrong?

    It could seem I'm obsedant with this, but I only want to fully understand I see my wrong to learn something new.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: variable shared between threads

    Quote Originally Posted by boudino
    Code:
    Model<int> m = new Model<int>(); // model is implemented the way I've used before
    ...
    m.Value = 20;
    // start other thread which is using m and can modify it
    // do other job in this thread
    int i = m.Value;
    Am I still wrong?
    I wouldn't do this with an object or by locking on this as Mutant mentioned already.

    Here's one thread-safe way to do it.

    Code:
    public class Model
    {
      private string _nameFirst = String.Empty;
      private object _nameFirstLock = new object( );
      
      public string NameFirst
      {
         get { lock (_nameFirstLock) return _nameFirst; }
         set { lock (_nameFirstLock) _nameFirst = value; }
       }
    }
    The bottom line is providing thread safety to a shared variable is simply preventing more than one thread from accessing the variable while a write is occuring.

    So when m.NameFirst is written to by one thread, and m.NameFirst is read from another thread, you need to only allow one thread access to the variable. This is what the lock statement does (internally it creates a critical section and enters and leaves during the scope of the lock statement).
    Last edited by Arjay; October 3rd, 2007 at 02:49 PM.

  15. #15
    Join Date
    Apr 2002
    Location
    Ibague, Colombia S.A.
    Posts
    54

    Re: variable shared between threads

    I have been reviewing some tutorials, I am really a little confused (thanks for the links).

    My program has a secondary thread consulting some peripheral (hardware) via comm1, when some event happens, it is created a new record in a SQL table. This thread must set a flag to indicate to a form that there are a new event in order to refresh a datagrid in such a way that the new record appears in screen.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured