C++ 如何直接;分配;使用windows API的信号量进程?
我使用Microsoft提供的以下代码作为模板:C++ 如何直接;分配;使用windows API的信号量进程?,c++,windows,process,C++,Windows,Process,我使用Microsoft提供的以下代码作为模板: #include <windows.h> #include <stdio.h> #define MAX_SEM_COUNT 10 #define THREADCOUNT 12 HANDLE ghSemaphore; DWORD WINAPI ThreadProc( LPVOID ); int main( void ) { HANDLE aThread[THREADCOUNT]; DWORD Thre
#include <windows.h>
#include <stdio.h>
#define MAX_SEM_COUNT 10
#define THREADCOUNT 12
HANDLE ghSemaphore;
DWORD WINAPI ThreadProc( LPVOID );
int main( void )
{
HANDLE aThread[THREADCOUNT];
DWORD ThreadID;
int i;
// Create a semaphore with initial and max counts of MAX_SEM_COUNT
ghSemaphore = CreateSemaphore(
NULL, // default security attributes
MAX_SEM_COUNT, // initial count
MAX_SEM_COUNT, // maximum count
NULL); // unnamed semaphore
if (ghSemaphore == NULL)
{
printf("CreateSemaphore error: %d\n", GetLastError());
return 1;
}
// Create worker threads
for( i=0; i < THREADCOUNT; i++ )
{
aThread[i] = CreateThread(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE) ThreadProc,
NULL, // no thread function arguments
0, // default creation flags
&ThreadID); // receive thread identifier
if( aThread[i] == NULL )
{
printf("CreateThread error: %d\n", GetLastError());
return 1;
}
}
// Wait for all threads to terminate
WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);
// Close thread and semaphore handles
for( i=0; i < THREADCOUNT; i++ )
CloseHandle(aThread[i]);
CloseHandle(ghSemaphore);
return 0;
}
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
// lpParam not used in this example
UNREFERENCED_PARAMETER(lpParam);
DWORD dwWaitResult;
BOOL bContinue=TRUE;
while(bContinue)
{
// Try to enter the semaphore gate.
dwWaitResult = WaitForSingleObject(
ghSemaphore, // handle to semaphore
0L); // zero-second time-out interval
switch (dwWaitResult)
{
// The semaphore object was signaled.
case WAIT_OBJECT_0:
// TODO: Perform task
printf("Thread %d: wait succeeded\n", GetCurrentThreadId());
bContinue=FALSE;
// Simulate thread spending time on task
Sleep(5);
// Release the semaphore when task is finished
if (!ReleaseSemaphore(
ghSemaphore, // handle to semaphore
1, // increase count by one
NULL) ) // not interested in previous count
{
printf("ReleaseSemaphore error: %d\n", GetLastError());
}
break;
// The semaphore was nonsignaled, so a time-out occurred.
case WAIT_TIMEOUT:
printf("Thread %d: wait timed out\n", GetCurrentThreadId());
break;
}
}
return TRUE;
}
因此,尽管决定信号量填充的是线程,但在实际意义上,它是由进程句柄的完整执行和关闭决定的
但这看起来是解决这个问题的一个蹩脚的方法,我敢打赌,如果这些过程需要额外的东西,这样做很可能会在将来带来问题
我如何创建一个信号量,那么真正决定信号量填充的是进程?澄清一下,这将是一个可能的解决方案,我认为无论如何都不可能
让我们考虑一下,你可以用这样的方法创建一个过程:
aThread[i] = CreateProcess(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE) ThreadProc,
NULL, // no thread function arguments
0, // default creation flags
&ThreadID); // receive thread identifier
然后,LPTHREAD_START_例程的工作方式将是等效的,但进程除外
信号量应该在Windows API中支持,但我找不到任何具体使用进程的示例,我也不知道如何实现
你知道如何实现我想要的吗
问候。您需要一个命名的信号量。其中每个进程通过使用相同的名称创建信号量来共享信号量 创建一个命名的信号量。与前面的相同,但最后一个参数获取传递给它的字符串:
HANDLE hSemaphore = CreateSemaphore(NULL,
MAX_SEM_COUNT,
MAX_SEM_COUNT,
L"TheSemaphoreForMyApp");
子进程在启动时可以附加到同一个信号量,并通过使用获取该信号量的句柄
您不必硬编码字符串作为信号量名称。父进程每次都可以创建一个唯一的名称,然后将该名称(例如命令行参数)传递给子进程。这将允许您的程序的多个实例与子进程协作
HANDLE hSemaphore = CreateSemaphore(NULL,
MAX_SEM_COUNT,
MAX_SEM_COUNT,
L"TheSemaphoreForMyApp");
HANDLE hSemaphore = OpenSemaphore(EVENT_ALL_ACCESS,
FALSE,
L"TheSemaphoreForMyApp");