利用儲存對映實現執行緒間通訊

2021-04-26 07:28:38 字數 2006 閱讀 8993

#include "stdafx.h"

#include "stdio.h"

#include "afxmt.h"

int i=50;

handle hmutex1;

//ccriticalsection cmysection;

handle   g_hmutexshrmem;                 // used for hare memory write&read

bool writesharedmemory(char* pname,void *pdata, uint nsize)

waitforsingleobject(g_hmutexshrmem, infinite);

return false;

}if ( pmapview == null ) 

//clear share memory

memset( pmapview, 0, nsize );

//write to memory

memcpy( pmapview, pdata, nsize );

unmapviewoffile( pmapview );

releasemutex( g_hmutexshrmem );

return true;

}bool readsharedmemory( char* pname, void *pdata, uint nsize )

waitforsingleobject( g_hmutexshrmem, infinite );

if ( pmapview == null )

// read from share memory

memcpy( pdata, pmapview, nsize );

// 共有メモリの內容をクリアする

memset( pmapview, 0, nsize );

unmapviewoffile( pmapview );

releasemutex( g_hmutexshrmem );

return true;

}dword winapi clientthread(lpvoid lpparam)

//  else

int *p;

int m;

p = &m;

*p=10;

char a[10]="rongning";

//  a[0]='r';

//  a[1]='o';

//       a[2]='4';

char* tt;

tt=a;

writesharedmemory("nm3200",tt,10);

::releasemutex(hmutex1);

// }

return 0;

}dword winapi clientthread2(lpvoid lpparam)

//  else

char ct[12];

readsharedmemory("nm3200",ct,12);

printf(ct);

// for(int i=0;i<10;i++)

::releasemutex(hmutex1);

// }

return 0;

}int main(int argc, char* argv)

hthread2=createthread(null,0,clientthread2,(lpvoid)(num+1),0,&dwthreadid);

//    if(hthread2==null)

//   

//    gets(szbuff);

hmutex1 = createmutex(null,true,"mutext1");

::releasemutex(hmutex1);

sleep(7000);

closehandle(hthread1);

closehandle(hthread2);

return 0;

}

程序間通訊 儲存I O對映

儲存i o對映能將磁碟檔案對映到儲存空間的乙個緩衝區上,於是,從緩衝區讀資料時,就相當於讀檔案中的相應位元組。mmap函式 將檔案對映到乙個儲存區域 include void mmap void addr,size t len,int prot,int flag,int fd,off t off 執...

python Queue實現執行緒間通訊

python中queue是訊息佇列,提供執行緒間通訊機制,python3中重名為為queue queue模組中的類 queue maxsize 0 建立乙個fifo佇列,若給定最大值,佇列沒有空間時阻塞,否則是無限佇列 lifoqueue maxsize 0 建立乙個棧,maxsize含義同上 pr...

利用管道實現程序間通訊

管道 pipe 是程序用來通訊的共享記憶體區域。乙個程序往管道中寫入資訊,而其它的程序可以從管道中讀出資訊。如其名,管道是程序間資料交流的通道。郵路 mailslots 的功能與管道類似,也是程序間通訊 interprocess communications,ipc 的媒介,只不過其具體實現方式與管...