我正在使用 pthread 在 C 中做一个小游戏。我有一个矩阵(int 的二维表),它的某些字符由数字(3、4、5,其他数字不是线程)表示。那些角色必须在我的矩阵中移动。
serveurAff
用于显示表格。
tabD
,tabCrim
并且tabJournaliste
是全球性的。它们都是指针。
这是我的代码main
:
int statut, i,arg[2];
while (!serveurAff.fin)
pthread_cond_wait(&serveurAff.termine, &serveurAff.mutex);
serveurAff.reqATraiter = 1;
serveurAff.fin = 0;
pthread_cond_signal(&serveurAff.requete);
pthread_mutex_unlock(&serveurAff.mutex);
//detective thread
for(i = 0; i < 3; i++){
arg[0] = tabD[i]->posY;
arg[1] = tabD[i]->posX;
printf("test1 \n ");
statut = pthread_create(&tabD[i]->threaddete, NULL, routinePersonne, (void *) &arg);
}
//criminel thread
for(i = 0; i < 5; i++){
arg[0] = tabCrim[i]->posY;
arg[1] = tabCrim[i]->posX;
printf("test2 \n ");
statut = pthread_create(&tabCrim[i]->threadcrim, NULL, routinePersonne, (void *) &arg);
}
//thread journaliste
for(i = 0; i < 3; i++){
arg[0] = tabJournaliste[i]->posY;
arg[1] = tabJournaliste[i]->posX;
printf("test3 \n ");
statut = pthread_create(&tabJournaliste[i]->threadjour, NULL, routinePersonne, (void *) &arg);
}
pthread_join(threadAff, NULL);
当我在开始我的 pthread 之前写一个时printf
,对于“一些迭代”位置是可以的
test1
test1
test1
test2
test2
test2
test2
test2
test3
test3
test3
coord1 => 9, coord0, 1
xancien => 9 yancien => 1
coord1 => 20, coord0, 7
xancien => 20 yancien => 7
azazaz
Etape 0
01 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
02 0 4 4 4 0 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0
03 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
04 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0
05 0 0 3 0 0 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0
coord1 => 2, coord0, 4
xancien => 2 yancien => 4
coord1 => 0, coord0, 0
coord1 => 20, coord0, 7
xancien => 20 yancien => 7
coord1 => 23, coord0, 4
xancien => 23 yancien => 4
xancien => 0 yancien => 0
coord1 => 0, coord0, 0
xancien => 0 yancien => 0
coord1 => 20, coord0, 7
xancien => 20 yancien => 7
06 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0
07 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
08 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 5 0 3 0 0 0 3 0 0
09 0 0 0 0 0 0 0 0 0 0 0 5 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
coord1 => 23, coord0, 4
xancien => 23 yancien => 4
coord1 => 0, coord0, 0
xancien => 0 yancien => 0
coord1 => 0, coord0, 0
xancien => 0 yancien => 0
coord1 => 0, coord0, 0
xancien => 0 yancien => 0
正如您在一些迭代之后所看到的,所有值都是 0 ...如果我printf("test1")
在启动线程之前不写,则值始终等于 0。
这里的一部分routinePersonne
:
void *routinePersonne(void *arg) {
while (1){
int* coord = (int*)arg;
int xancien = coord[1];
int yancien = coord[0];
printf("coord1 => %d, coord0, %d \n",xancien,yancien);
printf("xancien => %d yancien => %d \n",xancien,yancien);
int i =0;
crimines* c;
detective* d;
journaliste* j;
//on récupére le type de la personne
int element = g->carte[yancien][xancien];
switch(element){
case CRIMINEL:
c = malloc(sizeof(criminels));
while(tabCrim[i]->posX != xancien && tabCrim[i]->posY != yancien){
i++;
}
c = tabCrim[i];
break;
case DETECTIVE:
d = malloc(sizeof(detective));
while(tabD[i]->posX != xancien && tabD[i]->posY != yancien){
i++;
}
d = tabD[i];
break;
case JOURNALISTE:
j = malloc(sizeof(journaliste));
while(tabJournaliste[i]->posX != xancien && tabJournaliste[i]->posY != yancien){
i++;
}
j = tabJournaliste[i];
}
...
}
所以我认为这是一个同步的问题,但我不知道在哪里......
ps:对不起我的英语:)