Skip to content

ver. 1.0

Pre-release
Pre-release

Choose a tag to compare

@KBruy KBruy released this 02 Jan 18:26
· 2 commits to main since this release
1642dbc

Problem ucztujących filozofów rozwiązuję podejściem współbieżnym: każdy filozof działa w osobnym wątku, a wspólny stan (aktualny stan filozofa i liczba jego posiłków) trzymany jest w jednej współdzielonej strukturze, chronionej mutexem. Filozofowie tylko aktualizują ten stan, a osobny wątek UI okresowo robi krótki „snapshot” danych pod mutexem (kopiuje wektory do lokalnych zmiennych) i na tej podstawie rysuje widok w ncurses. Dzięki temu synchronizacja jest prosta (jeden mutex na wspólny stan), unika się wyścigów danych, a blokady są trzymane bardzo krótko, więc filozofowie i UI nie blokują się nawzajem na dłużej niż potrzeba.

Jednak występuje tu problem wydajności. Z racji na FIFO tylko jeden filozof może jeść w danej chwili. Inni są zatrzymani, czekają na odłożenie widelców i na swoją kolej.


I solve the problem of feasting philosophers with a concurrent approach: each philosopher runs in a separate thread, and the shared state (the philosopher's current state and the number of meals) is kept in a single shared structure, protected by a mutex. The philosophers only update this state, while a separate UI thread periodically takes a short "snapshot" of the data under the mutex (copying vectors to local variables) and draws the view in ncurses based on that. This makes synchronization simple (one mutex per shared state), avoids data races, and locks are held very briefly, so the philosophers and UI don't block each other for longer than necessary.

However, there is a performance problem. Due to the FIFO, only one philosopher can eat at a time. The others are held, waiting for their forks to be put away and for their turn.