|
// TestSync.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <set>
#include "ReaderWriterLock.h"
using namespace nTrack;
using namespace nTrack::Sync;
nTrack::Sync::ReadWriteLock rwLock;
volatile int countr=0, countw=0;
DWORD WINAPI ReaderThread(void *id)
{
int threadId=(int)id;
const int iterations=10000;
for(int i=0; i<iterations; ++i) {
AutoLockReader readLock(&rwLock);
//AutoLockWriter writeLock(&rwLock);
//for(volatile double f=0.0; f<double(rand())/RAND_MAX*10000; f+=0.1);
#ifdef _DEBUG
//printf("%d iteration by reader thread %d\n", i, threadId);
#endif
countr++;
countw++;
//if(!(countw%100000)) printf("%.2f\n", float(i)/iterations*100);
}
return 0;
}
DWORD WINAPI WriterThread(void *id)
{
int threadId=(int)id;
for(int i=0; i<1000; ++i) {
//AutoLockReader readLock(&rwLock);
//try
{
AutoLockWriter writeLock(&rwLock/*, 100*/);
// AutoLockReader readLock2(&rwLock);
#ifdef _DEBUG
//printf("%d iteration by writer thread %d\n", i, threadId);
#endif
//for(volatile double f=0.0; f<double(rand())/RAND_MAX*10000; f+=0.1);
//Sleep(3);
countw++;
countr--;
}
/*catch(ReadWriteLock::TimeoutExpiredException e)
{
printf("WriterThread: timeout\n");
}
catch(ReadWriteLock::ImplicitEscalationException e)
{
printf("WriterThread: ImplicitEscalationException\n");
}*/
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned long timeStart=timeGetTime();
static const int numThreadsReaders=100;
vector<HANDLE> threadHandles;
for(int i=0; i<numThreadsReaders; ++i)
threadHandles.push_back(CreateThread(NULL, 0, ReaderThread, (void*)i, 0, NULL));
static const int numThreadsWriters=100;
for(int i=0; i<numThreadsWriters; ++i)
threadHandles.push_back(CreateThread(NULL, 0, WriterThread, (void*)i, 0, NULL));
for(int i=0; i<(int)threadHandles.size(); ++i)
WaitForSingleObject(threadHandles[i], INFINITE);
printf("Total running time: %d ms\n", timeGetTime()-timeStart);
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.