Skip to content

Commit

Permalink
Refs #4554 using Poco::RWLock in a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Janik Zikovsky committed Jan 20, 2012
1 parent 336d26e commit 12d8156
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Code/Mantid/Build/CMake/CommonSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ include ( VersionNumber )
# Look for dependencies - bail out if any not found
###########################################################################

find_package ( Boost REQUIRED signals date_time regex ) # thread )
find_package ( Boost REQUIRED signals date_time regex )
include_directories( SYSTEM ${Boost_INCLUDE_DIRS} )
add_definitions ( -DBOOST_ALL_DYN_LINK )
# Need this defined globally for our log time values
Expand Down
152 changes: 70 additions & 82 deletions Code/Mantid/Framework/Kernel/test/MutexTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,40 @@
#include "MantidKernel/FunctionTask.h"
#include <iostream>
#include "MantidKernel/CPUTimer.h"

#include <Poco/RWLock.h>
using namespace Mantid::Kernel;

#define DATA_SIZE 10000000
std::vector<double> shared_data;


//#include <boost/thread.hpp>
//boost::shared_mutex _access;
//void reader()
//{
// boost::shared_lock< boost::shared_mutex > lock(_access);
// // do work here, without anyone having exclusive access
// for (size_t i=0; i<shared_data.size(); i++)
// {
// double val = shared_data[i];
// }
//}
//
//void conditional_writer()
//{
// boost::upgrade_lock< boost::shared_mutex > lock(_access);
// // do work here, without anyone having exclusive access
//
// if (true)
// {
// boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
// // do work here, but now you have exclusive access
// for (size_t i=0; i<shared_data.size(); i++)
// {
// shared_data[i] = 2.345;
// }
// }
//
// // do more work here, without anyone having exclusive access
//}
//
//void unconditional_writer()
//{
// boost::unique_lock< boost::shared_mutex > lock( _access );
// // do work here, with exclusive access
// shared_data.resize(shared_data.size()+1, 2.345);
// // Dumb thing to slow down the writer
// for (size_t i=0; i<shared_data.size(); i++)
// shared_data[i] = 4.567;
//}
Poco::RWLock _access;
void reader()
{
// std::cout << "Read started" << std::endl;
Poco::ScopedReadRWLock lock(_access);
// std::cout << "Read launching" << std::endl;
// do work here, without anyone having exclusive access
for (size_t i=0; i<shared_data.size(); i++)
{
double val = shared_data[i];
}
// std::cout << "Read finished" << std::endl;
}


void unconditional_writer()
{
// std::cout << "Write started" << std::endl;
Poco::ScopedWriteRWLock lock( _access );
// std::cout << "Write launching" << std::endl;
// do work here, with exclusive access
shared_data.resize(shared_data.size()+1, 2.345);
// Dumb thing to slow down the writer
for (size_t i=0; i<shared_data.size(); i++)
shared_data[i] = 4.567;
// std::cout << "Write finished" << std::endl;
}


class MutexTest : public CxxTest::TestSuite
Expand All @@ -72,48 +60,48 @@ class MutexTest : public CxxTest::TestSuite
{
}

// /** Launch a bunch of reading threads */
// void test_simultaneous_read()
// {
// ThreadPool pool;
// CPUTimer tim;
// size_t numTasks = 100;
// for (size_t i=0; i<numTasks; i++)
// pool.schedule( new FunctionTask(reader) );
// pool.joinAll();
// std::cout << tim << " to execute all " << numTasks << " tasks" << std::endl;
// }
//
// /** Launch a bunch of writing threads */
// void test_simultaneous_write()
// {
// ThreadPool pool;
// CPUTimer tim;
// size_t numTasks = 10;
// for (size_t i=0; i<numTasks; i++)
// pool.schedule( new FunctionTask(unconditional_writer) );
// pool.joinAll();
// std::cout << tim << " to execute all " << numTasks << " tasks" << std::endl;
// TSM_ASSERT_EQUALS( "The writers were all called", shared_data.size(), DATA_SIZE + numTasks)
// }
//
// /** Mix 1 writing thread for 9 reading threads */
// void test_write_blocks_readers()
// {
// ThreadPool pool;
// CPUTimer tim;
// size_t numTasks = 100;
// for (size_t i=0; i<numTasks; i++)
// {
// if (i%10 == 0)
// pool.schedule( new FunctionTask(unconditional_writer) );
// else
// pool.schedule( new FunctionTask(reader) );
// }
// pool.joinAll();
// std::cout << tim << " to execute all " << numTasks << " tasks" << std::endl;
// TSM_ASSERT_EQUALS( "The writers were all called", shared_data.size(), DATA_SIZE + numTasks/10)
// }
/** Launch a bunch of reading threads */
void test_simultaneous_read()
{
ThreadPool pool;
CPUTimer tim;
size_t numTasks = 50;
for (size_t i=0; i<numTasks; i++)
pool.schedule( new FunctionTask(reader) );
pool.joinAll();
std::cout << tim << " to execute all " << numTasks << " tasks" << std::endl;
}

/** Launch a bunch of writing threads */
void test_simultaneous_write()
{
ThreadPool pool;
CPUTimer tim;
size_t numTasks = 10;
for (size_t i=0; i<numTasks; i++)
pool.schedule( new FunctionTask(unconditional_writer) );
pool.joinAll();
std::cout << tim << " to execute all " << numTasks << " tasks" << std::endl;
TSM_ASSERT_EQUALS( "The writers were all called", shared_data.size(), DATA_SIZE + numTasks)
}

/** Mix 1 writing thread for 9 reading threads */
void test_write_blocks_readers()
{
ThreadPool pool;
CPUTimer tim;
size_t numTasks = 50;
for (size_t i=0; i<numTasks; i++)
{
if (i%10 == 0)
pool.schedule( new FunctionTask(unconditional_writer) );
else
pool.schedule( new FunctionTask(reader) );
}
pool.joinAll();
std::cout << tim << " to execute all " << numTasks << " tasks" << std::endl;
TSM_ASSERT_EQUALS( "The writers were all called", shared_data.size(), DATA_SIZE + numTasks/10)
}

};

Expand Down

0 comments on commit 12d8156

Please sign in to comment.