1 / 16

Efficient Thread Synchronization Techniques in Programming

Learn about different thread synchronization techniques, mutex, conditions, semaphores, barriers, and common mistakes like race conditions and deadlocks. Discover practical solutions for synchronizing threads effectively.

Download Presentation

Efficient Thread Synchronization Techniques in Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Synchronisatiefouten

  2. Rendez-vous Draad 1 Draad 2 Draad 1 Draad 2 release Rendez-vous acquire

  3. Rendez-vous Semafoor s1; Semafoor s2; Draad1: { // Doe werk s2.release(); s1.acquire(); // Doe werk } Draad2: { // Doe werk s1.release(); s2.acquire(); // Doe werk } Draad 1 Draad 2 release release s1 s2 acquire acquire

  4. Rendez-vous Draad 1 Draad 2 Draad 3 release release release acquire acquire acquire

  5. Semafoor mutex = 1; Conditionbarrier; int count = 0; draad: { // doe werk mutex.acquire(); count++; mutex.release(); if (count < n) barrier.wait(); else barrier.signal(); // doe werk } Draad 1 Draad 2 Draad n … Barrier

  6. Semafoor mutex = 1; Conditionbarrier; int count = 0; draad: { // doe werk mutex.acquire(); count++; if (count < n) barrier.wait(); else barrier.signal(); mutex.release(); // doe werk } Draad 1 Draad 2 Draad n … Barrier

  7. Semafoor mutex = 1; Conditionbarrier; int count = 0; draad: { // doe werk mutex.acquire(); count++; if (count < n) barrier.wait(mutex); else barrier.signal(); mutex.release(); // doe werk } Draad 1 Draad 2 Draad n … Barrier

  8. Semafoor mutex = 1; Conditionbarrier; int count = 0; draad: { // doe werk mutex.acquire(); count++; if (count < n) { barrier.wait(mutex); barrier.signal(); } else barrier.signal(); mutex.release(); // doe werk } Draad 1 Draad 2 Draad n Barrier

  9. Semafoor mutex = 1; Conditionbarrier; int count = 0; draad: { // doe werk mutex.acquire(); count++; if (count < n) { barrier.wait(mutex); barrier.signal(); } else { barrier.signal(); count = 0; } mutex.release(); // doe werk Draad 1 Draad 2 Draad n mutex.acquire(); count++; if (count < n) { barrier.wait(mutex); barrier.signal(); } else { barrier.signal(); count = 0; } mutex.release(); // doe werk } Barrier

  10. Semafoor mutex = 1; Conditionbarrier; int count = 0; draad: { // doe werk mutex.acquire(); count++; if (count < n) { barrier.wait(mutex); } else { barrier.signalall(); count = 0; } mutex.release(); // doe werk Draad 1 Draad 2 Draad n mutex.acquire(); count++; if (count < n) { barrier.wait(mutex); } else { barrier.signalall(); count = 0; } mutex.release(); // doe werk } Barrier

  11. Semafoor mutex = 1; Semafoor filo[5] = [0,0,0,0,0]; State [hungry, eating, thinking] state[5] = [thinking, thinking, thinking, thinking, thinking]; filosoof(i): while (1) { get_forks(i); sleep(t1); // eat put_forks(i); sleep(t2); // think } get_forks(i): mutex.acquire(); state[i] = hungry; test(i); mutex.release(); filo[i].acquire(); test(i): if (state[i] == hungryand state[left(i)] != eatingand state[right(i)] != eating) { state[i] = eating; filo[i].release(); } put_forks(i): mutex.acquire(); state[i] = thinking; test(right(i)); test(left(i)); mutex.release();

  12. Vaak gemaakte fouten • Data races • Deadlock • Geen lus in draad • Niet blokkeren indien vereist • Volgorde van events niet respecteren • Sequentialiseren

  13. N togen voor bestelling klanten en 1 frietkok. Lading frietjes = M porties. Toog vraagt nieuwe frietjes aan kok indien maar 2 porties meer zijn. Frietkok bezorgt frietjes in volgorde van aanvragen. De Brug mailbox bestellingen; semafoor m[N] = [1,…,1]; int porties[N] = [M,…,M]; conditionmeerfrietjes[N]; Toog(int n) { while (1) { m[n].acquire(); if (porties[n] == 0) meerfrietjes[m].wait(m[n]); porties[n]--; if (porties[n] == 2) bestellingen.send(n); m[n].release(); } } Frietkok { int n; while (1) { bestellingen.receive(&n); m[n].acquire(); porties[n] += M; m[n].release(); meerfrietjes[n].signal()l; } }

  14. Alternatieve oplossing 1 Mutexwachtrij_mutex; Cond_varniet_lege_wachtrij; dist<Integer> togen = new ArrayList<>(); Toog(int n) { lock(wachtrij_mutex); togen.add(n); broadcast(niet_lege_wachtrij, wachtrij_mutex); unlock(wachtrij_mutex); } Frietkok { lock(wachtrij_mutex); if (togen.size() == 0) { wait(niet_lege_wachtrij, wachtrij_mutex); } serveer(togen.get(0)); unlock(wachtrij_mutex); }

  15. Alternatieve oplossing 2 Monitor { int porties[N] = {M}; Queue wachtrij = 0; Condition kok, klaar[N]; Toog(int n) { if (porties[n] < 1) klaar[n].wait(); porties[n]--; if (porties[n] == 2) { wachtrij.enqueue(n); kok.signal(); } } Frietkok { if (wachtrij.is_empty()) kok.wait(); int toog = wachtrij.dequeue(); porties[n] += M; klaar[toog].signal(); } }

  16. Alternatieve oplossing 3 Mutexmutex = new Mutex(); Condition kok new Condition(); Volatile int aantalPorties = M; Toog(int n) { mutex.acquire(); if (aantalPorties > 0) { aantalPorties--; if (aantalPorties == 2) kok.signal(); } mutex.release(); } Frietkok { while (true) { while (aantalPorties > 2) kok.wait(); bakfrietjes(); mutex.acquire(); aantalPorties += M; mutex.release(); } }

More Related