问题描述
- pthread_create后子线程输出了两次
-
#include
#include
#include
#include
#includevoid *start_handler(void *);
int main(void)
{
pthread_t tid;
int err;if(err = pthread_create(&tid, NULL, start_handler, NULL)) { fprintf(stderr, "Create thread: %sn", strerror(err)); return -1; } printf("OKn"); return 0;
}
void *start_handler(void *arg)
{
int err;if(err = pthread_detach(pthread_self())) { fprintf(stderr, "Detach thread: %sn", strerror(err)); pthread_exit((void *)-1); } printf("Start!n"); pthread_exit((void *)1);
}
解决方案
输出了两次是什么?线程函数执行了,然后输出?
时间: 2024-12-28 12:32:14