问题描述
我有 3 个程序,每个程序都需要在一个文件中写入 1000 次.Mutex 必须在每 10 次写入后切换程序
I have 3 programs, each of them need to write 1000 times in one file. Mutex must switch programs after every 10 writings
#include "stdafx.h"
#include <Windows.h>
#include <fstream>
#include <iostream>
using namespace std;
HANDLE ghMutex;
void write()
{
ghMutex = OpenMutex(SYNCHRONIZE, false, (LPTSTR)"WriteData");
for (int i=0; i<100; i ++) {
WaitForSingleObject(ghMutex, INFINITE);
ofstream f1("c:\\write.txt", ios_base::app);
for (int j=0; j<10; j++)
f1 << 2;
ReleaseMutex(ghMutex);
f1.close();
Sleep(100);
}
}
void main(){
write();
}
第一个程序为f1 <
First program with "f1 << 1" and second with "f1 << 2"They aren't synchronize, how to make it?Help me to do it with 2 programs for beginning, please.
推荐答案
从您的描述中并不清楚您希望程序如何运行.您是否希望程序保证进程交替,如果是,您是否需要循环类型的排序(例如进程 1,然后进程 2,然后进程 3,然后再次进程 1)?
It's not clear from your descriptions exactly how you want your program to behave. Do you want the program to guarantee that the processes alternate, if so, do you want a round-robin type ordering (e.g. process 1, then process 2, then process 3, then process 1 again)?
但是,很明显,您当前的互斥锁用法是不正确的.首先,至少有一个进程必须调用 CreateMutex()
来实际创建互斥锁(否则就没有要打开的互斥锁).由于 CreateMutex()
说:
However, it's clear that your current mutex usage is incorrect. First, at least one process must call CreateMutex()
to actually create the mutex (or else there is no mutex to open). Since the documentation for CreateMutex()
says:
如果 lpName
匹配现有的命名互斥对象的名称,则该函数请求 MUTEX_ALL_ACCESS
访问权限.
假设请求更多访问对您来说不是问题(很少是),那么您可以让所有实例调用 CreateMutex()
确保它们都共享相同的互斥锁,无论哪个先启动创建是.
Assuming requesting more access is not a problem for you (it rarely is), then you can just have all instances call CreateMutex()
ensuring that they all share the same mutex and whichever starts first actually creates is.
这是一个简单的例子,其中的进程以不可预测的顺序交替(允许重复),但连续 10 次正确写入文件而不会被并发写入器中断.
This is a simple example with processes that alternate in an unpredictable order (repeats are allowed), but correctly write to the file 10 times in a row without being interrupted by concurrent writers.
#include <Windows.h>
#include <conio.h>
#include <iostream>
#include <fstream>
int main ( int, char ** )
{
// get a named mutex. open the same mutex as other
// concurrent instances.
const ::HANDLE mutex = ::CreateMutexW(0, FALSE, L"Fubar");
if (mutex == INVALID_HANDLE_VALUE)
{
const ::DWORD error = ::GetLastError();
std::cerr
<< "Could not open mutex (" << error << ")."
<< std::endl;
return (EXIT_FAILURE);
}
// open the input file. notice the flags, you need to make
// sure the 2nd process doesn't overwrite whatver the first
// wrote, etc.
std::ofstream file("foo.txt", std::ios::app);
if (!file.is_open())
{
std::cerr
<< "Could not open file."
<< std::endl;
return (EXIT_FAILURE);
}
// wait for user to unblock after launch. this gives time
// to start concurrent processes before we write to the
// file.
::getch();
// not sure how your application goes, but lets just
// repeatedly write to the file until we're fed up.
for (int j=0; j < 100; ++j)
{
// lock the mutex around the code that writes to the
// file in a loop.
::WaitForSingleObject(mutex, INFINITE);
for (int i=0; i < 10; ++i) {
file << "process " << ::GetCurrentProcessId() << std::endl;
}
::ReleaseMutex(mutex);
// slow down so the application doesn't finish before we
// unblock concurrent instances.
::Sleep(100);
}
// don't forget to clean up!
::CloseHandle(mutex);
}
启动此进程的多个实例(2 个或更多),当所有实例都启动后,在每个控制台窗口中按一个键即可让进程开始写入.输出文件将包含每个进程的 10 个输出.
Start multiple instances of this process (2 or more) and when all are launched, press a single key in each console window to have the processes start writing. The output file will contain burts of 10 outputs for each process.
这篇关于如何使用互斥锁对 3 个进程进行自动交换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!