static void *thread_fun2(void *arg) { struct foo *fp = (struct foo *)arg; printf("thread 2 starting...\n"); foo_hold(fp); foo_hold(fp); printf("thread 2 returning...\n"); pthread_exit((void*)2); }
int main(void){ struct foo *fo; struct foo *fo2; struct foo *fo_find; int a=12; fo=foo_alloc(a); printf("f_count=%d,f_id=%d\n",fo->f_count,fo->f_id); foo_hold(fo); printf("f_count=%d,f_id=%d\n",fo->f_count,fo->f_id); foo_rele(fo); printf("f_count=%d,f_id=%d\n",fo->f_count,fo->f_id); fo_find=foo_find(1); printf("foo_find f_count=%d,f_id=%d\n",fo_find->f_count,fo_find->f_id); printf("----\n"); int b=13; fo2=foo_alloc(b); printf("f_count=%d,f_id=%d\n",fo2->f_count,fo2->f_id); exit(0); }
void *thr_fn(void *arg) { while(m_fp!=NULL){ foo_hold(m_fp); printf("thread %u is using foo.\n",(unsigned int)pthread_self()); sleep(1); } printf("thread %u has no foo to use.\n",(unsigned int)pthread_self()); }
void * thr_fn(void *arg) { struct foo *fp = arg; sleep(1); foo_hold(fp); pthread_exit((void *) 0); }
struct foo * foo_find(int id){ struct foo * fp; pthread_mutex_lock(&haslock); for(fp = fh[HASH(id)];fp != NULL;fp = fp->f_next){ if(fp->f_id == id){ foo_hold(fp); break; } } pthread_mutex_unlock(&haslock); return (fp); }
void *thr_fn(void *arg) { int i; /* do limit job */ for(i = 0; i < 2 && fptr != NULL; ++i) { foo_hold(fptr); sleep(1); foo_rele(fptr); /* thread completes and releases object before exit */ } printf("thread %ld returns\n", pthread_self()); return (void *)NULL; }
struct foo *foo_find(int id) /* find an existing object */ { struct foo *fp; int idx; idx = HASH(fp); pthread_mutex_lock(&hashlock); for (fp = fh[idx]; fp != NULL; fp = fp->f_next) { if (fp->f_id == id) { foo_hold(fp); break; } } pthread_mutex_unlock(&hashlock); return(fp); }
void* thr_fn(void *arg) { pthread_t pt; pid_t pid; pt = pthread_self(); pid = getpid(); if(fptr) { foo_hold(fptr); //fptr->count++; sleep(2); printf("count = %d,thread_id = %u pid = %u\n",fptr->f_count,(unsigned int)pt,pid); foo_rele(fptr); } return (void *)1; }
struct foo *foo_find(int id){ struct foo *fp; int idx; //printf("foo_find\n"); idx=HASH(id); printf("foo_find idx=%d\n",idx); pthread_mutex_lock(&hashlock); for(fp=fh[idx];fp!=NULL;fp=fp->f_next){ printf("foo_find id=%d,fp_count=%d\n",id,fp->f_count); if(fp->f_id==id){ printf("last find id=%d\n",id); foo_hold(fp); break; } } pthread_mutex_unlock(&hashlock); return(fp); }
struct foo * foo_find(int id) { struct foo *fp; int i; pthread_mutex_lock(&hashlock); for (i=0; i<NHASH; i++) { for (fp=fh[i]; fp != NULL; fp = fp->f_next) { if (fp->f_id == id) { foo_hold(fp); pthread_mutex_unlock(&hashlock); return(fp); } } } pthread_mutex_unlock(&hashlock); return(fp); }
void fun(const char *thname,mutexfoo *fp){ foo_hold(fp); printf(" thread:%s, fooid: %d ,foo->count is : %d \n",thname,fp->f_id,fp->f_count); foo_rele(fp); }