Homework - dining philosophers problem with visualization#50
Homework - dining philosophers problem with visualization#50lukasz-plewa wants to merge 1 commit intocoders-school:mainfrom
Conversation
Modify number of philosophers in main() as parameter to Table object Modify number of dishes to be eaten by every philosopher in Philosopher class declaration.
|
Let me check this later in next week. |
ziobron
left a comment
There was a problem hiding this comment.
Udało mi się złapać wyścig:
https://pastebin.pl/view/300df1dc
Niestety tylko raz się pojawił w trybie release. Potem ani w debug ani realease się nie pojawił, więc nie mam więcej info.
Podoba mi się rozwiązanie. Można jeszcze trochę zoptymalizować wyciągając niektóre rzeczy z sekcji krytycznych. Może nawet niektóre rzeczy można bez blokowania robić.
| #include "philosopher.h" | ||
|
|
||
| class Fork { | ||
| int id_; |
There was a problem hiding this comment.
U mnie był błąd:
dining_table.h:19:9: error: private field 'id_' is not used
[-Werror,-Wunused-private-field]
int id_;
| private: | ||
| std::mt19937 m_generator; | ||
| std::uniform_int_distribution<int> m_distribution; | ||
| static EatTimeEngine *pinstance_; |
There was a problem hiding this comment.
polecam implementować singletony Meyersa - https://stackoverflow.com/questions/17712001/how-is-meyers-implementation-of-a-singleton-actually-a-singleton
Najprostsze i bezpieczne bez dodatkowych mechanizmów.
|
|
||
| void Table::joinPhilosopherToTheFeast(std::shared_ptr<Philosopher> p) { | ||
| std::thread t = std::thread(&Philosopher::threadStartTheFeast, &(*p)); | ||
| p->setThread(t); |
There was a problem hiding this comment.
| p->setThread(t); | |
| p->setThread(std::move(t)); |
| PhilStatus getStatus(); | ||
| void threadStartTheFeast(); | ||
|
|
||
| void setThread(std::thread &t) { |
There was a problem hiding this comment.
| void setThread(std::thread &t) { | |
| void setThread(std::thread &&t) { |
|
|
||
| int getId() { | ||
| std::lock_guard<std::mutex> lock(mtx_); | ||
| return id_; |
There was a problem hiding this comment.
gdyby id_ było const to do odczytania danych nie trzeba by było blokować mutexa
| void endThread() { | ||
| std::lock_guard<std::mutex> lock(mtx_); | ||
| if (m_t.joinable()) { | ||
| m_t.join(); | ||
| } | ||
| } |
| class Table { | ||
| int seats_; | ||
| int wait_for; | ||
| std::shared_ptr<Table> self_; |
| drawHeader(); | ||
| do { | ||
| std::unique_lock<std::mutex> mlock(m_update_mtx); | ||
| m_cond_update.wait(mlock); |
There was a problem hiding this comment.
dobry pomysł z synchronizacją wyświetlania za pomocą condition_variable :)
Dzięki Łukaszu za tak wnikliwe review. Kulawy i niedojrzały ten mój kod. Postaram się gu ulepszyć wg Twoich wskazówek no i poszukam źródła tego wyścigu. |
Modify number of philosophers in main() as parameter to Table object
Modify number of dishes to be eaten by every philosopher in Philosopher
class declaration.