双缓冲的誉抄的过程

(151) 2024-05-19 18:01:01

程序一利用双缓冲、三线程的串行执行实现从输入文件到屏幕的拷贝输出:

代码如下:

#include<windows.h>

#include<iostream>

using namespacestd;

FILE *infile;

char buf1,buf2;

void get(){buf1=fgetc(infile);}

void copy(){buf2=buf1;}

void put( ){putchar(buf2);}

int main()

{

      infile=fopen("in.txt","r");

for(;buf1!=EOF;)

      {

           get();

           copy();

           put();

      }

fclose(infile);

return 0;

}

程序二实现线程的随机并发执行:

代码如下:

#include <cstdio>

#include "conio.h"

#include "windows.h"

#include <iostream>

using namespace std;

typedef struct _THREADDATA

{

      FILE*infile;

      char*buf1;

      char*buf2;

}*LPTHREADDATA, THREADDATA;

DWORD WINAPI get(LPVOID lParam)

{

   LPTHREADDATA pData = (LPTHREADDATA)lParam;

for (;*pData->buf1!=EOF;)

      {

           *pData->buf1=fgetc(pData->infile);    

           Sleep(1);

      }

 return 0;

}

DWORD WINAPI copy(LPVOID lParam)

{

LPTHREADDATA pData = (LPTHREADDATA)lParam;

for (;*pData->buf1!=EOF;)

      *pData->buf2=*pData->buf1;

      return0;

}

DWORD WINAPI put(LPVOID lParam)

{

   LPTHREADDATA pData = (LPTHREADDATA)lParam;

for (;*pData->buf1!=EOF;)

      {

           cout<<*pData->buf2<<endl;

           Sleep(1);

      }

return 0;

}

int main()

{

      FILE*infile=fopen("in.txt","r");

      charbuf1,buf2;

   LPTHREADDATA pData = new THREADDATA;

      pData->infile=infile;

      pData->buf1=&buf1;

      pData->buf2=&buf2;;

DWORD ThreadId1,ThreadId2,ThreadId3;

      ::CreateThread(NULL,0, get, (LPVOID)pData, 0, &ThreadId1);    

      ::CreateThread(NULL,0, copy, (LPVOID)pData, 0, &ThreadId2); 

      ::CreateThread(NULL,0, put, (LPVOID)pData, 0, &ThreadId3);      

getch();

      fclose(infile);

return0;

}

程序三控制进程实现正确的并发誊抄:

代码如下:

#include <cstdio>

#include "conio.h"

#include "windows.h"

#include <iostream>

using namespace std;

HANDLE Mutex;

HANDLE buf1_e;

HANDLE buf1_f;

HANDLE buf2_e;

HANDLE buf2_f;

typedef struct _THREADDATA

{

      FILE*infile;

      char*buf1;

      char*buf2;

}*LPTHREADDATA, THREADDATA;

DWORD WINAPI get(LPVOID lParam)

{

      LPTHREADDATApData = (LPTHREADDATA)lParam;

for (;*pData->buf1!=EOF;)

      {

           WaitForSingleObject(buf1_e,INFINITE);

           *pData->buf1=fgetc(pData->infile);    

           ReleaseSemaphore(buf1_f,1,NULL);

      }

 return 0;

}

 

DWORD WINAPI copy(LPVOID lParam)

{

      LPTHREADDATApData = (LPTHREADDATA)lParam;

for (;*pData->buf1!=EOF;)

      {

           WaitForSingleObject(buf1_f,INFINITE);

           WaitForSingleObject(buf2_e,INFINITE);

           *pData->buf2=*pData->buf1;

           ReleaseSemaphore(buf1_e,1,NULL);

           ReleaseSemaphore(buf2_f,1,NULL);

      }

return 0;

}

DWORD WINAPI put(LPVOID lParam)

{

      LPTHREADDATApData = (LPTHREADDATA)lParam;

for (;*pData->buf1!=EOF;)

      {

           WaitForSingleObject(buf2_f,INFINITE);

           cout<<*pData->buf2;

           ReleaseSemaphore(buf2_e,1,NULL);

      }

return 0;

}

int main()

{

 //  Mutex=CreateMutex(NULL,FALSE,NULL);  

      buf1_e=CreateSemaphore(NULL,1,1,NULL);

      buf1_f=CreateSemaphore(NULL,0,1,NULL);

      buf2_e=CreateSemaphore(NULL,1,1,NULL);

      buf2_f=CreateSemaphore(NULL,0,1,NULL);

      FILE*infile=fopen("in.txt","r");

      charbuf1,buf2;

   LPTHREADDATA pData = new THREADDATA;

      pData->infile=infile;

      pData->buf1=&buf1;

      pData->buf2=&buf2;

   DWORDThreadId1,ThreadId2,ThreadId3;

      ::CreateThread(NULL,0, copy, (LPVOID)pData, 0, &ThreadId2); 

      ::CreateThread(NULL,0, put, (LPVOID)pData, 0, &ThreadId3);      

      ::CreateThread(NULL,0, get, (LPVOID)pData, 0, &ThreadId1);    

getch();

      fclose(infile);

   return 0;

}

THE END

发表回复