簡単なセマフォプログラムを書こうとしていますが、OSXで異なる点がいくつか見つかりました。MountainLionと最新のXcodeバージョンを使用しています。構文エラーを忘れて、中括弧が欠落している..以下にさらにコードがあるため、完全なスニペットをコピーしませんでした、
基本的に私はsem_waitで停止し、それを超えないようにするコードを除いて。
コードがコンパイルされ、出力は次のようになります
Output:
-------
Semaphore wait failed with ret code: -1, and err: 9.
Semaphore init failed with ret code: -1, and err: 9.
エラーコード9に戻ると、それはEBADFです。
私のプログラムは
int main(int argc, char * argv[])
{
pthread_t tid1, tid2;
int rc;
rc = sem_unlink(&mutex);
rc = sem_open(&mutex, O_CREAT,O_RDWR,0);
rc = sem_wait(&mutex);
if(rc == 0) {
printf("Semaphore try wait ok!. \n");
} else {
printf("Semaphore wait failed with ret code: %d, and err: %d. \n",
rc, errno);
}
if(rc != SEM_FAILED) {
printf("Semaphore init ok!. \n");
} else {
printf("Semaphore init failed with ret code: %d, and err: %d. \n",
rc, errno);
return 0;
}
ここでの助けは非常に貴重です。