我在我的程序中使用了方法'pthread_create',並在此方法中出現了分段錯誤。
什麼可能導致這種情況?我用正確的參數類型調用這個函數!pthread_create分段錯誤
這是代碼:
pthread_t* _daemon;
void* writer(void* arg){
// stuff that dont involve "arg"...
}
int initdevice(){
if(pthread_create(_daemon, NULL, &writer, NULL) != 0) //seg in this line
{
cerr << "system error\n";
return ERR_CODE;
}
return SUCCESS;
}
int main() {
initdevice();
return 0;
}
注:我試圖調用到作家在pthread_create之前也運行它沒有「&」,也是 - 我們嘗試發送給該方法一些void *參數而不是最後一個NULL參數。
發佈你在這個調用中使用的代碼,你可能做得不對。 – Mat 2011-04-03 16:53:45
我認爲問題出在第17行。 – Nawaz 2011-04-03 16:55:09
不要使用下劃線'_'作爲全局變量的第一個字符(只是不要在變量的開頭使用它們)。 [這些標識符保留給編譯器和操作系統](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier/228797#228797 )。 – 2011-04-03 17:22:47