// SlimLock.cpp #include "SlimLock.h" #include #include #include #include #include #include "../Utilities/Utilities.h" void testAlone() { SlimLock lck; std::string shared = ""; std::function threadProc = [&](size_t id) { for (size_t i = 1; i < 20; ++i) { lck.lock(); shared += Utilities::Converter::toString(id); lck.unlock(); Sleep(0); // yield to allow other thread to run } }; std::thread t1(threadProc, 1); std::thread t2(threadProc, 2); t1.join(); t2.join(); std::cout << "\n shared = " << shared.c_str() << "\n"; } void testWithLock_Guard() { SlimLock lck; std::string shared = ""; std::function threadProc = [&](size_t id) { for (size_t i = 1; i < 20; ++i) { { std::lock_guard grd(lck); shared += Utilities::Converter::toString(id); } Sleep(0); // yield to allow other thread to run } }; std::thread t1(threadProc, 1); std::thread t2(threadProc, 2); t1.join(); t2.join(); std::cout << "\n shared = " << shared.c_str(); } using namespace Utilities; using Utils = StringHelper; int main() { Utils::Title("Testing SlimLock"); putline(); Utils::title("testing bare lock"); testAlone(); //putline(); Utils::title("testing wrapped in lock_guard"); testWithLock_Guard(); std::cout << "\n\n"; }