Я написал эту программу, чтобы практиковать системные вызовы pthread, поэтому я использовал несколько строк печати, чтобы проверить результаты, но они экранировались:
Thread 1 created
Thread 2 created
test3
, хотя я думаю, что это должноbe
thread 1 created
test2
thread 2 created
test3
test1
Порядок может измениться, но у меня должны быть эти строки, так почему он избегает этих операторов печати?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *function();
void *function2();
int main(int argc, char *argv[])
{
pthread_t tid;
int rc;
rc = pthread_create(&tid, NULL, function(), NULL);
if(rc > 0){
fprintf(stderr, "Error\n");
exit(1);
}
pthread_join(tid, NULL);
sleep(1);
printf("test1\n");
pthread_exit(NULL);
}
void *function(){
int rc;
pthread_t tid;
printf("Thread 1 created\n");
rc = pthread_create(&tid, NULL, function2(), NULL);
if(rc > 0){
fprintf(stderr, "Error\n");
exit(1);
}
printf("test2\n");
pthread_exit(NULL);
}
void *function2(){
pthread_detach(pthread_self());
printf("Thread 2 created\n");
printf("test3\n");
pthread_exit(NULL);
}