C++

std::mutex & lock_guard

Në shqip përdoret i njëjti emër.

ShpjegimiSQ

Një mutex siguron që vetëm një thread të hyjë në një pjesë kodi në të njëjtën kohë. lock_guard e kyç mutex-in dhe e zhkyç vetë kur del nga blloku (RAII). Kujdes nga deadlock-u: dy thread-e që presin njëri-tjetrin përgjithmonë.

EnglishEN

A mutex makes sure only one thread enters a section of code at a time. lock_guard locks the mutex and unlocks it automatically when leaving the block (RAII). Beware of deadlock: two threads waiting for each other forever.

Si ta mendosh

Si çelësi i banjës në një kafene: vetëm një person hyn, dhe e lë çelësin kur del. Deadlock është kur dy persona presin njëri-tjetrin te dy dyer të ndryshme.

Lexoje në anglisht

Like the toilet key in a café: only one person goes in, and hands the key back on the way out. Deadlock is two people each waiting for the other at two different doors.

Shembull kodicpp

std::mutex m;
int numeruesi = 0;
void rrit() {
    std::lock_guard<std::mutex> kyci(m); numeruesi++;
}

Terma të lidhur