私のコードはテキストを印刷しないと思います
ああ、なぜここに来た!\n
しかし、そうです。
に何か「問題」がありsystem()
ますか? 削除すると、コードが思いどおりに実行され、停止したためです。
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
pthread_t id0, id1;
sem_t sp;
void *fun0(void *) {
// When erasing the following line "system("");",
// it block up, and doesn't print "oh why come here!\n".
// But with it, it print the text!
system("");
return NULL;
}
void *fun1(void *) {
sem_wait(&sp);
fprintf(stderr, "oh why come here!\n");
return NULL;
}
int main() {
sem_init(&sp, 0, 0);
pthread_create(&id0, 0, fun0, NULL);
pthread_create(&id1, 0, fun1, NULL);
void *stat0, *stat1;
pthread_join(id0, &stat0);
pthread_join(id1, &stat1);
return 0;
}
コンパイラ: gcc 4.1.2 Linux カーネル: 2.6.18
gcc 4.6.3、カーネル 3.2.0 でコンパイルしましたが、思い通りに動作しました。したがって、gcc 4.1.2 またはカーネル 2.6.18 が原因だと思います。