在下面的程序中,我創建了一個pthread_t
thread1,它在函數func()
中崩潰。我感興趣的是pthread_join
命令在main()
中發生了什麼。當子線程崩潰並且主要等待連接時會發生什麼?
我在程序下運行,並在正常退出時打印「完成」。我不知道爲什麼?
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstring>
#include <climits>
#include <cstdio>
#include<pthread.h>
#include <stdlib.h>
using namespace std;
void* func(void *data)
{
cout<<"Calling func"<<(long)(data)<<endl;
int *a;
cout<<a[2]<<endl;
pthread_exit(0);
}
int main()
{
pthread_t thread1;
pthread_create(&thread1, 0 , &func, (void*)2);
pthread_join(thread1, NULL);
cout<<"complete"<<endl;
}
您調用了未定義的行爲。你是(非)幸運的程序繼續,似乎在宇宙中的一切都是正確的。因此UB的性質。 – WhozCraig
@WhozCraig哪裏有「未定義」? – GhostCat
@GhostCat取消引用一個不確定的指針。 – WhozCraig