如何在 C++ (Windows) 中同步两个进程?
How do I synchronize two processes in C++ (Windows)?
父进程通过调用 CreateProcess() 方法启动子进程,该方法 returns 甚至在子进程初始化之前。如何让父进程等到子进程初始化并开始执行? WaitForSingleObject() 调用使父进程等待,直到子线程终止或超时。
有没有类似的方法让父进程一直等到子进程初始化完成?
子进程初始化后可以create a named event in WinAPI in the parent process and set it to unsignalled state. Call WaitForSingleObject on the event handle. Then in the child process you can open event by name and signal it (call SetEvent())
如果您正在使用线程,让父线程等待子线程完成的最简单方法之一是使用 .join() 函数。在父进程中并使用
child.join();
父进程通过调用 CreateProcess() 方法启动子进程,该方法 returns 甚至在子进程初始化之前。如何让父进程等到子进程初始化并开始执行? WaitForSingleObject() 调用使父进程等待,直到子线程终止或超时。
有没有类似的方法让父进程一直等到子进程初始化完成?
子进程初始化后可以create a named event in WinAPI in the parent process and set it to unsignalled state. Call WaitForSingleObject on the event handle. Then in the child process you can open event by name and signal it (call SetEvent())
如果您正在使用线程,让父线程等待子线程完成的最简单方法之一是使用 .join() 函数。在父进程中并使用
child.join();