我是韓國人,我不擅長英語,但如果你給我評價那裏
我會很高興,並會嘗試去理解它。pthread(分割錯誤)
我創建了例如10個線程,並在創建後嘗試加入它們並返回值。
但是,當我加入最後一個線程時,出現了分段錯誤。
結果出來這樣的..
Before Thread 1 create
After Thread 1 create
Before Thread 0 create
After Thread 0 create
Before Thread 1 join
After Thread 1 join
Before Thread 0 join
Segmentation Fault(core dumped)
當我創建4個線程就好像
Before Thread 3 create
After Thread 3 create
Before Thread 2 create
After Thread 2 create
Before Thread 1 create
After Thread 1 create
Before Thread 0 create
After Thread 0 create
Before Thread 3 join
After Thread 3 join
Before Thread 2 join
After Thread 2 join
Before Thread 1 join
After Thread 1 join
Before Thread 0 join
Segmentation Fault(core dumped)
我似乎無法找到原因。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex_lock;
struct arg_struct {
int a;
int b;
};
void *t_function(void *data) {
pthread_mutex_lock(&mutex_lock);
struct arg_struct *arg = (struct arg_struct *)data;
long int s;
s = arg->a;
pthread_mutex_unlock(&mutex_lock);
return (void **)s;
}
int main()
{
int i;
pthread_t p_thread[2];
int thr_id;
int status;
struct arg_struct arg[2];
for(i = 1; i >= 0; i--) {
arg[i].a = i;
arg[i].b = i;
}
pthread_mutex_init(&mutex_lock, NULL);
for(i = 1; i >= 0; i--) {
printf("Before Thread %d create\n", i);
thr_id = pthread_create(&p_thread[i],NULL, t_function, (void *)&arg[i]);
printf("After Thread %d create\n", i);
usleep(1000);
}
int temp[2];
for(i = 1; i >= 0; i--) {
printf("Before Thread %d join\n", i);
pthread_join(p_thread[i], (void**)&status);
printf("After Thread %d join\n", i);
temp[i] = status;
}i
printf("%d%d", temp[1], temp[0]);
pthread_mutex_destroy(&mutex_lock);
return 0;
}
我不能重現這一點,我試過2和4線程。它的工作非常好,即使在Valgrind中也是如此。你能告訴我們關於這個問題的其他事嗎? – VolatileDream
您是否嘗試過調試器? gdb是你的朋友。 – vanza
歡迎來到SO。 :-) – 2012-01-11 00:36:54