Go Back   ZeroC Forums > Comments

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 09-15-2006
ymwang 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
Reply With Quote
  #2 (permalink)  
Old 09-15-2006
matthew's Avatar
matthew matthew is offline
ZeroC Staff
 
Name: Matthew Newhook
Organization: ZeroC, Inc.
Project: Internet Communications Engine
 
Join Date: Feb 2003
Location: NL, Canada
Posts: 1,052
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.
Reply With Quote
  #3 (permalink)  
Old 09-18-2006
ymwang 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.
Reply With Quote
  #4 (permalink)  
Old 09-18-2006
benoit's Avatar
benoit benoit is online now
ZeroC Staff
 
Name: Benoit Foucher
Organization: ZeroC, Inc.
Project: Ice
 
Join Date: Feb 2003
Location: Rennes, France
Posts: 1,532
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.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help about one usage of AMD rc_hz Help Center 1 09-30-2006 12:19 AM
slice2cpp - DLL usage timp Help Center 2 01-12-2006 01:03 PM
IceInternal::Handle RyanFogarty Help Center 7 12-18-2004 01:27 AM
How to handle distributed transactions robert Help Center 3 10-31-2004 10:35 PM
Exception handle of ice php fengxb Help Center 2 04-08-2004 10:38 PM


All times are GMT -4. The time now is 04:48 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
(c) 2008 ZeroC, Inc.