1
#include "apue.h"
#include <limits.h>
#include <pthread.h>
void* fun(void *arg)
{
printf("%lu: I'm alive\n", pthread_self());
pthread_exit(0);
}
int main(int argc, char **v)
{
pthread_t tid;
pthread_create(&tid, NULL, fun, NULL);
printf("%lu: I'm dead\n", pthread_self());
return 0;
}
$ ./a.out
3075692224: I'm dead
3075689280: I'm alive
3075689280: I'm alive
$ ./a.out
3076470464: I'm dead
Somtimes,它打印一條線。 我覺得 一行:3076470464: I'm dead
或 兩行:
3075692224: I'm dead
3075689280: I'm alive
應rigjt。 因爲我不使用pthread_join
,但爲什麼它可以打印三行;
正確退出你的線程,你的輸出應該沒問題。目前代碼產生未定義的行爲。 – this
有時候,解決問題比理解它發生的原因要容易得多。 –