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 cleanup(void *arg) { if(arg) { foo_rele((struct foo *)arg); } }
int main(void) { int i, err; void *tret; pthread_t tids[LENGTH]; struct foo *fp; fp = foo_alloc(); for (i=0; i<LENGTH; i++) { if ((err = pthread_create(tids+i, NULL, thr_fn, (void *) fp)) != 0) continue; } for (i=0; i<LENGTH; i++) { if ((err = pthread_join(tids[i], &tret)) != 0) continue; } printf("count = %d\n", fp->f_count); foo_rele(fp); exit(0); }
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; }
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; }
void main() { int i,err; unsigned int ntid; //Intial our hash for(i=0;i<NHASH;++i) fh[i]=NULL; m_fp=foo_alloc(); for(i=0;i<5;++i){ err=pthread_create((pthread_t *)(&ntid),NULL,thr_fn,NULL); if(err!=0) err_quit("can't create thread:%s \n",strerror(err)); } sleep(5); while(m_fp!=NULL){ printf("usage count: %d\n",m_fp->f_count); foo_rele(m_fp); } printf("usage count: 0\n"); sleep(10); }
void main() { struct foo *test; test = foo_alloc(); foo_rele(test); }
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); }