#include <pthread.h>
void *testThread(){
printf("Hello, World!New Thread11\n");
}
void *testThread2(){
printf("Hello, World!New Thread22\n");
}
int main() {
printf("Hello, World!\n");
pthread_t pthread_t,pthread_t2;
pthread_create(&pthread_t, NULL, testThread, NULL);
pthread_create(&pthread_t2, NULL, testThread2, NULL);
pthread_detach(&pthread_t);
pthread_detach(&pthread_t2);
return 0;
}
输出:
Hello, World!
Hello, World!New Thread11
Hello, World!New Thread22
//销毁线程的3种方法
//方法一
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
ret = pthread_create(&pid, &attr, __task, NULL);
//方法二
pthread_detach(pid);
pthread_detach(pthread_self());
//方法三 阻塞等待线程退出(线程内部必须有退出的变量等标识)
pthread_join(pid, NULL);