Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

threadsanitizer data race #254

Closed
royal-ai opened this issue Aug 4, 2021 · 2 comments
Closed

threadsanitizer data race #254

royal-ai opened this issue Aug 4, 2021 · 2 comments

Comments

@royal-ai
Copy link

royal-ai commented Aug 4, 2021

I try to write a multipthread test for this queue, and compile it with -fsanitize=thread which is a tool for checking datarace. And data race error just happened.
My testcase:

typedef typename moodycamel::ConcurrentQueue<long long> moodyQueue;
void moodyPushThread(moodyQueue &m)
{
    for(int i=0; i<queueNum; i++)
    {
        long long num = rand()%10000;
        m.enqueue(num);
    }
    for(int i=0; i<100; i++)
    {
        m.enqueue(-1);
    }
}

void moodyPopThread(moodyQueue &m)
{
    while(1)
    {
        long long num=0;
        m.try_dequeue(num);
        if(num==-1)break;
    }
}

Error Message:
WARNING: ThreadSanitizer: data race (pid=50231)
Write of size 8 at 0x7b48000bb5d0 by thread T2:
#0 bool moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::ImplicitProducer::enqueue<(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::AllocationMode)0, long long const&>(long long const&) concurrentqueue.h:2529 (testGoogleTest:x86_64+0x10001a95b)
#1 moodyPushThread(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&) test.cpp:491 (testGoogleTest:x86_64+0x10000b96d)
#2 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_deletestd::__1::__thread_struct >, void ()(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> > > >(void) thread:291 (testGoogleTest:x86_64+0x10001d467)

Previous read of size 8 at 0x7b48000bb5d0 by thread T8:
[failed to restore the stack]

Location is heap block of size 328 at 0x7b48000bb500 allocated by thread T1:
#0 malloc :3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x5186a)
#1 moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::Block* moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::requisition_block<(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::AllocationMode)0>() concurrentqueue.h:3087 (testGoogleTest:x86_64+0x10001bef7)
#2 bool moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::ImplicitProducer::enqueue<(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::AllocationMode)0, long long const&>(long long const&) concurrentqueue.h:2493 (testGoogleTest:x86_64+0x10001ab49)
#3 moodyPushThread(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&) test.cpp:491 (testGoogleTest:x86_64+0x10000b96d)
#4 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_deletestd::__1::__thread_struct >, void ()(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> > > >(void) thread:291 (testGoogleTest:x86_64+0x10001d467)

Thread T2 (tid=1382399, running) created by main thread at:
#0 pthread_create :3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x2cd8d)
#1 std::__1::thread::thread<void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >, void>(void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >&&) thread:307 (testGoogleTest:x86_64+0x10001d369)
#2 Queue2_moody_Test::TestBody() test.cpp:517 (testGoogleTest:x86_64+0x10000c052)
#3 void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x100032c5c)
#4 testing::Test::Run() gtest.cc:2474 (testGoogleTest:x86_64+0x100032b48)
#5 testing::TestInfo::Run() gtest.cc:2656 (testGoogleTest:x86_64+0x100034860)
#6 testing::TestCase::Run() gtest.cc:2774 (testGoogleTest:x86_64+0x100035846)
#7 testing::internal::UnitTestImpl::RunAllTests() gtest.cc:4649 (testGoogleTest:x86_64+0x1000412a6)
#8 bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x10004077c)
#9 testing::UnitTest::Run() gtest.cc:4257 (testGoogleTest:x86_64+0x100040695)
#10 main test.cpp:534 (testGoogleTest:x86_64+0x10000c2e1)

Thread T8 (tid=1382426, running) created by main thread at:
#0 pthread_create :3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x2cd8d)
#1 std::__1::thread::thread<void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >, void>(void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >&&) thread:307 (testGoogleTest:x86_64+0x10001d369)
#2 Queue2_moody_Test::TestBody() test.cpp:521 (testGoogleTest:x86_64+0x10000c170)
#3 void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x100032c5c)
#4 testing::Test::Run() gtest.cc:2474 (testGoogleTest:x86_64+0x100032b48)
#5 testing::TestInfo::Run() gtest.cc:2656 (testGoogleTest:x86_64+0x100034860)
#6 testing::TestCase::Run() gtest.cc:2774 (testGoogleTest:x86_64+0x100035846)
#7 testing::internal::UnitTestImpl::RunAllTests() gtest.cc:4649 (testGoogleTest:x86_64+0x1000412a6)
#8 bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x10004077c)
#9 testing::UnitTest::Run() gtest.cc:4257 (testGoogleTest:x86_64+0x100040695)
#10 main test.cpp:534 (testGoogleTest:x86_64+0x10000c2e1)

Thread T1 (tid=1382398, running) created by main thread at:
#0 pthread_create :3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x2cd8d)
#1 std::__1::thread::thread<void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >, void>(void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >&&) thread:307 (testGoogleTest:x86_64+0x10001d369)
#2 Queue2_moody_Test::TestBody() test.cpp:517 (testGoogleTest:x86_64+0x10000c022)
#3 void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x100032c5c)
#4 testing::Test::Run() gtest.cc:2474 (testGoogleTest:x86_64+0x100032b48)
#5 testing::TestInfo::Run() gtest.cc:2656 (testGoogleTest:x86_64+0x100034860)
#6 testing::TestCase::Run() gtest.cc:2774 (testGoogleTest:x86_64+0x100035846)
#7 testing::internal::UnitTestImpl::RunAllTests() gtest.cc:4649 (testGoogleTest:x86_64+0x1000412a6)
#8 bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x10004077c)
#9 testing::UnitTest::Run() gtest.cc:4257 (testGoogleTest:x86_64+0x100040695)
#10 main test.cpp:534 (testGoogleTest:x86_64+0x10000c2e1)

SUMMARY: ThreadSanitizer: data race concurrentqueue.h:2529 in bool moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::ImplicitProducer::enqueue<(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::AllocationMode)0, long long const&>(long long const&)

==================
WARNING: ThreadSanitizer: data race (pid=50231)
Write of size 8 at 0x7b48008e02e8 by thread T4:
#0 bool moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::ImplicitProducer::enqueue<(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::AllocationMode)0, long long const&>(long long const&) concurrentqueue.h:2529 (testGoogleTest:x86_64+0x10001a95b)
#1 moodyPushThread(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&) test.cpp:491 (testGoogleTest:x86_64+0x10000b96d)
#2 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_deletestd::__1::__thread_struct >, void ()(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> > > >(void) thread:291 (testGoogleTest:x86_64+0x10001d467)

Previous read of size 8 at 0x7b48008e02e8 by thread T8:
#0 bool moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::ImplicitProducer::dequeue(long long&) concurrentqueue.h:2581 (testGoogleTest:x86_64+0x10001c645)
#1 bool moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::try_dequeue(long long&) concurrentqueue.h:1129 (testGoogleTest:x86_64+0x10000be6b)
#2 moodyPopThread(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&) test.cpp:504 (testGoogleTest:x86_64+0x10000ba1a)
#3 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_deletestd::__1::__thread_struct >, void ()(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> > > >(void) thread:291 (testGoogleTest:x86_64+0x10001d467)

Location is heap block of size 328 at 0x7b48008e0200 allocated by thread T1:
#0 malloc :3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x5186a)
#1 moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::Block* moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::requisition_block<(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::AllocationMode)0>() concurrentqueue.h:3087 (testGoogleTest:x86_64+0x10001bef7)
#2 bool moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::ImplicitProducer::enqueue<(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::AllocationMode)0, long long const&>(long long const&) concurrentqueue.h:2493 (testGoogleTest:x86_64+0x10001ab49)
#3 moodyPushThread(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&) test.cpp:491 (testGoogleTest:x86_64+0x10000b96d)
#4 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_deletestd::__1::__thread_struct >, void ()(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> > > >(void) thread:291 (testGoogleTest:x86_64+0x10001d467)

Thread T4 (tid=1382401, running) created by main thread at:
#0 pthread_create :3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x2cd8d)
#1 std::__1::thread::thread<void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >, void>(void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >&&) thread:307 (testGoogleTest:x86_64+0x10001d369)
#2 Queue2_moody_Test::TestBody() test.cpp:517 (testGoogleTest:x86_64+0x10000c0b2)
#3 void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x100032c5c)
#4 testing::Test::Run() gtest.cc:2474 (testGoogleTest:x86_64+0x100032b48)
#5 testing::TestInfo::Run() gtest.cc:2656 (testGoogleTest:x86_64+0x100034860)
#6 testing::TestCase::Run() gtest.cc:2774 (testGoogleTest:x86_64+0x100035846)
#7 testing::internal::UnitTestImpl::RunAllTests() gtest.cc:4649 (testGoogleTest:x86_64+0x1000412a6)
#8 bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x10004077c)
#9 testing::UnitTest::Run() gtest.cc:4257 (testGoogleTest:x86_64+0x100040695)
#10 main test.cpp:534 (testGoogleTest:x86_64+0x10000c2e1)

Thread T8 (tid=1382426, running) created by main thread at:
#0 pthread_create :3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x2cd8d)
#1 std::__1::thread::thread<void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >, void>(void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >&&) thread:307 (testGoogleTest:x86_64+0x10001d369)
#2 Queue2_moody_Test::TestBody() test.cpp:521 (testGoogleTest:x86_64+0x10000c170)
#3 void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x100032c5c)
#4 testing::Test::Run() gtest.cc:2474 (testGoogleTest:x86_64+0x100032b48)
#5 testing::TestInfo::Run() gtest.cc:2656 (testGoogleTest:x86_64+0x100034860)
#6 testing::TestCase::Run() gtest.cc:2774 (testGoogleTest:x86_64+0x100035846)
#7 testing::internal::UnitTestImpl::RunAllTests() gtest.cc:4649 (testGoogleTest:x86_64+0x1000412a6)
#8 bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x10004077c)
#9 testing::UnitTest::Run() gtest.cc:4257 (testGoogleTest:x86_64+0x100040695)
#10 main test.cpp:534 (testGoogleTest:x86_64+0x10000c2e1)

Thread T1 (tid=1382398, running) created by main thread at:
#0 pthread_create :3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x2cd8d)
#1 std::__1::thread::thread<void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >, void>(void (&)(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>&), std::__1::reference_wrapper<moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits> >&&) thread:307 (testGoogleTest:x86_64+0x10001d369)
#2 Queue2_moody_Test::TestBody() test.cpp:517 (testGoogleTest:x86_64+0x10000c022)
#3 void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x100032c5c)
#4 testing::Test::Run() gtest.cc:2474 (testGoogleTest:x86_64+0x100032b48)
#5 testing::TestInfo::Run() gtest.cc:2656 (testGoogleTest:x86_64+0x100034860)
#6 testing::TestCase::Run() gtest.cc:2774 (testGoogleTest:x86_64+0x100035846)
#7 testing::internal::UnitTestImpl::RunAllTests() gtest.cc:4649 (testGoogleTest:x86_64+0x1000412a6)
#8 bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::)(), char const) gtest.cc:2438 (testGoogleTest:x86_64+0x10004077c)
#9 testing::UnitTest::Run() gtest.cc:4257 (testGoogleTest:x86_64+0x100040695)
#10 main test.cpp:534 (testGoogleTest:x86_64+0x10000c2e1)

SUMMARY: ThreadSanitizer: data race concurrentqueue.h:2529 in bool moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::ImplicitProducer::enqueue<(moodycamel::ConcurrentQueue<long long, moodycamel::ConcurrentQueueDefaultTraits>::AllocationMode)0, long long const&>(long long const&)

@royal-ai
Copy link
Author

royal-ai commented Aug 4, 2021

Is data race normal for lock-free implementation?

@cameron314
Copy link
Owner

For a given definition of normal, yes :-) ThreadSanitizer doesn't do well with lock-free code. See also #49 and #146.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants