c++ - 信号量上的 WaitForSingleObject 不等待,立即返回

标签 c++ windows file-mapping

我正在尝试使用 Windows 文件映射并使用信号量制作一个简单的客户端-服务器程序。客户端向服务器发送 2 个数字,服务器计算 nr1+nr2 和 nr1 * nr2。我尝试了一些方法,但它甚至对 1 个客户不起作用,我希望它对更多客户起作用。这是代码:

服务器:

#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;

typedef struct {
    int nr1;
    int nr2;
} Mesaj;

int main(int argc, char** argv) {

    Mesaj* mesaj;

    HANDLE createSemaphore = CreateSemaphore(NULL, 1, 1, "Semafor");
    if (createSemaphore == NULL || createSemaphore == INVALID_HANDLE_VALUE) {
        wcout << "Failed to create a semaphore\n";
    } else {
        wcout << "Created the semaphore\n";
    }

    HANDLE hMemory = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
            PAGE_READWRITE, 0, sizeof(Mesaj), "SharedMemory");

    WaitForSingleObject(createSemaphore, INFINITE);

    mesaj = (Mesaj*) MapViewOfFile(hMemory, FILE_MAP_READ, 0, 0, sizeof(Mesaj));

    printf("The numbers received are: %d, %d\n", mesaj->nr1, mesaj->nr2);

    int produs = mesaj->nr1 * mesaj->nr2;
    int suma = mesaj->nr1 + mesaj->nr2;

    printf("\nSuma numerelor este: %d iar produsul lor este: %d", suma, produs);

    ReleaseSemaphore(createSemaphore, 1, NULL);

    Sleep(INFINITE);

    return 0;
}

客户:

#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;

typedef struct {
    int nr1;
    int nr2;
} Mesaj;

int main(int argc, char** argv) {

    Mesaj* mesaj, *mesaj2;

    mesaj2 = (Mesaj*) malloc(sizeof(Mesaj));

    HANDLE hMemory = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE,
            "SharedMemory");

    if (hMemory == NULL) {
        wcout << "Error at OpenFileMapping\n";
    }

    HANDLE openSemaphore = OpenSemaphore(SEMAPHORE_ALL_ACCESS,TRUE,"Semafor");
    if(openSemaphore != NULL || openSemaphore != INVALID_HANDLE_VALUE){
        wcout<<"the semaphore is opened\n";
    }

    mesaj2 = (Mesaj*) MapViewOfFile(hMemory, FILE_MAP_WRITE, 0, 0,
            sizeof(Mesaj));

    int nr1 = 0, nr2 = 0;
    printf("Give a number: ");
    scanf("%d", &nr1);
    printf("Give another number: ");
    scanf("%d", &nr2);

    mesaj2->nr1 = nr1;
    mesaj2->nr2 = nr2;

    if (mesaj2 == NULL) {
        wcout << "Error\n"
    } else {
        wcout << "I sent " << mesaj2->nr1 << " and " << mesaj2->nr2 << endl;
    }


    system("pause");
    return 0;
}

我究竟做错了什么?我应该如何使用信号量?

最佳答案

When I open the server it doesn't wait for the client.

The documentation for CreateSemaphore

The state of a semaphore object is signaled when its count is greater than zero, and nonsignaled when its count is equal to zero. The lInitialCount parameter specifies the initial count.

您在创建信号量时传递了 lInitialCount=1。和 1 > 0,因此信号量发出信号并且 WaitForSingleObject 立即返回。

您可能希望创建初始计数为 0 的信号量,以便在有人调用 ReleaseSemaphore 之前它不会发出信号。

关于c++ - 信号量上的 WaitForSingleObject 不等待,立即返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19548588/

相关文章:

c++ - 使用在派生类中声明的方法

c++ - boost::ptr_vector 对比 std::vector<std::unique_ptr<T>>?

c++ - 为什么覆盖虚函数时不考虑访问限定符?

.net - 如何在 LiveCharts 中添加每点工具提示?如何让同一条线上的点有不同的颜色?

c++ - 如何递归删除目录?

windows - 驱动程序安装失败,因为交叉签名链不包含微软

data-structures - 红黑树与B树

c++ - windows下如何正确使用共享内存

c - 两个进程之间的命名共享内存

c++ - 你如何遍历元组?