Linux下的信号量有一个有趣的特性,你可以让信号量阻塞,等候任何一个期望的值。这里,以阻塞等候信号量的值为零说明。
Linux的代码如下:
sem_buf.sem_num = 0;
sem_buf.sem_op = 0;
sem_buf.sem_flg = 0;
if (semop(mtx, &sem_buf, 1) == -1) {
if (EINTR != errno) {
return -1;
}
} else {
return 0;
}
Windows下先创建进程间通信的Semaphore,例如:
mtx = OpenSemaphore(SEMAPHORE_ALL_ACCESS/*SEMAPHORE_MODIFY_STATE*/, FALSE, TEXT(name));
if (NULL != mtx) {
fprintf(stderr, "Open mutex[%d] %s success! \n", i, name);
} else {
fprintf(stderr, "Open mutex[%d] %s failed! create... \n", i, name);
mtx = CreateSemaphore(
Linux的代码如下:
sem_buf.sem_num = 0;
sem_buf.sem_op = 0;
sem_buf.sem_flg = 0;
if (semop(mtx, &sem_buf, 1) == -1) {
if (EINTR != errno) {
return -1;
}
} else {
return 0;
}
Windows下先创建进程间通信的Semaphore,例如:
mtx = OpenSemaphore(SEMAPHORE_ALL_ACCESS/*SEMAPHORE_MODIFY_STATE*/, FALSE, TEXT(name));
if (NULL != mtx) {
fprintf(stderr, "Open mutex[%d] %s success! \n", i, name);
} else {
fprintf(stderr, "Open mutex[%d] %s failed! create... \n", i, name);
mtx = CreateSemaphore(