Собственно непонятно, каким образом меняется значение, если перед заданием значений используется WaitForSingleObject на dataMutex.
Единственное место, где еще меняется эта переменная, - в main в цикле while, но там используется тоже конструкция WaitForSingleObject на этот же dataMutex.
Весь код:
#include <stdio.h>
#include <Windows.h>
#include <time.h>
#include <memory.h>
#define SHAREDMEMORYNAME L"CHATBPSM"
#define INITMUTEXNAME L"INITCBMSHMUTEX"
#define INCPROCMUTEXNAME L"INCPROCMUTEX"
#define INPUTMUTEXNAME L"INPUTCBMSHMUTEX"
#define MSGLEN 1024
#define INFOLEN sizeof(clock_t)+3*sizeof(unsigned char)
#define SHAREDMEMORYSIZE MSGLEN+INFOLEN
// countofproc countofreaded pid clock message'
HANDLE initMutex;
HANDLE incReadCntMutex;
HANDLE inputMutex;
HANDLE dataMutex;
char* shared_data;
char* shared_message;
unsigned char* shared_countOfProcesses;
unsigned char* shared_countOfReadedProc;
unsigned char* shared_messagePID;
clock_t* shared_messageClock;
bool active = true;
unsigned int myID = 0;
char input_buffer[MSGLEN];
void mappingInit(HANDLE sharedMemory){
shared_data = (char*)MapViewOfFile(sharedMemory,FILE_MAP_ALL_ACCESS,0,0,SHAREDMEMORYSIZE);
shared_countOfProcesses = (unsigned char*)(shared_data);
shared_countOfReadedProc = (unsigned char*)(shared_data+1);
shared_messagePID = (unsigned char*)(shared_data+2);
shared_messageClock = (clock_t*)(shared_data+3);
shared_message = shared_data+INFOLEN;
}
DWORD WINAPI input_thread_function(LPVOID){
while(active){
gets(input_buffer);
//if(!strcmp(input_buffer,"#quit"))
//active = false;
WaitForSingleObject(dataMutex,INFINITE);
clock_t current_clock = clock();
memcpy(shared_message,input_buffer,MSGLEN);
*shared_messagePID = myID;
*shared_countOfReadedProc = 1;
*shared_messageClock = current_clock;
printf("SND: %d %d ]%03d:%s\n",*shared_countOfProcesses,*shared_countOfReadedProc,*shared_messagePID,shared_message);
ReleaseMutex(dataMutex);
}
return 0;
}
int main(){
HANDLE sharedMemory = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,SHAREDMEMORYNAME);
initMutex = CreateMutex(NULL,FALSE,INITMUTEXNAME);
dataMutex = CreateMutex(NULL,FALSE,L"DATAMUdTXCFGSFD");
//ReleaseMutex(dataMutex);
WaitForSingleObject(initMutex,INFINITE);
if(sharedMemory == NULL){
//инициализируем память
puts("Init\n");
sharedMemory = CreateFileMapping(INVALID_HANDLE_VALUE,0,PAGE_READWRITE,0,SHAREDMEMORYSIZE,SHAREDMEMORYNAME);
mappingInit(sharedMemory);
memset(shared_message,0,MSGLEN);
*shared_countOfProcesses = 1;
*shared_countOfReadedProc = 1;
*shared_messageClock = 0;
*shared_messagePID = 0xFF;
myID = 1;
}else{
puts("Already init\n");
mappingInit(sharedMemory);
if(*shared_countOfProcesses<0xff){
myID = ++*shared_countOfProcesses;
*shared_countOfReadedProc++;
}else{
puts("To many processes!\n");
ReleaseMutex(initMutex);
getchar();
return 1;
}
}
ReleaseMutex(initMutex);
printf("PID = %d\n",myID);
clock_t last_printed_message = 0;
CreateThread(NULL,0,input_thread_function,NULL,0,0);
while(active){
WaitForSingleObject(dataMutex,INFINITE);
if(*shared_messageClock > last_printed_message && *shared_messagePID!=myID){
printf("%d %d ]%03d:%s\n",*shared_countOfProcesses,*shared_countOfReadedProc,*shared_messagePID,shared_message);
*shared_countOfReadedProc++;
last_printed_message = *shared_messageClock;
}
ReleaseMutex(dataMutex);
}
UnmapViewOfFile(shared_data);
CloseHandle(sharedMemory);
CloseHandle(initMutex);
CloseHandle(dataMutex);
}
[CRITICAL](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608.aspx)_[SECTION](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684169.aspx)
?