, , , , .
bool waitAndLockMutex();
void unlockMutex();
int getProcessCount();
void main() {
try {
waitAndLockMutex();
if (getProcessCount() > MAX_ALLOWED)
return;
doUsualWork();
} finally {
unlockMutex();
}
}
, , .NET
EDIT:
If you do not want to go along the path of counting the processes of interest, you can use the global mutex for this. Not sure if .NET provides this. But the bottom line is that you can get all the mutexes up to MAX, and in the process, if you get a Mutex that has not yet been created or ABANDONED, then you go ahead and let the process start, otherwise exit, saying if the maximum number is exceeded
void main() {
for (int i = 0; i < MAX; ++i) {
int status = TryToAcquireMutex("mutex" + i);
continue if (status == locked);
if (status == success || status == WAIT_ABANDONED) {
doUsusalWork();
}
}
}
source
share