Thread: thread safe in my code to manipulate List?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    thread safe in my code to manipulate List?

    Hello everyone,


    Here is my code for review to see whether it is thread-safety. My question is, after remove the object instance from List buffer, I exit the lock synchronization block in method RemoveAndUseItem, is it thread safe to exit the lock synchronization and call method RemoveAndUseItem (method RemoveAndUseItem will only touch the removed instance)?

    Code:
            List<Foo> Buffer;
            object ListLock = new object();
    
            void AddItem(Foo f)
            {
                lock (ListLock)
                {
                    Buffer.Add(f);
                }
            }
    
            void RemoveAndUseItem()
            {
                Foo LastItem;
                lock (ListLock)
                {
                    LastItem = Buffer[Buffer.Count - 1];
                    Buffer.RemoveAt(Buffer.Count - 1);
                }
    
                // no lock here? safe?
                SafeUseItem(LastItem);
    
                return;
    
            }
    
            void SafeUseItem(Foo f)
            {
                // use f here without synchronization control
            }

    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    IF that is the ONLY place where you directly access the members inside Buffer, then yes it's thread safe.

    If anything else has a copy of the pointer then it's not necessarily thread safe.

    So it really all comes down to whether or not there is other code that's either manipulates Buffer, or other code that retained a pointer to a Foo object. For example, it's NOT thread safe if you did this:

    Code:
            Foo f = new Foo();
            AddItem(f);
            f.Bar();   
           // If f.Bar() happens at the same time that another thread is calling 
           // RemoveAndUseItem() it could be thread unsafe.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Cat,


    If multiple threads will access Buffer container, but only one thread will access SafeUseItem, then my code is thread safe, right?

    Quote Originally Posted by Cat View Post
    IF that is the ONLY place where you directly access the members inside Buffer, then yes it's thread safe.

    If anything else has a copy of the pointer then it's not necessarily thread safe.

    So it really all comes down to whether or not there is other code that's either manipulates Buffer, or other code that retained a pointer to a Foo object. For example, it's NOT thread safe if you did this:

    Code:
            Foo f = new Foo();
            AddItem(f);
            f.Bar();   
           // If f.Bar() happens at the same time that another thread is calling 
           // RemoveAndUseItem() it could be thread unsafe.

    regards,
    George

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    To be threadsafe, ANY method accessing your object needs to use the locking mechanism ( some locking mechanism, not neccessarily "lock()", but all need to use the same obviously). If a single one doesn't, it's not threadsafe.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks nvoigt,


    Any comments to my post #3?

    Quote Originally Posted by nvoigt View Post
    To be threadsafe, ANY method accessing your object needs to use the locking mechanism ( some locking mechanism, not neccessarily "lock()", but all need to use the same obviously). If a single one doesn't, it's not threadsafe.

    regards,
    George

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Your list is guarded by lock-blocks. Manipulating an object no longer in the list is threadsafe concerning the list. So the answer is yes.

    You don't need an extra object. You can just "lock( Buffer )". That way, it's way more readable because it's obvious what you are locking and why.

    Sidenote: Quoting full blocks of text is considered extremly bad style on the internet, because it adds volume without information. Quoting is required when you need to answer a specific point in another post. Quoting a post entirely therefore is contradictory to the intention of quoting something and as a result a waste of space. Maybe you can use "reply" instead of quoting half the thread.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks nvoigt,


    Your below answer is very clear. :-)

    Quote Originally Posted by nvoigt View Post
    Your list is guarded by lock-blocks. Manipulating an object no longer in the list is threadsafe concerning the list. So the answer is yes.

    You don't need an extra object. You can just "lock( Buffer )". That way, it's way more readable because it's obvious what you are locking and why.

    regards,
    George

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by George2 View Post
    If multiple threads will access Buffer container, but only one thread will access SafeUseItem, then my code is thread safe, right?
    The container code itself is thread-safe so long as all inserts, deletes, and iterations on the container are synchronized, e.g. by a lock. So yes, from what you've shown, it seems thread-safe.


    The bigger question is -- do any other threads have a pointer to any of the objects inside your container? I mean, like this:

    Code:
    Foo a;         // Make a pointer to Foo
    a = new Foo(); // Make it point to a Foo object
    AddItem(a);    // It puts the object in the container
    
    
    a.Bar();       // This line is potentially bad.  This object is in the container but you're also
                   // manipulating it here.  Potentially thread unsafe depending on what Bar() does.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for sharing your points, Cat! There is no pointer leak in my code. But you are correct, I need to be careful not to expose pointer outside? :-)

    Quote Originally Posted by Cat View Post
    The container code itself is thread-safe so long as all inserts, deletes, and iterations on the container are synchronized, e.g. by a lock. So yes, from what you've shown, it seems thread-safe.


    The bigger question is -- do any other threads have a pointer to any of the objects inside your container? I mean, like this:

    Code:
    Foo a;         // Make a pointer to Foo
    a = new Foo(); // Make it point to a Foo object
    AddItem(a);    // It puts the object in the container
    
    
    a.Bar();       // This line is potentially bad.  This object is in the container but you're also
                   // manipulating it here.  Potentially thread unsafe depending on what Bar() does.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  4. Simple list code
    By JimpsEd in forum C Programming
    Replies: 1
    Last Post: 05-24-2006, 02:01 PM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM