C 中的命名管道读取奇怪的结果

Named pipes in C reading weird result

我正在 windows 上使用 C 中的命名管道实现我的第一个程序。 我正在尝试为数字 pi 实现 Monte Carlo 方法。

创建随机数时,一切都生成正常。然后我发送数字并在主线程上接收它。

那里,不知何故,数据看起来很奇怪..

看来问题出在 ConnectNamedPipe 中,因为它 returns 错误 此外,如果我更改所有带句柄的路径,程序将在 ConnectNamedPipe 处挂起。 它看起来像这样:

z:-1.#QNAN0, x:-1.#QNAN0, y:0.000000
z:1.#INF00, x:310579431956368060000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:-0.000000, y:0.000000
z:1.#INF00, x:833189623448552210000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:1.#INF00, x:315573513832673330000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000.000000, y:0.000000
z:-1.#QNAN0, x:-1.#QNAN0, y:0.000000
z:1.#INF00, x:835455692328165330000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000
z:4.000000, x:2.000000, y:0.000000
z:0.000000, x:0.000000, y:0.000000

什么时候应该是这样的:

z:4.00000 x:0.475953, y:0.983213

这是生成数字的线程的代码:

 HANDLE readpipe, writepipe;

    int a;
    BOOL   fConnected = FALSE; 
    LPTSTR lpszPipename = TEXT("\\.\pipe\mynamedpipe1"); 


  unsigned int __stdcall stage1(void * param) {
    srand(time(NULL));
    printf("creating...\n");
    writepipe = CreateNamedPipe(
        lpszPipename, // name of the pipe
        PIPE_ACCESS_OUTBOUND, // 1-way pipe -- send only
        PIPE_TYPE_MESSAGE, 
        PIPE_UNLIMITED_INSTANCES, 
        1024, 
        1024, 
        0, // use default wait time
        NULL // use default security attributes
    );
    printf("created...\n");
     fConnected = ConnectNamedPipe("\\.\pipe\mynamedpipe1", NULL) ? 
         TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); 

         if(!fConnected)
         {
            printf("Not working...");
         }
    printf("connected...\n");
    double y[a];
    DWORD length;
    double x[a];
    int i;
    for (i = 0; i < a; i++) 
    {
        x[i] = ((double)rand()) / (double)RAND_MAX;
        y[i] = ((double)rand()) / (double)RAND_MAX;
        printf("%f  %f\n",x[i],y[i]);

    }
    printf("writing...\n");

    /*PIPE*/
    WriteFile( writepipe, x, sizeof(x), &length, 0);
    WriteFile( writepipe, y, sizeof(y), &length, 0);
    CloseHandle( writepipe );
    /*PIPE*/


    return 0;
}

这里是主线程:

int main( int argc, char* argv[] ){
    a = atoi(argv[1]);
    srand (time(NULL));

    /*PIPE*/
    HANDLE thread1,thread2;

    /*CreatePipe( &readpipe, &writepipe, 0, 0 );*/
    /*PIPE*/

    thread1 = (HANDLE)_beginthreadex( 0, 0, &stage1, 0, 0, 0 );
    WaitForSingleObject( thread1, INFINITE );
    printf("reading.1111..\n");
    CloseHandle(thread1);
    readpipe = CreateFile( lpszPipename, GENERIC_READ,0,               NULL,           OPEN_EXISTING,  0,NULL); 
    DWORD length;
    double x[a];
    double y[a];
    printf("reading...\n");

    ReadFile( readpipe, x, sizeof(x), &length, 0);
    ReadFile( readpipe, y, sizeof(y), &length, 0 );

    double z = 0.0;
    int n=0;
    int N=0;
    int i;
    for(i=0;i<a;i++)
    {
        z = x[i] * x[i] + y[i] * y[i];
        printf("z:%f, x:%f, y:%f\n",z,x[i],y[i]);
        if (z < 1) {
            n++;
        }
        N++;
    }

    CloseHandle( readpipe );

    printf("%d %d\n",n,N);
    double result = ((double)n / (double)N) * 4;
    printf("%f\n", result);
    getchar();

    return 0;
}

在开始线程之前,在主函数中调用 CreateNamedPipe。应该可以。