@@ -555,20 +555,23 @@ TEST(StaticReadWriteLockTest, ScopedWriteUnlockThreaded) {
555
555
template <typename RW> void readLockWhileReadLockedThreaded (RW &lock) {
556
556
lock.readLock ();
557
557
558
- std::vector<bool > results;
559
- results.assign (10 , false );
558
+ const int threadCount = 10 ;
559
+
560
+ std::atomic<bool > results[threadCount] = {};
560
561
561
562
std::atomic<bool > done (false );
562
- threadedExecute (10 ,
563
+ threadedExecute (threadCount ,
563
564
[&](int index) {
564
- while (!done) {
565
+ // Always perform at least one iteration of this loop to
566
+ // avoid spurious failures if this thread is slow to run.
567
+ do {
565
568
lock.withReadLock ([&] {
566
569
results[index] = true ;
567
570
std::this_thread::sleep_for (
568
571
std::chrono::milliseconds (5 ));
569
572
});
570
573
std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
571
- }
574
+ } while (!done);
572
575
},
573
576
[&] {
574
577
std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
@@ -577,7 +580,7 @@ template <typename RW> void readLockWhileReadLockedThreaded(RW &lock) {
577
580
578
581
lock.readUnlock ();
579
582
580
- for (auto result : results) {
583
+ for (auto & result : results) {
581
584
ASSERT_TRUE (result);
582
585
}
583
586
}
@@ -595,28 +598,31 @@ TEST(StaticReadWriteLockTest, ReadLockWhileReadLockedThreaded) {
595
598
template <typename RW> void readLockWhileWriteLockedThreaded (RW &lock) {
596
599
lock.writeLock ();
597
600
598
- std::vector<int > results;
599
- results.assign (10 , 0 );
601
+ const int threadCount = 10 ;
602
+
603
+ std::atomic<int > results[threadCount] = {};
600
604
601
605
std::atomic<bool > done (false );
602
- threadedExecute (10 ,
606
+ threadedExecute (threadCount ,
603
607
[&](int index) {
604
- while (!done) {
608
+ // Always perform at least one iteration of this loop to
609
+ // avoid spurious failures if this thread is slow to run.
610
+ do {
605
611
lock.withReadLock ([&] {
606
612
results[index] += 1 ;
607
613
std::this_thread::sleep_for (
608
614
std::chrono::milliseconds (5 ));
609
615
});
610
616
std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
611
- }
617
+ } while (!done);
612
618
},
613
619
[&] {
614
620
std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
615
621
done = true ;
616
622
lock.writeUnlock ();
617
623
});
618
624
619
- for (auto result : results) {
625
+ for (auto & result : results) {
620
626
ASSERT_EQ (result, 1 );
621
627
}
622
628
}
@@ -636,28 +642,29 @@ template <typename RW> void writeLockWhileReadLockedThreaded(RW &lock) {
636
642
637
643
const int threadCount = 10 ;
638
644
639
- std::vector<int > results;
640
- results.assign (threadCount, 0 );
645
+ std::atomic<int > results[threadCount] = {};
641
646
642
647
std::atomic<bool > done (false );
643
648
threadedExecute (threadCount,
644
649
[&](int index) {
645
- while (!done) {
650
+ // Always perform at least one iteration of this loop to
651
+ // avoid spurious failures if this thread is slow to run.
652
+ do {
646
653
lock.withWriteLock ([&] {
647
654
results[index] += 1 ;
648
655
std::this_thread::sleep_for (
649
656
std::chrono::milliseconds (5 ));
650
657
});
651
658
std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
652
- }
659
+ } while (!done);
653
660
},
654
661
[&] {
655
662
std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
656
663
done = true ;
657
664
lock.readUnlock ();
658
665
});
659
666
660
- for (auto result : results) {
667
+ for (auto & result : results) {
661
668
ASSERT_EQ (result, 1 );
662
669
}
663
670
}
@@ -677,28 +684,29 @@ template <typename RW> void writeLockWhileWriteLockedThreaded(RW &lock) {
677
684
678
685
const int threadCount = 10 ;
679
686
680
- std::vector<int > results;
681
- results.assign (threadCount, 0 );
687
+ std::atomic<int > results[threadCount] = {};
682
688
683
689
std::atomic<bool > done (false );
684
690
threadedExecute (threadCount,
685
691
[&](int index) {
686
- while (!done) {
692
+ // Always perform at least one iteration of this loop to
693
+ // avoid spurious failures if this thread is slow to run.
694
+ do {
687
695
lock.withWriteLock ([&] {
688
696
results[index] += 1 ;
689
697
std::this_thread::sleep_for (
690
698
std::chrono::milliseconds (5 ));
691
699
});
692
700
std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
693
- }
701
+ } while (!done);
694
702
},
695
703
[&] {
696
704
std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
697
705
done = true ;
698
706
lock.writeUnlock ();
699
707
});
700
708
701
- for (auto result : results) {
709
+ for (auto & result : results) {
702
710
ASSERT_EQ (result, 1 );
703
711
}
704
712
}
@@ -719,10 +727,12 @@ template <typename RW> void tryReadLockWhileWriteLockedThreaded(RW &lock) {
719
727
std::atomic<bool > done (false );
720
728
threadedExecute (10 ,
721
729
[&](int ) {
722
- while (!done) {
730
+ // Always perform at least one iteration of this loop to
731
+ // avoid spurious failures if this thread is slow to run.
732
+ do {
723
733
ASSERT_FALSE (lock.try_readLock ());
724
734
std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
725
- }
735
+ } while (!done);
726
736
},
727
737
[&] {
728
738
std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
@@ -747,19 +757,20 @@ template <typename RW> void tryReadLockWhileReadLockedThreaded(RW &lock) {
747
757
748
758
const int threadCount = 10 ;
749
759
750
- std::vector<bool > results;
751
- results.assign (threadCount, false );
760
+ std::atomic<bool > results[threadCount] = {};
752
761
753
762
std::atomic<bool > done (false );
754
763
threadedExecute (threadCount,
755
764
[&](int index) {
756
- while (!done) {
765
+ // Always perform at least one iteration of this loop to
766
+ // avoid spurious failures if this thread is slow to run.
767
+ do {
757
768
ASSERT_TRUE (lock.try_readLock ());
758
769
results[index] = true ;
759
770
std::this_thread::sleep_for (std::chrono::milliseconds (5 ));
760
771
lock.readUnlock ();
761
772
std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
762
- }
773
+ } while (!done);
763
774
},
764
775
[&] {
765
776
std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
@@ -768,7 +779,7 @@ template <typename RW> void tryReadLockWhileReadLockedThreaded(RW &lock) {
768
779
769
780
lock.readUnlock ();
770
781
771
- for (auto result : results) {
782
+ for (auto & result : results) {
772
783
ASSERT_TRUE (result);
773
784
}
774
785
}
@@ -789,10 +800,12 @@ template <typename RW> void tryWriteLockWhileWriteLockedThreaded(RW &lock) {
789
800
std::atomic<bool > done (false );
790
801
threadedExecute (10 ,
791
802
[&](int ) {
792
- while (!done) {
803
+ // Always perform at least one iteration of this loop to
804
+ // avoid spurious failures if this thread is slow to run.
805
+ do {
793
806
ASSERT_FALSE (lock.try_writeLock ());
794
807
std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
795
- }
808
+ } while (!done);
796
809
},
797
810
[&] {
798
811
std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
@@ -818,10 +831,12 @@ template <typename RW> void tryWriteLockWhileReadLockedThreaded(RW &lock) {
818
831
std::atomic<bool > done (false );
819
832
threadedExecute (10 ,
820
833
[&](int ) {
821
- while (!done) {
834
+ // Always perform at least one iteration of this loop to
835
+ // avoid spurious failures if this thread is slow to run.
836
+ do {
822
837
ASSERT_FALSE (lock.try_writeLock ());
823
838
std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
824
- }
839
+ } while (!done);
825
840
},
826
841
[&] {
827
842
std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
0 commit comments