Frage

Ich arbeite durch ein Beispiel, um ein globales Doppel mit Mutexe zu schützen, aber ich bekomme den Fehler -

Ausnahme bei 0x77b6308e in Lab7.exe: 0xc0000005: Zugangsverletzung Schreibstandort 0x00000068.

Ich gehe davon aus, dass dies mit dem Zugriff auf Score zusammenhängt? (Das globale Doppel) generasacodicetagpre.

update:

Nach dem Fixieren des Problems mit der Schleife, die auf 1000 anstelle von 10 auf 1000 eingestellt ist, ist der Fehler immer noch aufgetreten, aber wenn ich die Code-Teile, die sich auf den Mutex, der auf den Mutex auskommt, ereignete, trat der Fehler nicht auf. generasacodicetagpre.

update 2

Die Threads rufen nach dem Übereinkommen zurück (es ist eine lange Woche!)

Ich habe versucht, im Mutex-Code zurückzunehmen, und das Programm wird mit Critical_Section, InitialiCercriticalsection und DeletecriticalSection, die alle hinzugefügt, kompiliert und ausführen. Seien Sie mit der Entercriticalsection oder in LeaveCriticalsection, da der Fehler wieder auftritt, wenn ich sie hinzufüge.

War es hilfreich?

Lösung

The remaining bug in your code is in the call to WaitForMultipleObjects(). You set the 3rd parameter to 0 (FALSE) such that the main thread unblocks as soon as any of the 10 threads finishes.

This causes the call to DeleteCriticalSection() to execute before all threads are finished, creating an access violation when one of the (possibly) 9 other threads starts and calls EnterCriticalSection().

Andere Tipps

You're writing beyond the end of your threads[10] array:

for (int loop = 0; loop < 1000; loop++){
     threads[loop];
}

threads only has size 10!

Your problem is that WaitForMultipleObjects is not waiting for all the threads to complete, causing the critical section to be prematurely deleted. According to MSDN, the third argument is

bWaitAll [in]

If this parameter is TRUE, the function returns when the state of all objects in the >lpHandles array is signaled. If FALSE, the function returns when the state of any one of >the objects is set to signaled. In the latter case, the return value indicates the object >whose state caused the function to return.

You set this to 0, which returns when ANY ONE of your threads completes. This causes the following DeleteCriticalSection to be run while there's still threads waiting to access it.

You should also declare score as a volatile so you don't have cached value problem.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top