int main(int argc,char *argv[]) { int err; pthread_t tid1,tid2; struct foo *fp; err=pthread_create(&tid1,NULL,thr_fn1,NULL); if(err!=0) printf("can not create thread 1:%s\n",strerror(err)); err=pthread_join(tid1,(void *)&fp); if(err!=0) printf("can not join with thread 1:%s\n",strerror(err)); printfoo("thread 1 exit:\n",fp); sleep(1); err=pthread_create(&tid2,NULL,thr_fn2,NULL); if(err!=0) printf("can not create thread 2:%s\n",strerror(err)); err=pthread_join(tid2,(void *)&fp); if(err!=0) printf("can not join with thread 2:%s\n",strerror(err)); sleep(1); printfoo("main thread:\n",fp); exit(0); }
int main(void) { int err; pthread_t tid1, tid2; struct foo *fp; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if(err != 0) err_quit("can't create thread 1: %s\n", strerror(err)); /* * fp points to foo struct in thread 1 stack, * the foo is overwrited after thread 1 exits. */ err = pthread_join(tid1, (void *)&fp); if(err != 0) err_quit("can't join with thread 1: %s\n", strerror(err)); sleep(1); printf("parent starting second thread\n"); /* thread 2 overwrite the foo space in thread 1 */ err = pthread_create(&tid2, NULL, thr_fn2, NULL); if(err != 0) err_quit("can't create thread 2: %s\n", strerror(err)); sleep(1); printfoo("parent:\n", fp); /* fp is allocated in thread, remember to free */ free(fp); return 0; }
int main(int argc, char const *argv[]) { pthread_t tid1, tid2; int err; struct foo *fp; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if (err != 0) { fprintf(stderr, "can't create thread 1\n"); exit(1); } err = pthread_join(tid1, (void *)&fp); if (err != 0) { fprintf(stderr, "can't join with thread 1\n"); exit(1); } sleep(1); printf("parent starting second thread\n"); err = pthread_create(&tid2, NULL, thr_fn2, NULL); if (err != 0) { fprintf(stderr, "can't create thread 2\n"); exit(1); } sleep(1); printfoo("parent:\n", fp); return 0; }
int main(int argc, char const *argv[]) { int err; pthread_t tid1, tid2; struct foo* fp; if ((err = pthread_create(&tid1, NULL, thr_fn1, NULL)) != 0) { fprintf(stderr, "pthread_create 1 error: %s\n", strerror(err)); exit(1); } if ((err = pthread_join(tid1, (void*)&fp)) != 0) { fprintf(stderr, "pthread_join 1 error: %s\n", strerror(err)); exit(1); } sleep(1); printf("parent starting second thread\n"); if ((err = pthread_create(&tid2, NULL, thr_fn2, NULL)) != 0) { fprintf(stderr, "pthread_create 2 error: %s\n", strerror(err)); exit(1); } sleep(1); printfoo("parent: \n", fp); return 0; }
/* gcc apue.h apue_err.c figure-11.4.c -lpthread */ int main(void) { int err; pthread_t tid1, tid2; struct foo *fp; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if (err != 0) err_exit(err, "can't create thread 1"); err = pthread_join(tid1, (void*)&fp); if (err != 0) err_exit(err, "can't join with thread 1"); sleep(1); printf("parent starting second thread\n"); err = pthread_create(&tid2, NULL, thr_fn2, NULL); if (err != 0) err_exit(err, "can't create thread 2"); sleep(1); printfoo("parent:\n", fp); exit(0); }
void main() { int err; pthread_t tid1, tid2; struct foo *fp; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if(err != 0) perror("Can't create thread"); err = pthread_join(tid1, (void*)&fp); if(err != 0) perror("Can't join thread"); sleep(1); printf("Parent starting second thread \n"); err = pthread_create(&tid2, NULL, thr_fn2, NULL); if(err != 0) perror("Can't create thread"); sleep(1); printfoo("parent : \n", &fp); exit(0); }
int main(void) { int err; pthread_t tid1, tid2; struct foo *fp; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if (err != 0) err_quit("can not create thread 1: %s\n", strerror(err)); err = pthread_join(tid1, (void *)&fp); if (err != 0) err_quit("can not join with thread 1: %s\n", strerror(err)); sleep(1); printf("parent starting second thread\n"); err = pthread_create(&tid2, NULL, thr_fn2, NULL); printf("tid2 is now %lu\n", tid2); printf("tid2 is now 0X%lx\n", tid2); if (err != 0) err_quit("can not create thread 2: %s\n", strerror(err)); sleep(2); printfoo("parent:\n", fp); exit(0); }
void * thr_fn1(void * arg) { struct foo foo = {1,2,3,4}; printfoo("thread 1:\n",&foo); pthread_exit((void *) &foo); }
int main() { int err; pthread_t tid1, tid2; struct foo *fp; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if (err != 0) { err_quit("can't create therad 1: %s\n", strerror(err)); } err = pthread_join(tid1, (void **)&fp); if (err != 0) { err_quit("can't join with thread 1: %s\n", strerror(err)); } sleep(1); printf("parent starting second thread\n"); err = pthread_create(&tid2, NULL, thr_fn2, NULL); if (err != 0) { err_quit("cannpt create thread 2: %s\n", strerror(err)); } err = pthread_join(tid2, NULL); if (err != 0) { err_quit("can't join with thread 2: %s\n", strerror(err)); } printfoo("parent:\n", fp); return 0; }
int main(void) { int ret; pthread_t tid1,tid2; struct foo *fp; ret = pthread_create(&tid1,NULL,thr_fn1,NULL); if(0 != ret){ printf("thread 1\n"); return -1; } ret = pthread_join(tid1,(void *)&fp); if(0 != ret){ printf("join 1\n"); return -1; } sleep(1); printf("parent starting second thread\n"); ret = pthread_create(&tid2,NULL,thr_fn2,NULL); if(0 != ret){ printf("thread2\n"); return -1; } sleep(1); printfoo("parent:\n",fp); exit(0); }
void * thr_fn1(void *arg) { struct foo foo = {1, 2, 3, 4}; /* 线程栈上数据,线程结束后,栈数据可不用。*/ printfoo("thread 1: \n", &foo); pthread_exit((void*)&foo); }
void * thr_fn1(void *arg) { struct foo foo = {1, 2, 3, 4}; // printf("thread 1 returning\n"); printfoo("thread 1:\n", &foo); return ((void *)&foo); }
void * fn1(void * arg) { // pthread_mutex_lock(&foo.lock); printfoo("thread1:\n",&foo); printf("thread1:%d\n", i); i++; // pthread_mutex_unlock(&foo.lock); pthread_exit((void *)&foo); }
void * fn2(void * arg) { printf("thread2:%lu\n",(unsigned long)pthread_self()); printfoo("thread2:\n",&foo); printf("thread2:%d\n", i); i++; pthread_exit((void *)0); }
void * thr_fn1(void * arg) { struct foo foo1 = {1, 2, 3, 4}; //foo1 = (struct foo *)malloc(sizeof(struct foo)); //struct foo temp = {1, 2, 3, 5}; //memcpy(foo1, &temp, sizeof(temp)); printfoo("thread 1:\n", &foo1); pthread_exit((void *)&foo1); }
void *thr_fn1(void *arg){ //struct foo *foo = (struct foo*)malloc(sizeof(struct foo)); //foo->a = 1; //foo->b = 2; //foo->c = 3; //foo->d = 4; struct foo foo = {1,2,3,4}; printfoo("thread 1:\n", &foo); pthread_exit((void*)&foo); }
void* thr_fn1(void *arg) { struct foo foo = {1, 2, 3, 4}; printfoo("thread1:\n", &foo); /* 出错了,线程退出时,数据在栈上,得到的数据可能是脏数据 解决办法:可用malloc在堆上返回解决。 */ pthread_exit((void *)&foo); }
int main(int argc,char *argv[]) { int err; pthread_t tid1,tid2; struct foo *fp; if((err = pthread_create(&tid1,NULL,func1,NULL)) != 0 ) perror("pthread_exit error"); if(( err = pthread_join(tid1,(void**)&fp)) != 0 ) perror("pthread_join error"); printfoo("parent:",fp); exit(0); }
void * thr_fn1(void *arg) { struct foo *fp; if ((fp = malloc(sizeof(struct foo))) == NULL) err_sys("can't allocate memory"); fp->a = 1; fp->b = 2; fp->c = 3; fp->d = 4; printfoo("thread:\n", fp); return((void *)fp); }
void *thr_fn1(void *arg) { // struct foo foo = {1, 2, 3, 4}; struct foo *fp; if((fp = malloc(sizeof(struct foo))) == NULL) err_sys("malloc error"); fp->a = 1; fp->b = 2; fp->c = 3; fp->d = 4; printfoo("thread 1: \n", fp); pthread_exit((void *)fp); }
int main(void) { int err; pthread_t tid1; struct foo *fp; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if (err != 0) err_exit(err, "can't create thread 1"); err = pthread_join(tid1, (void *)&fp); if (err != 0) err_exit(err, "can't join with thread 1"); printfoo("parent:\n", fp); exit(0); }
int main(void){ int err; pthread_t tid1,tid2; struct foo *fp; err = pthread_create(&tid1,NULL,thr_fn1,NULL); if ( err != 0 ) printf("can't create thread 1:%d\n",err); err = pthread_join(tid1,(void *)&fp); if ( err != 0 ) printf("can't join with thread 1\n"); sleep(1); err = pthread_create(&tid2,NULL,thr_fn2,NULL); if ( err != 0 ) printf("can't create thread 1:%d\n",err); pthread_join(tid2,(void *)&fp); sleep(1); printfoo("parent:\n",fp); exit(0); }
int main(void){ int err; pthread_t tid1,tid2; struct foo *p; err = pthread_create(&tid1,NULL,thr_fn1,NULL); if(err!=0){ err_quit("cant't create thread 1:%s\n",strerror(err)); } err = pthread_join(tid1,(void *)&p); if(err!=0) err_quit("can't join with thraed1:%s\n",strerror(err)); sleep(1); printf("parent starting second thread"); err = pthread_create(&tid2,NULL,thr_fn2,NULL); if(err!=0) err_quit("can't create thread 2:%s\n",strerror(err)); sleep(1); printfoo("parent : \n",p); exit(0); }
int main() { int err; pthread_t tid1,tid2; struct foo *fp; err=pthread_create(&tid1,NULL,thr_fn1,NULL); if(err != 0) err_exit(err,"can't create thread 1\n"); err = pthread_join(tid1,(void *)&fp); sleep(1); err=pthread_create(&tid2,NULL,thr_fn2,NULL); if(err != 0) err_exit(err,"can't create thread 2\n"); err = pthread_join(tid2,(void *)&fp); sleep(1); printfoo("parent:\n",fp); return 0; }
int main( int argc, char *argv[] ) { int err; pthread_t tid1, tid2; struct foo *fp; err = pthread_create(&tid1, NULL, thr_fn1, NULL); if ( err != 0 ) err_quit("can't create thread 1: %s\n", strerror(err)); err = pthread_join(tid1, (void *)&fp); if ( err != 0 ) err_quit("can't join with thread 1: %s\n", strerror(err)); sleep(1); printf("parent starting second thread\n"); err = pthread_create(&tid2, NULL, thr_fn2, NULL); if ( err != 0 ) err_quit("can't create thread 2: %s\n", strerror(err)); sleep(1); printfoo("parent:\n", fp); exit(0); }
void * thr_fn1(void *arg){ printfoo("thread 1 \n",&foo); pthread_exit((void *)&foo); }
void *func1() { struct foo foo = {1,2,3,4}; printfoo("thread 1:",&foo); pthread_exit((void*)&foo); }
int main(int argc, char *argv[]) { int err; pthread_t tid1, tid2; // void *tret; struct foo *fp; /* err = pthread_create(&tid1, NULL, thr_fn1, NULL); if (err != 0) { printf("can't create thread1\n"); exit(1); } err = pthread_create(&tid2, NULL, thr_fn2, NULL); if (err != 0) { printf("can't create thread2\n"); exit(1); } printf("thread 1 exit code %d\n", (int )tret); err = pthread_join(tid1, &tret); if (err != 0) { printf("can't join with thread 1: %s\n", strerror(err)); exit(1); } err = pthread_join(tid2, &tret); if (err != 0) { printf("can't join with thread 2: %s\n", strerror(err)); exit(1); } printf("thread 2 exit code %d\n", (int )tret); */ err = pthread_create(&tid1, NULL, thr_fn1, NULL); if (err) { printf("can't create thread 1: %s\n", strerror(err)); exit(1); } err = pthread_join(tid1, (void *)&fp); if (err) { printf("can't join with thread 1: %s\n", strerror(err)); exit(1); } sleep(1); printf("parent starting second thread\n"); err = pthread_create(&tid2, NULL, thr_fn2, NULL); if (err) { printf("can't create thread 2: %s\n", strerror(err)); exit(1); } printfoo("parent:\n", fp); exit(0); }