双缓冲的誉抄的过程

原创 2016年06月01日 19:28:08

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

代码如下:

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

}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

基于双缓冲的DMX512协议

  • 2016-01-29 17:43
  • 61KB
  • 下载

GID+ 双缓冲

最近在学 GDI+ 为了提高实践就写了个图片处理+绘图程序。在写创建选区的时候就发现屏幕一直闪烁,以为我是在MouseMove事件里不断重绘整个窗口。这时我想到了双缓冲。GDI+ 的双缓冲很简单,下面...

MFC绘制动态曲线,用双缓冲绘图技术防闪烁

转载自:ZHY_ongu的博客:MFC绘制动态曲线,用双缓冲绘图技术防闪烁 先上效果图 随着时间的推移,曲线向右平移,同时X轴的时间坐标跟着更新。一、如何绘制动态曲线。所谓动画,都是一帧一帧的图...

双缓冲画图拖动问题

  • 2007-11-14 12:49
  • 37KB
  • 下载

Qt双缓冲绘图简例

双缓冲绘图 如果想在涂鸦板上绘制矩形,并且可以动态地绘制这个矩形,也就是说可以用鼠标画出随意大小的矩形,那该怎么办呢? 我们先进行下面的三步,最后引出所谓的双缓...

双缓冲和欢迎界面

  • 2008-06-07 09:47
  • 543KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)