سؤال

أنا أعمل من خلال مثال على حماية مزدوجة عالمية باستخدام Mutexes، ولكن أحصل على الخطأ -

استثناء غير معالج في 0x77b6308E في Lab7.exe: 0xc0000005: انتهاك الوصول كتابة الموقع 0x00000068.

أفترض أن هذا يرتبط بالوصول إلى النتيجة؟ (مزدوج العالم) giveacodicetagpre.

تحديث:

بعد إصلاح المشكلة مع تعيين الحلقة التي يجري تعيينها إلى 1000 بدلا من 10، لا يزال الخطأ قد حدث، ولكن عندما علقت على القطع من التعليمات البرمجية التي تشير إلى mutex، لم يحدث الخطأ. giveacodicetagpre.

تحديث 2

عودة المواضيع 0 حسب الاتفاقية (لقد كان أسبوع طويل!)

حاولت إضافة مرة أخرى في التعليمات البرمجية المتعلقة بالمشبك، وسوف تجميع البرنامج بشكل جيد (بخلاف مشكلات شرط السباقات مع ضعف الدورة التدريبية) مع الحرجة_SITIONSICENTIONECTIONECTIONECTIONSECTIONSECTIONECTIONECTIONENTECTION يكون مع entercricicalsection أو التقييمات، حيث أن الخطأ يتكرر عندما أضيف لهم.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top