文档介绍:C语言线程问题,异步Share10/asynchronous10(2010-11-1518:27)Categories:CandC++andCoreOpenafiledescriptor(handle),aprocess(thread)towrite,aprocess(thread)toread・Thiscanbedonewithouthavingtoopenthefilemultipletimes,butthewritesidemustrefreshthebufferandthereadendmustresetthefilepointer・monlyusedtoallowthesamefiletobereadandwrittenbydifferentmodules・There,snoproblemwiththat・AsynchronousIoandmulti-+,youoftenuseselect,,whichhasthreadpools・pletionportprogrammingissimilartotheselect,activequeryevent,theeventnotificationbythebackgroundthreadpoolfinished,don,tneedtoregisterthecallback,,itistheApeofWindows・Linux'sepollisalsoakernelcallbackthatdoesnotrequireuserregistration(pletionStatus),,:Unixformulti-processimplementation:includestdio,h>includesys/>includesys/.>>>Intmain(void){Intfd二open(“,0_RDWR0_CREAT,S_IRWXU):If(0二二fd){Printf(“openfailed\n〃);Return1;Pid_tp二fork();Charbuf[20]・If(p){//fatherWait(0);Lseek(fd,0,SEEK_SET);If(6!二read(fd,sizeof(buf)){Printf(“readerror%d:%s\n〃,errno,strerror(errno));Return1;}Printf(“contentread:%s\n〃,buf);Close(fd);Elseif(0==p){}Write(fd,〃hello〃,6);Sync();Return0;}Programoutput:>・/:hello!Sourcecode2:multi-threadedimplementationunderWindows:include〃stdafx・H〃include<Windows・H>include<stdio,h>HANDLEhFile;Void*fun(void*){.Charhw[]二,,helloworld,/;DWORDdwWriten;If(.writefile,(LPVOID)hw,sizeof(hw),&dwWriten(NULL))Printf(“WriteFilefailed:%d\n〃,GetLastError());If(!FlushFileBuffers(hFile)){...Printf(“Flushfailed\n〃);}ReturnNULL;}Int_tmain(intargc,_