#include <iostream>
#include <fstream>
#include <windosws.h>
struct Mutex {
Mutex() {
h = ::CreateMutex(0, false, "{any-GUID-1247965802375274724957}");
}
~Mutex() {
::CloseHandle(h);
}
HANDLE h;
};
Mutex mutex; // GLOBAL mutex
void dll_write_func() {
::WaitForSingleObject(mutex.h, INFINITE);
////////////////
// Write here
std::ofstrem f("output.txt");
f << "Test" << std::endl;
////////////////
::ReleaseMutex(mutex.h);
}
或者
struct MutexLock {
explicit MutexLock(Mutex & m) : m(m) {
::WaitForSingleObject(m.h, INFINITE);
}
~MutexLock() {
::ReleaseMutex(m.h);
}
Mutex & m;
};
void dll_write_func2() {
MutexLock mlock(mutex);
// Write here
std::ofstrem f("output.txt");
f << "Auto mutex Release" << std::endl;
}
輝煌!謝謝! – Rob 2010-02-18 20:31:29