作业社区
探索学习新天地,共享知识资源!
学无止境呀呀呀 的学生作业:
#include #include #include struct person { char name[10]; int age; }; void *do_thread(void *arg) { static struct person ps = {"zhangsan",18}; pthread_exit((void *)&ps); } int main (void) { pthread_t pid;//保存线程id void *pret = NULL; struct person *ret = NULL; if(pthread_create(&pid,NULL,do_thread,NULL)!=0) { perror("[ERROR] pthread_create:"); exit(EXIT_FAILURE); } pthread_join(pid,&pret); ret = (struct person *)pret; //强转struct person 指针 printf("p->name:%s,p->age:%d\n",ret->name,ret->age); return 0; }
+92
学无止境呀呀呀 的学生作业:
#include #include #include #include #include void *do_thread(void *arg) { printf("Thread start.\n"); sleep(1); printf("Thread end.\n"); return NULL; } int main(void) { int err,errt; pthread_t tid = 0; pthread_t tidt = 0; err = pthread_create(&tid,NULL,do_thread,NULL); if(err != 0) { perror("[ERROR] pthread_create:"); return -1; } errt = pthread_create(&tidt,NULL,do_thread,NULL); if(errt != 0) { perror("[ERROR] pthread_createt:"); return -1; } pthread_join(tid,NULL); pthread_join(tidt,NULL); printf("main is running\n"); }
+87
学无止境呀呀呀 的学生作业:
#include #include #include #include #include void *do_thread(void *arg) { printf("Thread start.\n"); } int main(void) { int err; pthread_t tid = 0; //系统自动创建 err = pthread_create(&tid,NULL,do_thread,NULL); if(err != 0) { fprintf(stderr,"[ERROR] pthread_create:< %s >\n",strerror(err)); exit(EXIT_FAILURE); } printf("tid = %ld\n",tid); sleep(10); if(pthread_create(&tid,NULL,do_thread,NULL)!=0){ fprintf(stderr,"[ERROR] pthread_create:< %s >\n",strerror(err)); exit(EXIT_FAILURE); } printf("tdt = %ld\n",tid); return 0; }
+90