[英]Pthreads : Segmentation fault error
我有幾個問題。 這是我第一次嘗試制作多線程程序。
注意 - 完整程序位於頁面底部
(編譯,使用
g++ -pthread -o <executable file name> <sourcefile>.cpp -fpermissive
)
我使用Ubuntu Studio 10.10 64位編譯它。
這個程序最大的問題是它給了我一個分段錯誤。
它似乎是由我在int main()中注釋的行引起的。 如果我評論該行,它不會給我一個分段錯誤錯誤。
為方便起見,這里僅使用int main():
int main()
{
pthread_attr_t attr;
pthread_t threads[30];
/* Initialize mutex and condition variable objects */
pthread_mutex_init(&direction_mutex, NULL);
pthread_mutex_init(&arrive_mutex,NULL);
pthread_cond_init (&count_threshold, NULL);
pthread_cond_init(&arrive_done, NULL);
/*
For portability, explicitly create threads in a joinable state
I'll take your word for it on that one.
*/
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for( int x = 0 ; x < 30 ; x++)
{
long random = rand();
int direction;
if (random < RAND_MAX/2)
{
direction = 0;
}
else
{
direction = 1;
}
directions[x] = direction;
printf("%d",direction);
}
printf("\n");
currdir = directions[0];
for(int j = 0 ; j < 30 ; j++)
{
if(j != 0)
{
pthread_cond_wait(&arrive_done, NULL); // THIS line of code is what is causing the problem
}
pthread_create(&threads[j], &attr, OneCar, (void *)&Thread_IDs[j]);
}
/* Wait for all threads to complete */
for (j = 0; j < 30; j++)
{
pthread_join(threads[j], NULL);
}
printf("test\n");
/* Clean up and exit */
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&direction_mutex);
pthread_cond_destroy(&count_threshold);
pthread_exit (NULL);
}
沒有那一行,程序運行,但問題是,它似乎在線程順序中相當隨機。
我試圖使用該互斥鎖來保持int main()從啟動一個新線程直到最后一個完成,因為該程序應該讓線程以FIFO順序運行。
如果沒有此代碼,則行為會有所不同。
大部分時間它從線程0開始,然后轉到線程3,4,有時甚至是5,然后返回線程1。
有時候,它從線程3開始,然后進入線程4,然后線程0 ......我無法弄清楚它為什么這樣做。
它每次都是一個不同的線程執行序列,但它永遠不會像它需要的那樣是0,1,2,3,4
這是輸出違規行注釋掉的:
*** Output of program with "pthread_cond_wait(&arrive_done, NULL);" commented out:
101110010101011111010001000010
ArriveBridge(): Car 1 goes accross the bridge
ExitBridge(): car 1 has left the bridge
Arrivebridge(): Thead 0 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 0 goes accross the bridge
ExitBridge(): car 0 has left the bridge
Arrivebridge(): Thead 5 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 5 goes accross the bridge
ExitBridge(): car 5 has left the bridge
Arrivebridge(): Thead 3 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 3 goes accross the bridge
ExitBridge(): car 3 has left the bridge
ArriveBridge(): Car 2 goes accross the bridge
ExitBridge(): car 2 has left the bridge
ArriveBridge(): Car 4 goes accross the bridge
ExitBridge(): car 4 has left the bridge
Arrivebridge(): Thead 6 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 6 goes accross the bridge
ExitBridge(): car 6 has left the bridge
Arrivebridge(): Thead 7 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 7 goes accross the bridge
ExitBridge(): car 7 has left the bridge
Arrivebridge(): Thead 8 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 8 goes accross the bridge
ExitBridge(): car 8 has left the bridge
Arrivebridge(): Thead 9 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 9 goes accross the bridge
ExitBridge(): car 9 has left the bridge
Arrivebridge(): Thead 10 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 10 goes accross the bridge
ExitBridge(): car 10 has left the bridge
Arrivebridge(): Thead 11 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 11 goes accross the bridge
ExitBridge(): car 11 has left the bridge
ArriveBridge(): Car 13 goes accross the bridge
ExitBridge(): car 13 has left the bridge
Arrivebridge(): Thead 12 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 12 goes accross the bridge
ExitBridge(): car 12 has left the bridge
Arrivebridge(): Thead 14 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 14 goes accross the bridge
ExitBridge(): car 14 has left the bridge
ArriveBridge(): Car 15 goes accross the bridge
ExitBridge(): car 15 has left the bridge
ArriveBridge(): Car 16 goes accross the bridge
ExitBridge(): car 16 has left the bridge
Arrivebridge(): Thead 18 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 18 goes accross the bridge
ExitBridge(): car 18 has left the bridge
Arrivebridge(): Thead 17 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 17 goes accross the bridge
ExitBridge(): car 17 has left the bridge
ArriveBridge(): Car 19 goes accross the bridge
ExitBridge(): car 19 has left the bridge
Arrivebridge(): Thead 21 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 21 goes accross the bridge
ExitBridge(): car 21 has left the bridge
ArriveBridge(): Car 20 goes accross the bridge
ExitBridge(): car 20 has left the bridge
ArriveBridge(): Car 22 goes accross the bridge
ExitBridge(): car 22 has left the bridge
Arrivebridge(): Thead 23 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 23 goes accross the bridge
ExitBridge(): car 23 has left the bridge
Arrivebridge(): Thead 24 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 24 goes accross the bridge
ExitBridge(): car 24 has left the bridge
ArriveBridge(): Car 25 goes accross the bridge
ExitBridge(): car 25 has left the bridge
ArriveBridge(): Car 26 goes accross the bridge
ExitBridge(): car 26 has left the bridge
ArriveBridge(): Car 27 goes accross the bridge
ExitBridge(): car 27 has left the bridge
ArriveBridge(): Car 29 goes accross the bridge
ExitBridge(): car 29 has left the bridge
Arrivebridge(): Thead 28 is trying to go in the opposite direction, it must wait for traffic to clear
ArriveBridge(): Car 28 goes accross the bridge
ExitBridge(): car 28 has left the bridge
test
這是沒有該行注釋掉的輸出
****output of program before commenting pthread_cond_wait(&arrive_done, NULL); out:
101110010101011111010001000010
Segmentation fault
正如您所看到的,在創建任何線程之前,它幾乎立即失敗。
我試圖改進的另一件事是我的零和一系列不是很隨機。 是否有更好的方法來生成隨機數? 它不必非常隨機,但這個序列每次都完全相同。
謝謝你的時間
你需要實際將一個互斥量傳遞給pthread_cond_wait
,你傳遞的是NULL
。
對於從/dev/random
或/dev/urandom
讀取的隨機數據(至少在linux上)。 您也可以嘗試: direction = (rand() >> 8) & 1
你的主循環應該是:
pthread_mutex_lock(&arrive_mutex);
for(int j = 0 ; j < 30 ; j++)
{
if(j != 0)
{
// Pass a locked mutex as the second parameter.
pthread_cond_wait(&arrive_done, &arrive_mutex);
// This releases the lock and suspends the thread.
// When the condition variable is signaled. It re-establishes the lock
// then releases the thread. If another processes is holding the lock
// the released thread is stalled until it can acquire the lock.
//
// This means the child thread should acquire the lock on the mutex.
// call signal and then release the lock. If the child does not
// aquire the lock first then there is the potential for the child to
// reach the signal before the parent waits() if this happens then the
// parent will suspend forever (as nobody else will signal).
}
pthread_create(&threads[j], &attr, OneCar, (void *)&Thread_IDs[j]);
}
// Unlock the mutex afterwords.
pthread_mutex_unlock(&arrive_mutex);
我認為你需要將一個互斥鎖而不是NULL傳遞給pthread_cond_wait。 手冊頁指出:
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
這些函數以原子方式釋放互斥鎖並導致調用線程阻塞條件變量cond ; 原子地這里的意思是“相對於另一個線程訪問互斥鎖然后條件變量的原子性”。
它試圖以代碼的方式發布null互斥鎖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.