multithreading - Reference counting of the object in multithreaded application c++ -


i trying implement reference count objects in multithreaded program, can delete object when none of threads wants use or 1 initiate object delete.

for doing following thing.

class refcount{       int ref_count;     pubic:    void incr_ref_count(){         ref_count ++;    }      int decr_ref_count(){        ref_count--;        return ref_count;     }  } 

and using following code whenever copy obj in different threads.

pthread_mutex_lock(&ref_count_lock); if(obj != null){    dup_obj = obj;    obj.incr_ref_count();  } pthread_mutex_unlock(&ref_count_lock);  

and while removing reference

pthread_mutex_lock(&ref_count_lock); if(dup_obj != null){    count = dup_obj.decr_ref_count();     if(count == 0)       delete dup_obj }    pthread_mutex_unlock(&ref_count_lock); 

i hoping work fine, problem think hav using mutexs(lock), need have mutex(lock) each object create seperately can make coping , increasing or decreasing of count can atomic. how implement this.

please note not working code kinda sudo code.

thanks help.

locks kill reference-counting performance. see example atomic reference counting pointers ways reduce or eliminate number of locks needed.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -