Pregunta

Estoy trabajando a través de un ejemplo de protección de un doble doble usando mutexes, sin embargo, obtengo el error -

excepción no controlada en 0x77b6308e en Lab7.exe: 0xc0000005: Violación de acceso Escribir Ubicación 0x00000068.

¿Supongo que esto está relacionado con el punto de acceso? (El doble global)

#include <windows.h>
#include <iostream>   
#include <process.h>

double score = 0.0; 


HANDLE threads[10];     

CRITICAL_SECTION score_mutex; 


unsigned int __stdcall MyThread(void *data)
{
    EnterCriticalSection(&score_mutex);
    score = score + 1.0; 
    LeaveCriticalSection(&score_mutex); 

    return 0;
}

int main()
{
    InitializeCriticalSection(&score_mutex); 

    for (int loop = 0; loop < 10; loop++)
    {

        threads[loop] = (HANDLE) _beginthreadex(NULL, 0, MyThread, NULL, 0, NULL); 
    }

    WaitForMultipleObjects(10, threads, 0, INFINITE); 

    DeleteCriticalSection(&score_mutex); 

    std::cout << score; 

    while(true);

}

Actualizar:

Después de fijar el problema con el bucle establecido en 1000 en lugar de 10, el error aún ocurrió, sin embargo, cuando comenté las piezas de código que se refieren al mutex, el error no ocurrió.

CRITICAL_SECTION score_mutex; 
EnterCriticalSection(&score_mutex); 
LeaveCriticalSection(&score_mutex); 
InitializeCriticalSection(&score_mutex); 
DeleteCriticalSection(&score_mutex); 

actualización 2

Los hilos retornan 0 según la convención (¡ha sido una semana larga!)

Intenté agregar de nuevo en el código relacionado con Mutex, y el programa compilará y funcionará bien (aparte de las cuestiones de condición de la carrera con el doble por supuesto) con critics_section, la selección inicializecritical y la selección de eliminatica, se agregó. El problema parece Estar con la selección de entercriticalsection o leavecriticalsection, ya que el error se reembolsa cuando los agrego.

¿Fue útil?

Solución

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().

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top