生产者消费者问题算法的实现

上传者: kylin_bb | 上传时间: 2021-06-03 11:43:13 | 文件大小: 2.21MB | 文件类型: RAR
《生产者与消费者问题算法实现》 设计思想 因为有多个缓冲区,所以生产者线程没有必要在生成新的数据之前等待最后一个数据被消费者线程处理完毕。同样,消费者线程并不一定每次只能处理一个数据。在多缓冲区机制下,线程之间不必互相等待形成死锁,因而提高了效率。   多个缓冲区就好像使用一条传送带替代托架,传送带上一次可以放多个产品。生产者在缓冲区尾加入数据,而消费者则在缓冲区头读取数据。当缓冲区满的时候,缓冲区就上锁并等待消费者线程读取数据;每一个生产或消费动作使得传送带向前移动一个单位,因而,消费者线程读取数据的顺序和数据产生顺序是相同的。 可以引入一个count计数器来表示已经被使用的缓冲区数量。用hNotEmptyEvent 和hNotFullEvent 来同步生产者和消费者线程。每当生产者线程发现缓冲区满( count=BufferSize ),它就等待hNotEmptyEvent 事件。同样,当消费者线程发现缓冲区空,它就开始等待hNotEmptyEvent。生产者线程写入一个新的数据之后,就立刻发出hNotEmptyEvent 来唤醒正在等待的消费者线程;消费者线程在读取一个数据之后,就发出hNotFullEvent 来唤醒正在等待的生产者线程。 程序的设计思想大致为:设置一while循环,pi生产者访问临界区,得到权限访问缓冲区,如果缓冲区满的,则等待,直到缓冲区非满;访问互斥锁,当得到互斥锁且缓冲区非满时,跳出while循环,开始产生新数据,并把数据存放于Buffer缓冲区中,当数据存放结束则结束临界区;接着唤醒消费者线程;ci消费者访问临界区,得到权限访问缓冲区,如果缓冲区为空,没有可以处理的数据,则释放互斥锁且等待,直到缓冲区非空;当等到缓冲区非空时,跳出while循环;消费者获得数据,并根据所获得的数据按类别消费(当消费者获得的数据为大写字母时,则把大写字母转换成小写字母,并显示;当消费者获得的数据为小写字母时,则把小写字母转换成大写字母,并显示;当消费者获得的数据为字符0、1、2、……8、9时,把这些字符直接显示到屏幕;当消费者获得的数据为符号(+、-、*、\……)时,把这些符号打印成7行7列的菱形);处理完数据后,结束临界区;接着唤醒生产者线程。

文件下载

资源详情

[{"title":"( 99 个子文件 2.21MB ) 生产者消费者问题算法的实现","children":[{"title":"生产者消费者","children":[{"title":"train","children":[{"title":"test2.dsw <span style='color:#111;'> 527B </span>","children":null,"spread":false},{"title":"test2.rc <span style='color:#111;'> 1.42KB </span>","children":null,"spread":false},{"title":"StdAfx.cpp <span style='color:#111;'> 292B </span>","children":null,"spread":false},{"title":"test2.dsp <span style='color:#111;'> 4.23KB </span>","children":null,"spread":false},{"title":"test2.cpp <span style='color:#111;'> 978B </span>","children":null,"spread":false},{"title":"test2.h <span style='color:#111;'> 323B </span>","children":null,"spread":false},{"title":"test2.plg <span style='color:#111;'> 1.18KB </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"test2.ilk <span style='color:#111;'> 94.55KB </span>","children":null,"spread":false},{"title":"test2.exe <span style='color:#111;'> 96.08KB </span>","children":null,"spread":false},{"title":"vc60.pdb <span style='color:#111;'> 444.00KB </span>","children":null,"spread":false},{"title":"test2.obj <span style='color:#111;'> 14.29KB </span>","children":null,"spread":false},{"title":"vc60.idb <span style='color:#111;'> 241.00KB </span>","children":null,"spread":false},{"title":"StdAfx.obj <span style='color:#111;'> 112.48KB </span>","children":null,"spread":false},{"title":"test2.res <span style='color:#111;'> 128B </span>","children":null,"spread":false},{"title":"test2.pdb <span style='color:#111;'> 281.00KB </span>","children":null,"spread":false}],"spread":true},{"title":"ReadMe.txt <span style='color:#111;'> 1.56KB </span>","children":null,"spread":false},{"title":"StdAfx.h <span style='color:#111;'> 1.08KB </span>","children":null,"spread":false},{"title":"Resource.h <span style='color:#111;'> 446B </span>","children":null,"spread":false},{"title":"test2.ncb <span style='color:#111;'> 113.00KB </span>","children":null,"spread":false},{"title":"test2.opt <span style='color:#111;'> 79.00KB </span>","children":null,"spread":false},{"title":"test2.clw <span style='color:#111;'> 242B </span>","children":null,"spread":false},{"title":"su_343","children":[{"title":"su_343.clw <span style='color:#111;'> 243B </span>","children":null,"spread":false},{"title":"su_343.rc <span style='color:#111;'> 1.42KB </span>","children":null,"spread":false},{"title":"StdAfx.cpp <span style='color:#111;'> 293B </span>","children":null,"spread":false},{"title":"su_343.cpp <span style='color:#111;'> 2.49KB </span>","children":null,"spread":false},{"title":"su_343.plg <span style='color:#111;'> 1.20KB </span>","children":null,"spread":false},{"title":"su_343.dsp <span style='color:#111;'> 4.18KB </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"su_343.ilk <span style='color:#111;'> 183.40KB </span>","children":null,"spread":false},{"title":"vc60.pdb <span style='color:#111;'> 444.00KB </span>","children":null,"spread":false},{"title":"vc60.idb <span style='color:#111;'> 241.00KB </span>","children":null,"spread":false},{"title":"su_343.pdb <span style='color:#111;'> 321.00KB </span>","children":null,"spread":false},{"title":"su_343.exe <span style='color:#111;'> 100.09KB </span>","children":null,"spread":false},{"title":"su_343.res <span style='color:#111;'> 128B </span>","children":null,"spread":false},{"title":"StdAfx.obj <span style='color:#111;'> 112.53KB </span>","children":null,"spread":false},{"title":"su_343.obj <span style='color:#111;'> 38.13KB </span>","children":null,"spread":false}],"spread":false},{"title":"ReadMe.txt <span style='color:#111;'> 1.57KB </span>","children":null,"spread":false},{"title":"StdAfx.h <span style='color:#111;'> 1.08KB </span>","children":null,"spread":false},{"title":"Resource.h <span style='color:#111;'> 447B </span>","children":null,"spread":false},{"title":"su_343.h <span style='color:#111;'> 326B </span>","children":null,"spread":false}],"spread":false},{"title":"fuction.h <span style='color:#111;'> 1.06KB </span>","children":null,"spread":false},{"title":"test2.aps <span style='color:#111;'> 16.50KB </span>","children":null,"spread":false}],"spread":false},{"title":"生产者与消费者问题算法实现.txt <span style='color:#111;'> 1.72KB </span>","children":null,"spread":false},{"title":"su_343","children":[{"title":"su_343.clw <span style='color:#111;'> 243B </span>","children":null,"spread":false},{"title":"su_343.rc <span style='color:#111;'> 1.42KB </span>","children":null,"spread":false},{"title":"su_343.aps <span style='color:#111;'> 16.50KB </span>","children":null,"spread":false},{"title":"StdAfx.cpp <span style='color:#111;'> 293B </span>","children":null,"spread":false},{"title":"su_343.cpp <span style='color:#111;'> 2.49KB </span>","children":null,"spread":false},{"title":"su_343.dsw <span style='color:#111;'> 520B </span>","children":null,"spread":false},{"title":"su_343.opt <span style='color:#111;'> 47.50KB </span>","children":null,"spread":false},{"title":"su_343.plg <span style='color:#111;'> 1.78KB </span>","children":null,"spread":false},{"title":"su_343.dsp <span style='color:#111;'> 4.18KB </span>","children":null,"spread":false},{"title":"ReadMe.txt <span style='color:#111;'> 1.57KB </span>","children":null,"spread":false},{"title":"StdAfx.h <span style='color:#111;'> 1.08KB </span>","children":null,"spread":false},{"title":"Resource.h <span style='color:#111;'> 447B </span>","children":null,"spread":false},{"title":"su_343.ncb <span style='color:#111;'> 41.00KB </span>","children":null,"spread":false},{"title":"su_343.h <span style='color:#111;'> 326B </span>","children":null,"spread":false}],"spread":false},{"title":"test2","children":[{"title":"test2.dsw <span style='color:#111;'> 518B </span>","children":null,"spread":false},{"title":"test2.rc <span style='color:#111;'> 1.42KB </span>","children":null,"spread":false},{"title":"StdAfx.cpp <span style='color:#111;'> 292B </span>","children":null,"spread":false},{"title":"test2.dsp <span style='color:#111;'> 4.23KB </span>","children":null,"spread":false},{"title":"test2.cpp <span style='color:#111;'> 909B </span>","children":null,"spread":false},{"title":"GUITest","children":[{"title":"GUITestDlg.h <span style='color:#111;'> 1.49KB </span>","children":null,"spread":false},{"title":"StdAfx.cpp <span style='color:#111;'> 209B </span>","children":null,"spread":false},{"title":"GUITest.dsp <span style='color:#111;'> 4.08KB </span>","children":null,"spread":false},{"title":"GUITest.clw <span style='color:#111;'> 1.52KB </span>","children":null,"spread":false},{"title":"GUITest.cpp <span style='color:#111;'> 2.03KB </span>","children":null,"spread":false},{"title":"GUITest.h <span style='color:#111;'> 1.30KB </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"GUITest.ilk <span style='color:#111;'> 186.46KB </span>","children":null,"spread":false},{"title":"vc60.pdb <span style='color:#111;'> 356.00KB </span>","children":null,"spread":false},{"title":"GUITest.obj <span style='color:#111;'> 14.56KB </span>","children":null,"spread":false},{"title":"GUITest.res <span style='color:#111;'> 3.12KB </span>","children":null,"spread":false},{"title":"vc60.idb <span style='color:#111;'> 313.00KB </span>","children":null,"spread":false},{"title":"GUITestDlg.sbr <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"GUITest.exe <span style='color:#111;'> 108.09KB </span>","children":null,"spread":false},{"title":"StdAfx.obj <span style='color:#111;'> 103.35KB </span>","children":null,"spread":false},{"title":"GUITest.bsc <span style='color:#111;'> 2.99MB </span>","children":null,"spread":false},{"title":"GUITestDlg.obj <span style='color:#111;'> 28.26KB </span>","children":null,"spread":false},{"title":"GUITest.sbr <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"GUITest.pdb <span style='color:#111;'> 265.00KB </span>","children":null,"spread":false},{"title":"StdAfx.sbr <span style='color:#111;'> 1.31MB </span>","children":null,"spread":false}],"spread":false},{"title":"ReadMe.txt <span style='color:#111;'> 3.51KB </span>","children":null,"spread":false},{"title":"StdAfx.h <span style='color:#111;'> 1.03KB </span>","children":null,"spread":false},{"title":"GUITestDlg.cpp <span style='color:#111;'> 5.02KB </span>","children":null,"spread":false},{"title":"Resource.h <span style='color:#111;'> 869B </span>","children":null,"spread":false},{"title":"res","children":[{"title":"GUITest.rc2 <span style='color:#111;'> 399B </span>","children":null,"spread":false},{"title":"GUITest.ico <span style='color:#111;'> 1.05KB </span>","children":null,"spread":false}],"spread":false},{"title":"GUITest.aps <span style='color:#111;'> 21.24KB </span>","children":null,"spread":false},{"title":"GUITest.rc <span style='color:#111;'> 6.00KB </span>","children":null,"spread":false},{"title":"GUITest.plg <span style='color:#111;'> 1.79KB </span>","children":null,"spread":false}],"spread":false},{"title":"test2.h <span style='color:#111;'> 323B </span>","children":null,"spread":false},{"title":"test2.plg <span style='color:#111;'> 1.18KB </span>","children":null,"spread":false},{"title":"ReadMe.txt <span style='color:#111;'> 1.56KB </span>","children":null,"spread":false},{"title":"StdAfx.h <span style='color:#111;'> 1.08KB </span>","children":null,"spread":false},{"title":"Resource.h <span style='color:#111;'> 446B </span>","children":null,"spread":false},{"title":"test2.ncb <span style='color:#111;'> 97.00KB </span>","children":null,"spread":false},{"title":"test2.opt <span style='color:#111;'> 79.00KB </span>","children":null,"spread":false},{"title":"test2.clw <span style='color:#111;'> 242B </span>","children":null,"spread":false},{"title":"fuction.h <span style='color:#111;'> 1.07KB </span>","children":null,"spread":false},{"title":"test2.aps <span style='color:#111;'> 16.50KB </span>","children":null,"spread":false}],"spread":false}],"spread":true}],"spread":true}]

评论信息

  • ghghgh8032 :
    算法不是很完美,不过也算不错的参考
    2014-08-12
  • 媛媛媛 :
    资源不错,得到了应用
    2014-03-31
  • Qiushizhijian :
    缓冲区有多个,而且刷新速度慢点就好了,生产者线程没有必要在生成新的数据之前等待最后一个数据被消费者线程处理完毕
    2013-08-21
  • loadbalancing :
    能够成功编译运行。正是所需要的。谢谢共享。
    2013-02-17
  • Sanr :
    还不错,挺好的
    2012-11-27

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明