作业社区
探索学习新天地,共享知识资源!
蜡笔小方哎 的学生作业:
#include #include #include #include struct person { char name[20]; int age; }; void* thread_func(void* arg) { struct person* ret = (struct person*)malloc(sizeof(struct person)); strcpy(ret->name, "aaa"); ret->age = 15; pthread_exit((void*)ret); } int main() { int err; pthread_t pid; err = pthread_create(&pid, NULL, thread_func, NULL); if(err != 0) { fprintf(stderr, "pthread_create() error: %s\n", strerror(err)); exit(-1); } struct person* p; pthread_join(pid, (void**)&p); printf("name: %s, age: %d\n", p->name, p->age); free(p); return 0; }
蜡笔小方哎 的学生作业:
fang@fang:~/Projects/test$ cat main.c #include #include #include #include #include void* do_thread(void* arg) { printf("current threadid: %d\n", pthread_self()); } int main() { pthread_t tid_1, tid_2; int err; err = pthread_create(&tid_1, NULL, do_thread, NULL); if(err != 0) { fprintf(stderr, "error: %s\n", strerror(err)); exit(-1); } err = pthread_create(&tid_2, NULL, do_thread, NULL); if(err != 0) { fprintf(stderr, "error: %s\n", strerror(err)); exit(-1); } pthread_detach(tid_1); pthread_detach(tid_2); sleep(1); return 0; }
蜡笔小方哎 的学生作业:
#include #include #include #include #include void* do_thread(void* arg) { printf("current threadid: %d\n", pthread_self()); } int main() { pthread_t tid_1, tid_2; int err; err = pthread_create(&tid_1, NULL, do_thread, NULL); if(err != 0) { fprintf(stderr, "error: %s\n", strerror(err)); exit(-1); } err = pthread_create(&tid_2, NULL, do_thread, NULL); if(err != 0) { fprintf(stderr, "error: %s\n", strerror(err)); exit(-1); } pthread_join(tid_1, NULL); pthread_join(tid_2, NULL); return 0; }