Рандеву

  • Автор темы Artemd999
  • Дата начала
A

Artemd999

#1
Помогите разобраться с программой, лучший вариант - комментарии к коду. Спасибо.

Код:
// Rendezvous.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>

const int N=10;
int buffer[N];	
HANDLE mutex;
DWORD id_server, id_client;
MSG msg;
bool Baccept, Bcall;

DWORD WINAPI client(LPVOID)
{
while (1)
{
while (!PostThreadMessage(id_server, WM_USER, (WPARAM)0, (LPARAM)0))
{
printf("PostThreadMessage error1\n");
Sleep(3000);
}
if (GetMessage(&msg, NULL, 0, 0))
if (msg.message == WM_COMMAND)
Baccept = true;
//DispatchMessage(&msg);
if (Baccept)
{
WaitForSingleObject(mutex,INFINITE);
for (int i=0; i < N; i++) 
printf("c%d ",buffer[i]);
printf("\n");
ReleaseMutex(mutex);
Sleep(2000);
}
Baccept = false;
}
return 0;
}
DWORD WINAPI server(LPVOID)
{
while (1)
{
if (GetMessage(&msg, NULL, 0, 0))
if (msg.message == WM_USER)
Bcall = true;
//DispatchMessage(&msg);
if (Bcall)
{
WaitForSingleObject(mutex,INFINITE);
for (int i=0; i < N; i++)
{
buffer[i] = rand()/1000;
printf("p%d ",buffer[i]);
}
printf("\n");
ReleaseMutex(mutex);
Sleep(2000);
}
while (!PostThreadMessage(id_client, WM_COMMAND, (WPARAM)0, (LPARAM)0))
{
printf("PostThreadMessage error2\n");
Sleep(3000);
}
Bcall = false;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
mutex = CreateMutex(NULL,false,NULL);
HANDLE srvr = CreateThread(NULL,0,server,NULL,0,&id_server);
HANDLE clnt = CreateThread(NULL,0,client,NULL,0,&id_client);
WaitForSingleObject(clnt,INFINITE);
WaitForSingleObject(srvr,INFINITE);
CloseHandle(mutex);
return 0;
}