@@ -11,22 +11,28 @@ namespace
1111 std::mutex guard;
1212 cpptask::mutex guard2;
1313 cpptask::lockable_event event1;
14+ cpptask::lockable_semaphore sem1 (1 ,2 );
1415
1516 void processFuncLock ()
1617 {
1718 std::lock_guard<std::mutex> lock (guard);
1819 std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
1920 }
20- void processFuncLock2 ()
21+ void processFuncLockMutex ()
2122 {
2223 cpptask::process_lock<cpptask::mutex> lock (guard2); // allow use thread for calculations
2324 std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
2425 }
25- void processFuncLock3 ()
26+ void processFuncLockEvent ()
2627 {
2728 cpptask::process_lock<cpptask::lockable_event> lock (event1); // allow use thread for calculations
2829 std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
2930 }
31+ void processFuncLockSem ()
32+ {
33+ cpptask::process_lock<cpptask::lockable_semaphore> lock (sem1); // allow use thread for calculations
34+ std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
35+ }
3036}
3137
3238TEST_F (CppTaskTest, ProcessLock_Serial)
@@ -50,7 +56,7 @@ TEST_F(CppTaskTest, ProcessLock_Parallel)
5056 cpptask::TaskThreadPool threadPool (1 );
5157
5258 std::unique_lock<cpptask::mutex> lock (guard2);
53- auto f1 = cpptask::async (std::launch::async, processFuncLock2 );
59+ auto f1 = cpptask::async (std::launch::async, processFuncLockMutex );
5460 std::this_thread::sleep_for (std::chrono::milliseconds (10 )); // additional thread blocked, he stole task
5561 ASSERT_NO_THROW (cpptask::for_each (testArray2.begin (), testArray2.end (), CppTaskTestData::DoubleSqrt (), 4 )); // generate more tasks
5662 lock.unlock (); // unlock after second tasks done
@@ -64,7 +70,7 @@ TEST_F(CppTaskTest, ProcessLock_ParallelEvent)
6470{
6571 cpptask::TaskThreadPool threadPool (1 );
6672
67- auto f1 = cpptask::async (std::launch::async, processFuncLock3 );
73+ auto f1 = cpptask::async (std::launch::async, processFuncLockEvent );
6874 std::this_thread::sleep_for (std::chrono::milliseconds (10 )); // additional thread blocked, he stole task
6975 ASSERT_NO_THROW (cpptask::for_each (testArray2.begin (), testArray2.end (), CppTaskTestData::DoubleSqrt (), 4 )); // generate more tasks
7076 event1.notify (); // unlock after second tasks done
@@ -73,3 +79,18 @@ TEST_F(CppTaskTest, ProcessLock_ParallelEvent)
7379
7480 ASSERT_EQ (CppTaskTestData::instance ().getResultArray (), testArray2);
7581}
82+
83+ TEST_F (CppTaskTest, ProcessLock_ParallelSemaphore)
84+ {
85+ cpptask::TaskThreadPool threadPool (1 );
86+
87+ std::unique_lock<cpptask::lockable_semaphore> lock (sem1);
88+ auto f1 = cpptask::async (std::launch::async, processFuncLockSem);
89+ std::this_thread::sleep_for (std::chrono::milliseconds (10 )); // additional thread blocked, he stole task
90+ ASSERT_NO_THROW (cpptask::for_each (testArray2.begin (), testArray2.end (), CppTaskTestData::DoubleSqrt (), 4 )); // generate more tasks
91+ lock.unlock (); // unlock after second tasks done
92+
93+ f1.wait ();
94+
95+ ASSERT_EQ (CppTaskTestData::instance ().getResultArray (), testArray2);
96+ }
0 commit comments