zoukankan      html  css  js  c++  java
  • 读写锁 SRWLOCK

           读写锁在对资源进行保护的同时,还能区分想要读取资源值的线程(读取者线程)和想要更新资源的线程(写入者线程)。

           对于读取者线程,读写锁会允许他们并发的执行。当有写入者线程在占有资源时,读写锁会让其它写入者线程和读取者线程等待

           用读写锁来解决读者写者问题会使代码非常清晰和简洁。

      

    typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
    typedef struct _RTL_SRWLOCK {                            
        PVOID Ptr;                                       
    } RTL_SRWLOCK, *PRTL_SRWLOCK; 
    
    // 初始化读写锁
    WINBASEAPI
    VOID
    WINAPI
    InitializeSRWLock (
         __out PSRWLOCK SRWLock
     );
    
    // 独占式访问
    WINBASEAPI
    VOID
    WINAPI
    AcquireSRWLockExclusive (
         __inout PSRWLOCK SRWLock
     );
    
    // 共享式访问
    WINBASEAPI
    VOID
    WINAPI
    AcquireSRWLockShared (
         __inout PSRWLOCK SRWLock
     );
    
    // 独占式释放
    WINBASEAPI
    VOID
    WINAPI
    ReleaseSRWLockExclusive (
         __inout PSRWLOCK SRWLock
     );
    
    // 共享式释放
    WINBASEAPI
    VOID
    WINAPI
    ReleaseSRWLockShared (
         __inout PSRWLOCK SRWLock
     );
    

      

    // SRWLock.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>  
    #include <windows.h>  
    #include <process.h>  
    
    using namespace std;
    unsigned int __stdcall ReadThread(PVOID ParameterData);
    unsigned int __stdcall WriteThread(PVOID ParameterData);
    
    const int NUM = 30, READER_SIZE = 10;
    HANDLE ThreadRead[NUM],  ThreadWrite[NUM];
    SRWLOCK __SRWLock;
    
    int main() 
    {
    	InitializeSRWLock(&__SRWLock);
    
    	int i = 0;
    	for (; i<NUM; i++) {
    		ThreadRead[i] = (HANDLE)_beginthreadex(NULL, 0, ReadThread, NULL, 0, NULL);
    		ThreadWrite[i] = (HANDLE)_beginthreadex(NULL, 0, WriteThread, NULL, 0, NULL);
    	}
    
    	WaitForMultipleObjects(NUM, ThreadRead, TRUE, INFINITE);
    	WaitForMultipleObjects(NUM, ThreadWrite, TRUE, INFINITE);
    
    	cout << "运行完毕" << endl;
    	getchar();
    	return 0;
    }
    
    unsigned int __stdcall ReadThread(PVOID ParameterData) {
    	AcquireSRWLockShared(&__SRWLock);
    
    	cout << "ID" << GetCurrentThreadId() << "   Read Thread Begin!" << endl;
    	Sleep(100);
    	cout << "ID" << GetCurrentThreadId() << "   Read Thread Terminate!" << endl;
    
    	ReleaseSRWLockShared(&__SRWLock);
    	return 0;
    }
    
    unsigned int __stdcall WriteThread(PVOID ParameterData) {
    	AcquireSRWLockExclusive(&__SRWLock);
    	cout << "ID" << GetCurrentThreadId() << "       WRITE Thread Begin~~~~~~~~~~" << endl;
    	Sleep(200);
    	cout << "ID" << GetCurrentThreadId() << "	    WRITE Thread Terminate~~~~~~" << endl;
    	ReleaseSRWLockExclusive(&__SRWLock);
    	return 0;
    }
    

      

  • 相关阅读:
    MyCat 监控与日志查看
    Python--day40--threading模块
    Python--day40--主线程和子线程代码讲解
    Python--day40线程理论
    Python--day39--进程池的回调函数callback
    Python--day39--进程池原理及效率测试
    Python--day39--管道和数据共享(面试可能会问到)
    Python--day38--JoinableQueue解决生产者消费者模型
    Python--day38---进程间通信--初识队列(multiprocess.Queue)之生产者,消费者模型
    NPOI vs EPPlus 导出Excel简单对比
  • 原文地址:https://www.cnblogs.com/lsh123/p/7376093.html
Copyright © 2011-2022 走看看