c++初步实现:
点击(此处)折叠或打开
- // 读者写者.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <Windows.h>
- using namespace std;
- const int readerNum=5;
- const int writerNum=5;
- const int readerSleep=100;
- const int writerSleep=100;
- const int waittime=100;
- static int nr=0;
- static int nw=0;
- static int readerCount=0;
- HANDLE mutex;
- HANDLE db;
- void acquireReadLock(){
- WaitForSingleObject(mutex,waittime);
- ++readerCount;
- if(readerCount==1)
- WaitForSingleObject(db,waittime);
- ReleaseMutex(mutex);
- }
- void releaseReadLock(){
- WaitForSingleObject(mutex,waittime);
- --readerCount;
- if(readerCount==0)
- ReleaseSemaphore(db,1,NULL);
- ReleaseMutex(mutex);
- }
- void acquireWriteLock(){
- WaitForSingleObject(db,waittime);
- }
- void releaseWriteLock(){
- ReleaseSemaphore(db,1,NULL);
- }
- DWORD WINAPI readerFun(LPVOID lparam)
- {
- Sleep(readerSleep);
- acquireReadLock();
- cout<<"reader:"<<nr++<<endl;
- Sleep(readerSleep);
- releaseReadLock();
- return(0);
- }
- DWORD WINAPI writerFun(LPVOID lparam)
- {
- Sleep(writerSleep);
- acquireWriteLock();
- cout<<"writer:"<<nw++<<endl;
- Sleep(writerSleep);
- releaseWriteLock();
- return(0);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- int i;
- HANDLE rThread[readerNum];
- HANDLE wThread[writerNum];
- mutex=CreateMutex(NULL,FALSE,NULL);
- db=CreateSemaphore(NULL,1,1,NULL);
- for(i=0 ; i<readerNum ;i++){
- rThread[i]=CreateThread(NULL,0,readerFun,&db,NULL,NULL);
- if(rThread[i]==NULL)
- return -1;
- }
- for(i=0 ; i<writerNum ; i++){
- wThread[i]=CreateThread(NULL,0,writerFun,&db,NULL,NULL);
- if(wThread[i]==NULL)
- return -1;
- }
- bool isContinue=true;
- while(isContinue){
- if(getchar())
- isContinue=false;
- }
- for(i=0 ;i<readerNum ; i++){
- CloseHandle(rThread[i]);
- }
- for(i=0 ; i<writerNum ; i++){
- CloseHandle(wThread[i]);
- }
- return 0;
- }
java初步实现:
点击(此处)折叠或打开
- import java.util.concurrent.Semaphore;
- interface RWLock
- {
- public abstract void acquireReadLock();
- public abstract void releaseReadLock();
- public abstract void acquireWriteLock();
- public abstract void releaseWriteLock();
- }
- class Database implements RWLock
- {
- private int readerCount;
- private Semaphore mutex;
- private Semaphore db;
-
- public Database(){
- readerCount=0;
- mutex=new Semaphore(1);
- db=new Semaphore(1);
- }
-
- public void acquireReadLock(){
- try {
- mutex.acquire();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ++readerCount;
-
- if(readerCount==1)
- try {
- db.acquire();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- mutex.release();
- }
-
- public void releaseReadLock(){
- try {
- mutex.acquire();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- --readerCount;
-
- if(readerCount==0)
- db.release();
-
- mutex.release();
- }
-
- public void acquireWriteLock(){
- try {
- db.acquire();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- public void releaseWriteLock(){
- db.release();
- }
- }
- class Reader implements Runnable
- {
- private RWLock db;
- private int readerNum=5;
- private long sleeptime=50;
-
- public Reader(RWLock db){
- this.db=db;
- }
-
- public void run(){
- for(int i=0 ; i<readerNum ; i++){
- try {
- Thread.sleep(sleeptime);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- db.acquireReadLock();
-
- System.out.println("reader:"+i);
-
- try {
- Thread.sleep(sleeptime);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- db.releaseReadLock();
- }
- }
- }
- class Writer implements Runnable
- {
- private RWLock db;
- private int writeNum=5;
- private long sleeptime=100;
-
- public Writer(RWLock db){
- this.db=db;
- }
-
- public void run(){
- for(int i=0 ; i<writeNum ; i++){
- try {
- Thread.sleep(sleeptime);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- db.acquireWriteLock();
-
- System.out.println("writer:"+i);
-
- try {
- Thread.sleep(sleeptime);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- db.releaseWriteLock();
- }
- }
- }
- public class 读者写者
- {
- public static void main(String[] args){
- RWLock db=new Database();
-
- Thread reader=new Thread(new Reader(db));
- Thread writer=new Thread(new Writer(db));
-
- reader.start();
- writer.start();
- }
- }
菜鸟学习中,求路过大神指点。