Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20062
  • 博文数量: 7
  • 博客积分: 154
  • 博客等级: 入伍新兵
  • 技术积分: 71
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-14 21:23
文章分类

全部博文(7)

文章存档

2012年(7)

我的朋友

分类: WINDOWS

2012-06-04 22:12:29

c++初步实现:

点击(此处)折叠或打开

  1. // 读者写者.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <Windows.h>
  6. using namespace std;

  7. const int readerNum=5;
  8. const int writerNum=5;
  9. const int readerSleep=100;
  10. const int writerSleep=100;
  11. const int waittime=100;
  12. static int nr=0;
  13. static int nw=0;
  14. static int readerCount=0;
  15. HANDLE mutex;
  16. HANDLE db;

  17. void acquireReadLock(){
  18.     WaitForSingleObject(mutex,waittime);
  19.     ++readerCount;

  20.     if(readerCount==1)
  21.         WaitForSingleObject(db,waittime);

  22.     ReleaseMutex(mutex);
  23. }

  24. void releaseReadLock(){
  25.     WaitForSingleObject(mutex,waittime);
  26.     --readerCount;

  27.     if(readerCount==0)
  28.         ReleaseSemaphore(db,1,NULL);

  29.     ReleaseMutex(mutex);
  30. }

  31. void acquireWriteLock(){
  32.     WaitForSingleObject(db,waittime);
  33. }

  34. void releaseWriteLock(){
  35.     ReleaseSemaphore(db,1,NULL);
  36. }

  37. DWORD WINAPI readerFun(LPVOID lparam)
  38. {
  39.     Sleep(readerSleep);
  40.     acquireReadLock();

  41.     cout<<"reader:"<<nr++<<endl;

  42.     Sleep(readerSleep);
  43.     releaseReadLock();
  44.     return(0);
  45. }

  46. DWORD WINAPI writerFun(LPVOID lparam)
  47. {
  48.     Sleep(writerSleep);
  49.     acquireWriteLock();

  50.     cout<<"writer:"<<nw++<<endl;

  51.     Sleep(writerSleep);
  52.     releaseWriteLock();
  53.     return(0);
  54. }

  55. int _tmain(int argc, _TCHAR* argv[])
  56. {
  57.     int i;
  58.     HANDLE rThread[readerNum];
  59.     HANDLE wThread[writerNum];

  60.     mutex=CreateMutex(NULL,FALSE,NULL);
  61.     db=CreateSemaphore(NULL,1,1,NULL);

  62.     for(i=0 ; i<readerNum ;i++){
  63.         rThread[i]=CreateThread(NULL,0,readerFun,&db,NULL,NULL);
  64.         if(rThread[i]==NULL)
  65.             return -1;
  66.     }

  67.     for(i=0 ; i<writerNum ; i++){
  68.         wThread[i]=CreateThread(NULL,0,writerFun,&db,NULL,NULL);
  69.         if(wThread[i]==NULL)
  70.             return -1;
  71.     }

  72.     bool isContinue=true;
  73.     while(isContinue){
  74.         if(getchar())
  75.             isContinue=false;
  76.     }

  77.     for(i=0 ;i<readerNum ; i++){
  78.         CloseHandle(rThread[i]);
  79.     }

  80.     for(i=0 ; i<writerNum ; i++){
  81.         CloseHandle(wThread[i]);
  82.     }

  83.     return 0;
  84. }
java初步实现:

点击(此处)折叠或打开

  1. import java.util.concurrent.Semaphore;


  2. interface RWLock
  3. {
  4.     public abstract void acquireReadLock();
  5.     public abstract void releaseReadLock();
  6.     public abstract void acquireWriteLock();
  7.     public abstract void releaseWriteLock();
  8. }

  9. class Database implements RWLock
  10. {
  11.     private int readerCount;
  12.     private Semaphore mutex;
  13.     private Semaphore db;
  14.     
  15.     public Database(){
  16.         readerCount=0;
  17.         mutex=new Semaphore(1);
  18.         db=new Semaphore(1);
  19.     }
  20.     
  21.     public void acquireReadLock(){
  22.         try {
  23.             mutex.acquire();
  24.         } catch (InterruptedException e) {
  25.             // TODO Auto-generated catch block
  26.             e.printStackTrace();
  27.         }
  28.         ++readerCount;
  29.         
  30.         if(readerCount==1)
  31.             try {
  32.                 db.acquire();
  33.             } catch (InterruptedException e) {
  34.                 // TODO Auto-generated catch block
  35.                 e.printStackTrace();
  36.             }
  37.         
  38.         mutex.release();
  39.     }
  40.     
  41.     public void releaseReadLock(){
  42.         try {
  43.             mutex.acquire();
  44.         } catch (InterruptedException e) {
  45.             // TODO Auto-generated catch block
  46.             e.printStackTrace();
  47.         }
  48.         --readerCount;
  49.         
  50.         if(readerCount==0)
  51.             db.release();
  52.         
  53.         mutex.release();
  54.     }
  55.     
  56.     public void acquireWriteLock(){
  57.         try {
  58.             db.acquire();
  59.         } catch (InterruptedException e) {
  60.             // TODO Auto-generated catch block
  61.             e.printStackTrace();
  62.         }
  63.         
  64.     }
  65.     
  66.     public void releaseWriteLock(){
  67.         db.release();
  68.     }
  69. }

  70. class Reader implements Runnable
  71. {
  72.     private RWLock db;
  73.     private int readerNum=5;
  74.     private long sleeptime=50;
  75.     
  76.     public Reader(RWLock db){
  77.         this.db=db;
  78.     }
  79.     
  80.     public void run(){
  81.         for(int i=0 ; i<readerNum ; i++){
  82.             try {
  83.                 Thread.sleep(sleeptime);
  84.             } catch (InterruptedException e) {
  85.                 // TODO Auto-generated catch block
  86.                 e.printStackTrace();
  87.             }
  88.             
  89.             db.acquireReadLock();
  90.             
  91.             System.out.println("reader:"+i);
  92.             
  93.             try {
  94.                 Thread.sleep(sleeptime);
  95.             } catch (InterruptedException e) {
  96.                 // TODO Auto-generated catch block
  97.                 e.printStackTrace();
  98.             }
  99.             
  100.             db.releaseReadLock();
  101.         }
  102.     }
  103. }

  104. class Writer implements Runnable
  105. {
  106.     private RWLock db;
  107.     private int writeNum=5;
  108.     private long sleeptime=100;
  109.     
  110.     public Writer(RWLock db){
  111.         this.db=db;
  112.     }
  113.     
  114.     public void run(){
  115.         for(int i=0 ; i<writeNum ; i++){
  116.             try {
  117.                 Thread.sleep(sleeptime);
  118.             } catch (InterruptedException e) {
  119.                 // TODO Auto-generated catch block
  120.                 e.printStackTrace();
  121.             }
  122.             
  123.             db.acquireWriteLock();
  124.             
  125.             System.out.println("writer:"+i);
  126.             
  127.             try {
  128.                 Thread.sleep(sleeptime);
  129.             } catch (InterruptedException e) {
  130.                 // TODO Auto-generated catch block
  131.                 e.printStackTrace();
  132.             }
  133.             
  134.             db.releaseWriteLock();
  135.         }
  136.     }
  137. }

  138. public class 读者写者
  139. {
  140.     public static void main(String[] args){
  141.         RWLock db=new Database();
  142.         
  143.         Thread reader=new Thread(new Reader(db));
  144.         Thread writer=new Thread(new Writer(db));
  145.         
  146.         reader.start();
  147.         writer.start();
  148.     }
  149. }
 菜鸟学习中,求路过大神指点。
阅读(857) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册