none
std::threads hanging VS2015RC debugger on Win 8.1 but not Win 7. Bug in code or VS? RRS feed

  • Question

  • // ThreadTest /* std:thread hangs with VS2015RC and windows 8.1.Works on Win 7 VS2015. Exhibits same failure with Win 8.1 on a virtual and non - virtual machine This program usually hangs within a few interations. Replicated */ #include <vector> #include <thread> #include <iostream> using namespace std; void processThread(const int offset, vector<int> &results) { for (volatile int i = 0; i < 1000000; i++) // volatile to force exec time results[offset] += i; } int main() { for (int loop = 0; loop < 10; loop++) { vector<int> result(10); vector<thread> vThreads(10); vThreads.clear(); #if 1 // spin 10 threads for (int i = 0; i < 10; i++)

    // hangs here: vThreads.emplace_back(thread(processThread, i, ref(result))); for (int i = 0; i < 10; i++) vThreads[i].join(); #else // execute on main thread for (int i = 0; i < 10; i++) processThread(i, result); #endif cout << result[0] << " " << result[1] << endl; } int done; cin >> done; // 1783293664 1783293664 }


    Intermittently hangs during debug. Especially with memory-cpu performance active. Both debug and release.
    Thursday, June 4, 2015 1:24 AM

Answers

All replies

  • https://msdn.microsoft.com/en-us/library/c9ceah3b(v=vs.140).aspx

    "If an object is being written to by one thread, then all reads and writes to that object on the same or other threads must be protected. For example, given an object A, if thread 1 is writing to A, then thread 2 must be prevented from reading from or writing to A."

    You're reading and writing to vector<int> results from up to 10 threads simultaneously.

    Thursday, June 4, 2015 1:58 AM
  • https://msdn.microsoft.com/en-us/library/c9ceah3b(v=vs.140).aspx

    "If an object is being written to by one thread, then all reads and writes to that object on the same or other threads must be protected. For example, given an object A, if thread 1 is writing to A, then thread 2 must be prevented from reading from or writing to A."

    You're reading and writing to vector<int> results from up to 10 threads simultaneously.

    While the vector<int> object is being read from by multiple threads, each entry, an int, in the vector is only being manipulated by a single thread. Nothing in the threads is manipulating the vector object, only the addressed int and only one thread manipulates any given int. I believe this is legal code.

    Specifically, Section 23.2.2 Specifically allows different objects contained in a vector (except for type bool) to be written concurrently. For instance a[1]=3; and a[2]=5; can run concurrently. a[0] =3; and *a.begin()=6 cannot.

    • Edited by vcangel Thursday, June 4, 2015 3:07 AM
    Thursday, June 4, 2015 2:28 AM
  • I don't believe the problem is related to anything in vector<int>. There is no indication that it is not conformant with C++11.

    I also ran the code by replacing:

       vector<int> result;

    with

       int result[10];

    and changing the necessary declarations and function call parameters. This is obviously thread safe since each integer in the array is only read or written to by one thread.  The problem with VS2013RC when running the debugger in Win 8.1 remains.

    Thursday, June 4, 2015 4:50 AM
  • Looks like there exist some issues, debugging multi-threaded under Win 8.1
    C++ Program hangs when launched inside the debugger
    https://connect.microsoft.com/VisualStudio/feedback/details/1327211/c-program-hangs-when-launched-inside-the-debugger

    With kind regards

    • Marked as answer by vcangel Friday, June 5, 2015 4:16 AM
    Thursday, June 4, 2015 8:20 AM
  • Looks like there exist some issues, debugging multi-threaded under Win 8.1
    C++ Program hangs when launched inside the debugger
    https://connect.microsoft.com/VisualStudio/feedback/details/1327211/c-program-hangs-when-launched-inside-the-debugger

    With kind regards


    Thanks for the link. Their experience exactly corresponds to mine as it happens only on Win 8.1, both with VMs and non-VMs. Interestingly, when the program hangs one can still break in the debugger at which point the debugger shows only the main thread is running. The program can then be continued and will run until encountering the same problem again. It always hangs at the same place where it is waiting for a system resource before launching a new thread.
    Thursday, June 4, 2015 2:48 PM
  • It's clearly an interaction between the VS2015RC debugger and Windows 8.1.

    The following code also fails and it is obviously legal code.

    // ThreadTest
    
    /*
    std:thread hangs with VS2015RC and windows 8.1.Works on Win 7 VS2015.
    	Exhibits same failure with Win 8.1 on a virtual and non - virtual machine
    	Works when run outside visual studio.
    
    	This program usually hangs within a few interations.
    	Replicated
    */
    
    #include <vector>
    #include <thread>
    #include <chrono>
    #include <iostream>
    
    using namespace std;
    
    const int loops = 500000;
    
    void wakeMeUp()
    {
    	for (volatile int i = 0; i < loops; i++)
    		;
    }
    
    int main()
    {
    	for (int loop = 0; loop < 1000; loop++)
    	{
    		auto done0 = thread(wakeMeUp);
    		auto done1 = thread(wakeMeUp);
    		auto done2 = thread(wakeMeUp);
    		auto done3 = thread(wakeMeUp);
    		auto done4 = thread(wakeMeUp);
    		auto done5 = thread(wakeMeUp);
    		auto done6 = thread(wakeMeUp);
    		auto done7 = thread(wakeMeUp);
    		auto done8 = thread(wakeMeUp);
    		auto done9 = thread(wakeMeUp);
    		done0.join();
    		done1.join();
    		done2.join();
    		done3.join();
    		done4.join();
    		done5.join();
    		done6.join();
    		done7.join();
    		done8.join();
    		done9.join();
    		cout << "pass" << loop <<  endl;
    	}
    }

    Friday, June 5, 2015 3:49 AM
  • Looks like there exist some issues, debugging multi-threaded under Win 8.1
    C++ Program hangs when launched inside the debugger
    https://connect.microsoft.com/VisualStudio/feedback/details/1327211/c-program-hangs-when-launched-inside-the-debugger

    With kind regards

    Looks like the bug occurs pretty easily with the simplest code. This makes VS2015RC essentially useless for multithreaded code that requires Win 8.1.

    Anyone know if the same problem exists on Win 10?

    Friday, June 5, 2015 3:53 AM
  • "Anyone know if the same problem exists on Win 10?"

    I cannot reproduce the issue on Win10.

    Friday, June 5, 2015 4:07 AM
    Moderator
  • For anyone seeing this problem it was a bug in Win 8.1 and has been reported as resolved:

    Thank you for reporting this issue and for creating this simple repro program.  This issue is caused by an OS scheduler bug introduced in the spring 2014 update to Windows 8.1.  A hotfix for this problem was released in May 2015 and available at https://support.microsoft.com/en-us/kb/3036169.


    Thanks to the Windows peeps for getting this fixed.
    • Edited by vcangel Thursday, June 11, 2015 11:12 PM
    Thursday, June 11, 2015 11:11 PM