Mutex 不同步 C++
Mutex doesn't synchronize C++
我有 2 个进程。第一个向另一个发送一些数据,同步每个动作。
它实际发送的数据如下:
Process : A sends 1
Process : B receives 1
Process : A sends 2
Process : B receives 2
问题是,当我 运行 进程 A 从头开始发送所有数据时,我看到如下内容:
Process : A sends 1
Process : A sends 2
Process : A sends 3
Process : B receives 3
我做了如下操作:
Process A
HANDLE mutex;
mutex = CreateMutex(NULL, FALSE, TEXT("mutex1"));
if (mutex == INVALID_HANDLE_VALUE) {
_tprintf(TEXT("Create mutex error !.\n"), GetLastError());
return 1;
}
for (int i = 0; i < sender_length;i++) {
WaitForSingleObject(mutex,INFINITE);
sendToB(data);
ReleaseMutex(mutex);
}
CloseHandle(mutex);
B 进程如下所示:
Process B:
HANDLE mutex;
mutex = OpenMutex(SYNCHRONIZE, FALSE, TEXT("mutex1"));
if (mutex == INVALID_HANDLE_VALUE) {
_tprintf(TEXT("Mutex error ! \n"), GetLastError());
return 1;
}
for (int i = 0; i < sender_length;i++) {
WaitForSingleObject(mutex,INFINITE);
receiveFromA(data);
ReleaseMutex(mutex);
}
CloseHandle(mutex);
我不确定这是你的问题,但我认为它很有可能至少是一个贡献者:Windows 锁已经有一段时间不公平了。有关详细信息,请参阅 Joe Duffy 的文章 Anti-convoy locks in Windows Server 2003 SP1 and Windows Vista。
Duffy 特别针对互斥锁说了以下内容(我添加了突出显示):
Of course, Windows locks are still a teensy bit fair. The wait lists for mutually exclusive locks are kept in FIFO order, and the OS always wakes the thread at the front of such wait queues. ... Now when a lock becomes unowned, a FIFO waking algorithm is still used, but the lock is immediately marked as unavailable. Another thread can sneak in and take the lock before the woken thread is even scheduled
那个线程可以是刚刚释放锁的线程。在您的代码中,释放互斥锁的线程接下来要做的就是尝试重新获取互斥锁,这是一个很好的位置。
我有 2 个进程。第一个向另一个发送一些数据,同步每个动作。 它实际发送的数据如下:
Process : A sends 1
Process : B receives 1
Process : A sends 2
Process : B receives 2
问题是,当我 运行 进程 A 从头开始发送所有数据时,我看到如下内容:
Process : A sends 1
Process : A sends 2
Process : A sends 3
Process : B receives 3
我做了如下操作:
Process A
HANDLE mutex;
mutex = CreateMutex(NULL, FALSE, TEXT("mutex1"));
if (mutex == INVALID_HANDLE_VALUE) {
_tprintf(TEXT("Create mutex error !.\n"), GetLastError());
return 1;
}
for (int i = 0; i < sender_length;i++) {
WaitForSingleObject(mutex,INFINITE);
sendToB(data);
ReleaseMutex(mutex);
}
CloseHandle(mutex);
B 进程如下所示:
Process B:
HANDLE mutex;
mutex = OpenMutex(SYNCHRONIZE, FALSE, TEXT("mutex1"));
if (mutex == INVALID_HANDLE_VALUE) {
_tprintf(TEXT("Mutex error ! \n"), GetLastError());
return 1;
}
for (int i = 0; i < sender_length;i++) {
WaitForSingleObject(mutex,INFINITE);
receiveFromA(data);
ReleaseMutex(mutex);
}
CloseHandle(mutex);
我不确定这是你的问题,但我认为它很有可能至少是一个贡献者:Windows 锁已经有一段时间不公平了。有关详细信息,请参阅 Joe Duffy 的文章 Anti-convoy locks in Windows Server 2003 SP1 and Windows Vista。
Duffy 特别针对互斥锁说了以下内容(我添加了突出显示):
Of course, Windows locks are still a teensy bit fair. The wait lists for mutually exclusive locks are kept in FIFO order, and the OS always wakes the thread at the front of such wait queues. ... Now when a lock becomes unowned, a FIFO waking algorithm is still used, but the lock is immediately marked as unavailable. Another thread can sneak in and take the lock before the woken thread is even scheduled
那个线程可以是刚刚释放锁的线程。在您的代码中,释放互斥锁的线程接下来要做的就是尝试重新获取互斥锁,这是一个很好的位置。