i'm having hard time figuring out how manage deallocation of memory in multithreaded environments. i'm having hard time using lock protect structure, when it's time free structure, have unlock lock destroy lock itself. cause problems if separate thread waiting on same lock need destroy.
i'm trying come mechanism has retain counts, , when object's retain count 0, it's freed. i've been trying number of different things can't right. i've been doing seems can't put locking mechanism inside of structure need able free , destroy, because requires unlock the lock inside of it, allow thread proceed if blocked in lock request same structure. mean undefined guaranteed happen - lock destroyed, , deallocated either memory access errors, or lock on undefined behavior..
would mind looking @ code? able put sandboxed example demonstrates i'm trying without bunch of files.
#include <pthread.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> struct xatom { short rc; pthread_rwlock_t * rwlck; }; typedef struct xatom xatom; struct container { xatom * atom; }; typedef struct container container; #define nr 1 #define nw 2 pthread_t readers[nr]; pthread_t writers[nw]; container * c; void retain(container * cont); void release(container ** cont); short retain_count(container * cont); void * rth(void * arg) { short rc; while(1) { if(c == null) break; rc = retain_count(c); } printf("rth exit!\n"); return null; } void * wth(void * arg) { while(1) { if(c == null) break; release((container **)&c); } printf("wth exit!\n"); return null; } short retain_count(container * cont) { short rc = 1; pthread_rwlock_rdlock(cont->atom->rwlck); printf("got rdlock in retain_count\n"); rc = cont->atom->rc; pthread_rwlock_unlock(cont->atom->rwlck); return rc; } void retain(container * cont) { pthread_rwlock_wrlock(cont->atom->rwlck); printf("got retain write lock\n"); cont->atom->rc++; pthread_rwlock_unlock(cont->atom->rwlck); } void release(container ** cont) { if(!cont || !(*cont)) return; container * tmp = *cont; pthread_rwlock_t ** lock = (pthread_rwlock_t **)&(*cont)->atom->rwlck; pthread_rwlock_wrlock(*lock); printf("got release write lock\n"); if(!tmp) { printf("return 2\n"); pthread_rwlock_unlock(*lock); if(*lock) { printf("destroying lock 1\n"); pthread_rwlock_destroy(*lock); *lock = null; } return; } tmp->atom->rc--; if(tmp->atom->rc == 0) { printf("deallocating!\n"); *cont = null; pthread_rwlock_unlock(*lock); if(pthread_rwlock_trywrlock(*lock) == 0) { printf("destroying lock 2\n"); pthread_rwlock_destroy(*lock); *lock = null; } free(tmp->atom->rwlck); free(tmp->atom); free(tmp); } else { pthread_rwlock_unlock(*lock); } } container * new_container() { container * cont = malloc(sizeof(container)); cont->atom = malloc(sizeof(xatom)); cont->atom->rwlck = malloc(sizeof(pthread_rwlock_t)); pthread_rwlock_init(cont->atom->rwlck,null); cont->atom->rc = 1; return cont; } int main(int argc, char ** argv) { c = new_container(); int = 0; int l = 4; for(i=0;i<l;i++) retain(c); for(i=0;i<nr;i++) pthread_create(&readers[i],null,&rth,null); for(i=0;i<nw;i++) pthread_create(&writers[i],null,&wth,null); sleep(2); for(i=0;i<nr;i++) pthread_join(readers[i],null); for(i=0;i<nw;i++) pthread_join(writers[i],null); return 0; }
thanks help!
yes, can't put key inside safe. approach refcount (create object when requested , doesn't exist, delete on last release) correct. lock must exist @ least moment before object created , after destroyed - is, while used. can't delete inside of itself.
otoh, don't need countless locks, 1 each object create. 1 lock excludes obtaining , releasing of objects not create performance loss @ all. create lock on init , destroy on program end. otaining/releasing object should take short enough lock on variable blocking access unrelated variable b should never happen. if happens - can still introduce 1 lock per obtained variables , 1 per each obtained one.
also, there seems no point rwlock, plain mutex suffices, , create/destroy operations must exclude each other, not parallel instances of - use pthread_create_mutex() family instead.
Comments
Post a Comment