关闭

双缓冲的誉抄的过程

526人阅读 评论(0) 收藏 举报
分类:

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

代码如下:

#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;

}


0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7047次
    • 积分:172
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类