Results 1 to 4 of 4

Thread: Handle class usage in multi-thread environment

  1. #1
    ymwang is offline Registered User
    Name: Yongming Wang
    Organization: Zhejiang University
    Project: No Project
    Join Date
    Apr 2004
    Posts
    2

    Handle class usage in multi-thread environment

    Hello,
    After readed class Handle's implemention in Handle.h, I have a question about the usage in multithread environment. It seems the copy constructor is not thread safe.
    e.g.

    template<typename Y>
    Handle(const Handle<Y>& r)
    {
    this->_ptr = r._ptr;

    if(this->_ptr)
    {
    incRef(this->_ptr);
    }
    }

    Thread 1:
    ptr1 = ptr2;
    Thread 2:
    ptr2 = 0; //when ptr2 has only 1 reference count.

    After thread 1 finished executing this->_ptr = r._ptr;
    it happens a task switch to thread 2.
    then back to thread 1, now this->_ptr point to a invalid address.

    I think add a lock will be better:

    private:
    LOCK lock_;
    public:
    T* __lock_addref()
    {
    lock_guard(this->lock_);
    if (this->_ptr)
    this->_ptr->__addref();
    return this->_ptr;
    }

    template<typename Y>
    Handle(const Handle<Y>& r)
    {
    this->_ptr = (const_cast<usys_smartptr<Y>&>(r)).__lock_addref() ;
    }

    Is it neccessary?

    Thanks,
    Yongming

  2. #2
    matthew's Avatar
    matthew is offline ZeroC Staff
    Name: Matthew Newhook
    Organization: ZeroC, Inc.
    Project: Internet Communications Engine
    Join Date
    Feb 2003
    Location
    NL, Canada
    Posts
    1,458
    Welcome to the forums. In order for us to answer your question you must fill out your signature details as described in the link contained in my signature.

  3. #3
    ymwang is offline Registered User
    Name: Yongming Wang
    Organization: Zhejiang University
    Project: No Project
    Join Date
    Apr 2004
    Posts
    2
    Ok, Sorry.
    * Your full name (no nicknames please).
    Yongming Wang

    * The name of your company or organization (such as universities), including a Web address (URL).
    Zhejiang University,China
    http://www.zju.edu.cn/

    * The name(s) of the Ice project(s) you are working on, if possible with URL.
    No project, studying the source.

  4. #4
    benoit's Avatar
    benoit is online now ZeroC Staff
    Name: Benoit Foucher
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Rennes, France
    Posts
    2,196
    Hi,

    Read and write access to the same IceUtil::Handle instance from multiple threads is not safe, it might result in undefined behavior. You need to add some synchronization if you want to read and modify the same handle instance from different threads.

    Adding this synchronization to the IceUtil::Handle implementation would have a performance and memory cost. Since most applications don't need such thread safety it's better to not add it.

    Cheers,
    Benoit.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 05-29-2009, 10:50 AM
  2. How to handle java.lang.Thread.interrupt() in a safe way?
    By Markus Bernhard in forum Help Center
    Replies: 4
    Last Post: 06-28-2008, 11:29 AM
  3. Replies: 1
    Last Post: 11-29-2007, 07:26 AM
  4. Replies: 5
    Last Post: 11-05-2007, 11:35 AM
  5. Logger usage?
    By vincei in forum Comments
    Replies: 9
    Last Post: 07-12-2007, 08:07 AM

Posting Permissions

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