























ReadWrit.h
/*
* ReadWrit.h
*
* Sample code for Multithreading Applications in Win32
* This is from Chapter 7, Listing 7-1
*
* Demonstrates an implementation of the
* Readers/Writers algorithm. This version
* gives preference to readers.
*////////////////////////////////////////////////////////
//
// Structure definition
//
typedef struct _RWLock
{
// Handle to a mutex that allows
// a single reader at a time access
// to the reader counter.
HANDLE hMutex;// Handle to a semaphore that keeps
// the data locked for either the
// readers or the writers.
HANDLE hDataLock;// The count of the number of readers.
// Can legally be zero or one while
// a writer has the data locked.
int nReaderCount;
} RWLock;//
// Reader/Writer prototypes
//
BOOL InitRWLock(RWLock *pLock);
BOOL DestroyRWLock(RWLock *pLock);
BOOL AcquireReadLock(RWLock *pLock);
int ReleaseReadLock(RWLock *pLock);
BOOL AcquireWriteLock(RWLock *pLock);
int ReleaseWriteLock(RWLock *pLock);
BOOL ReadOK(RWLock *pLock);
BOOL WriteOK(RWLock *pLock);
BOOL FatalError(
char *s);
代码
/*
* ReadWrit.c
*
* Sample code for "Multithreading Applications in Win32"
* This is from Chapter 7, various listings.
*
* Demonstrates an implementation of the
* Readers/Writers algorithm. This version
* gives preference to readers.
*/#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "ReadWrit.h"// If we wait more than 2 seconds, then something is probably wrong!
#define MAXIMUM_TIMEOUT 2000// Here's the pseudocode for what is going on:
//
// Lock for Reader:
// Lock the mutex
// Bump the count of readers
// If this is the first reader, lock the data
// Release the mutex
//
// Unlock for Reader:
// Lock the mutex
// Decrement the count of readers
// If this is the last reader, unlock the data
// Release the mutex
//
// Lock for Writer:
// Lock the data
//
// Unlock for Reader:
// Unlock the data
///////////////////////////////////////////////////////
BOOL MyWaitForSingleObject(HANDLE hObject)
{
DWORD result;
result
= WaitForSingleObject(hObject, MAXIMUM_TIMEOUT);BOOL InitRWLock(RWLock
*pLock)BOOL DestroyRWLock(RWLock
*pLock)CloseHandle(pLock
->hMutex);BOOL AcquireReadLock(RWLock
*pLock)ReleaseMutex(pLock
->hMutex);BOOL ReleaseReadLock(RWLock
*pLock)ReleaseMutex(pLock
->hMutex);BOOL AcquireWriteLock(RWLock
*pLock)BOOL ReleaseWriteLock(RWLock
*pLock)result
= ReleaseSemaphore(pLock->hDataLock, 1, &lPrevCount);BOOL ReadOK(RWLock
*pLock)BOOL WriteOK(RWLock
*pLock)
代码
/*
* List.c
*
* Sample code for "Multithreading Applications in Win32"
* This is from Chapter 7, Listing 7-1
*
* Demonstrates an implementation of the
* Readers/Writers algorithm. This version
* gives preference to readers.
*/#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "ReadWrit.h"///////////////////////////////////////////////////////
//
// Structure definition
//
typedef struct _Node
{
struct _Node *pNext;
char szBuffer[80];
} Node;
typedef
struct _ListBOOL DeleteList(List
*pList)DestroyRWLock(
&gpList->lock);GlobalFree(pList);
return TRUE;BOOL AddHead(List
*pList, Node *pNode)pNode
->pNext = pList->pHead;BOOL DeleteHead(List
*pList)pNode
= pList->pHead->pNext;BOOL Insert(List
*pList, Node *afterNode, Node *newNode)Node
*Next(List *pList, Node *pNode)gpList
= CreateList();hThrds[nThreadCount
++] = CreateThread(NULL,hThrds[nThreadCount
++] = CreateThread(NULL,hThrds[nThreadCount
++] = CreateThread(NULL,hThrds[nThreadCount
++] = CreateThread(NULL,DeleteList(gpList);
return EXIT_SUCCESS;FILE
* fp = fopen("List.c", "r");pNode
= GlobalAlloc(GPTR, sizeof(Node));ReleaseWriteLock(
&gpList->lock);AcquireReadLock(
&gpList->lock);ReleaseReadLock(
&gpList->lock);printf(
"Found %d lines with '%s'\n", nFoundCount, szSearch);od
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。