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