حالات التسابق
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
spot a race and a check-then-act bug.
الموضوعات المخطط لها
- A race condition: the result depends on thread timing.
count++is read, add, write: three steps, so updates can be lost.- Check-then-act:
if (stock > 0) stock--;can oversell when two threads pass the check. - Races are intermittent; passing tests do not prove safety.
- Shared mutable state is the root; remove sharing or mutation, or guard it.
تفاصيل المخطط المبدئي
عرض عملي
if (stock >= qty) { // thread A and B both pass
stock -= qty; // both subtract: oversell
}اختبر فهمك
Why is count++ unsafe?
أظهر الإجابة
It is not atomic: read, modify, write can interleave.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.