मेरा धागा सिंक्रनाइज़ेशन "शैली" हेल्ग्रिंड को फेंकने लग रहा है।हेल्ग्रिंड के साथ झूठी सकारात्मकताओं से कैसे बचें?
#include <thread>
#include <atomic>
#include <iostream>
int main()
{
std::atomic<bool> isReady(false);
int i = 1;
std::thread t([&isReady, &i]()
{
i = 2;
isReady = true;
});
while (!isReady)
std::this_thread::yield();
i = 3;
t.join();
std::cout << i;
return 0;
}
जहां तक मेरा बता सकते हैं, इसके बाद के संस्करण एक पूरी तरह से अच्छी तरह से गठित कार्यक्रम है: यह एक साधारण प्रोग्राम है कि समस्या reproduces है।
valgrind --tool=helgrind ./a.out
इस के उत्पादन में है:
==6247== Helgrind, a thread error detector
==6247== Copyright (C) 2007-2015, and GNU GPL'd, by OpenWorks LLP et al.
==6247== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==6247== Command: ./a.out
==6247==
==6247== ---Thread-Announcement------------------------------------------
==6247==
==6247== Thread #1 is the program's root thread
==6247==
==6247== ---Thread-Announcement------------------------------------------
==6247==
==6247== Thread #2 was created
==6247== at 0x56FBB1E: clone (clone.S:74)
==6247== by 0x4E46189: create_thread (createthread.c:102)
==6247== by 0x4E47EC3: [email protected]@GLIBC_2.2.5 (pthread_create.c:679)
==6247== by 0x4C34BB7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==6247== by 0x5115DC2: std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>, void (*)()) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==6247== by 0x4010EF: std::thread::thread<main::{lambda()#1}>(main::{lambda()#1}&&) (in /home/arman/a.out)
==6247== by 0x400F93: main (in /home/arman/a.out)
==6247==
==6247== ----------------------------------------------------------------
==6247==
==6247== Possible data race during read of size 1 at 0xFFF00035B by thread #1
==6247== Locks held: none
==6247== at 0x4022C3: std::atomic<bool>::operator bool() const (in /home/arman/a.out)
==6247== by 0x400F9F: main (in /home/arman/a.out)
==6247==
==6247== This conflicts with a previous write of size 1 by thread #2
==6247== Locks held: none
==6247== at 0x40233D: std::__atomic_base<bool>::operator=(bool) (in /home/arman/a.out)
==6247== by 0x40228E: std::atomic<bool>::operator=(bool) (in /home/arman/a.out)
==6247== by 0x400F4A: main::{lambda()#1}::operator()() const (in /home/arman/a.out)
==6247== by 0x40204D: void std::_Bind_simple<main::{lambda()#1}()>::_M_invoke<>(std::_Index_tuple<>) (in /home/arman/a.out)
==6247== by 0x401FA3: std::_Bind_simple<main::{lambda()#1}()>::operator()() (in /home/arman/a.out)
==6247== by 0x401F33: std::thread::_Impl<std::_Bind_simple<main::{lambda()#1}()> >::_M_run() (in /home/arman/a.out)
==6247== by 0x5115C7F: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==6247== by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==6247== Address 0xfff00035b is on thread #1's stack
==6247== in frame #1, created by main (???:)
==6247==
==6247== ----------------------------------------------------------------
==6247==
==6247== Possible data race during write of size 4 at 0xFFF00035C by thread #1
==6247== Locks held: none
==6247== at 0x400FAE: main (in /home/arman/a.out)
==6247==
==6247== This conflicts with a previous write of size 4 by thread #2
==6247== Locks held: none
==6247== at 0x400F35: main::{lambda()#1}::operator()() const (in /home/arman/a.out)
==6247== by 0x40204D: void std::_Bind_simple<main::{lambda()#1}()>::_M_invoke<>(std::_Index_tuple<>) (in /home/arman/a.out)
==6247== by 0x401FA3: std::_Bind_simple<main::{lambda()#1}()>::operator()() (in /home/arman/a.out)
==6247== by 0x401F33: std::thread::_Impl<std::_Bind_simple<main::{lambda()#1}()> >::_M_run() (in /home/arman/a.out)
==6247== by 0x5115C7F: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==6247== by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==6247== by 0x4E476F9: start_thread (pthread_create.c:333)
==6247== by 0x56FBB5C: clone (clone.S:109)
==6247== Address 0xfff00035c is on thread #1's stack
==6247== in frame #0, created by main (???:)
==6247==
3==6247==
==6247== For counts of detected and suppressed errors, rerun with: -v
==6247== Use --history-level=approx or =none to gain increased speed, at
==6247== the cost of reduced accuracy of conflicting-access information
==6247== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Helgrind एक दौड़ शर्त के रूप में मेरी जबकि पाश उठा किया जा रहा है लेकिन, जब मैं निम्न आदेश का उपयोग कर helgrind चलाने मैं त्रुटियों मिलता है। झूठी सकारात्मकताओं को फेंकने वाले हेल्ग्रिंड से बचने के लिए मुझे इस कार्यक्रम को कैसे बनाया जाना चाहिए?
व्यस्त लूप में पैदा करना सिंक्रनाइज़ेशन का एक खराब रूप है, इसके बजाय 'condition_variable' का उपयोग करने पर विचार करें। –
@ जोनाथन वाकई कैसे? मैंने [std :: condition_variable cppreference] के लिए उदाहरण कोड संकलित करने का प्रयास किया (http://en.cppreference.com/w/cpp/thread/condition_variable) लेकिन परिणामी प्रोग्राम एक संदिग्ध देता है: संबंधित लॉक किसी भी द्वारा नहीं आयोजित किया जाता है थ्रेड 'त्रुटि जब हेल्ग्रिंड के तहत चलाते हैं। – arman
इसे सही तरीके से उपयोग करके :-) –