courses:high_performance_computing:producer_consumer
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| courses:high_performance_computing:producer_consumer [2017/03/28 08:41] – created kel | courses:high_performance_computing:producer_consumer [2025/04/27 06:51] (current) – kel | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== |
| + | > [[https:// | ||
| Ваша задача - реализовать классический паттерн producer-consumer с небольшими дополнительными условиями. | Ваша задача - реализовать классический паттерн producer-consumer с небольшими дополнительными условиями. | ||
| - | Программа должна состоять из четырех | + | Программа должна состоять из 3+N потоков: |
| - | * Задача producer-потока - получить на вход список чисел, и по очереди использовать каждое значение из этого списка для обновления переменной разделяемой между потоками. После этого | + | |
| - | * Задача consumer-потока отреагировать на каждое изменение переменной data и набирать сумму полученных значений. После того как достигнуто последнее | + | - producer |
| - | * Задача потока-interruptor проста: пока | + | - interruptor |
| + | - N потоков consumer | ||
| + | В файл, чтение которого уже | ||
| + | * Задача producer-потока - получить на вход список чисел, и по очереди использовать каждое значение из этого списка для обновления переменной разделяемой между потоками | ||
| + | * Задача consumer-потоков отреагировать на уведомление от producer и набирать сумму полученных | ||
| + | - Функция, исполняющая код этого потока | ||
| + | - После суммирования переменной | ||
| + | - Потоки consumer не должны дублировать вычисления друг с другом одних и тех же значений | ||
| + | - В качестве возвращаемого значения поток должен вернуть | ||
| + | * Задача потока-interruptor | ||
| + | * Функция run_threads должна запускать все потоки, дожидаться их выполнения, и возвращать результат общего суммирования | ||
| + | * Завершение приложения происходит или при считывании | ||
| + | * При | ||
| - | Функция run_threads должна запускать все | + | Нефункциональные требования: |
| + | - В нашем producer/ | ||
| + | - При | ||
| - | Для обновления | + | Для обеспечения |
| + | - Число потоков consumer | ||
| + | | ||
| - | Для обеспечения межпоточного взаимодействия допускается использование только pthread API. | + | В поток вывода должно попадать только результирующее значение, по умолчанию никакой отладочной или |
| + | В случае детектирования ошибок нужно выдавать | ||
| <file cpp> | <file cpp> | ||
| + | #include < | ||
| + | #include < | ||
| #include < | #include < | ||
| - | |||
| - | class Value { | ||
| - | public: | ||
| - | Value() : _value(0) { } | ||
| - | |||
| - | void update(int value) { | ||
| - | _value = value; | ||
| - | } | ||
| - | |||
| - | int get() const { | ||
| - | return _value; | ||
| - | } | ||
| - | |||
| - | private: | ||
| - | int _value; | ||
| - | }; | ||
| void* producer_routine(void* arg) { | void* producer_routine(void* arg) { | ||
| - | // Wait for consumer to start | + | // Wait for consumer to start. |
| + | // You should use this waiting only for debugging your code | ||
| + | // For the final solution please remove this waiting | ||
| // Read data, loop through each value and update the value, notify consumer, wait for consumer to process | // Read data, loop through each value and update the value, notify consumer, wait for consumer to process | ||
| + | std:: | ||
| + | // ... | ||
| } | } | ||
| void* consumer_routine(void* arg) { | void* consumer_routine(void* arg) { | ||
| // notify about start | // notify about start | ||
| - | // allocate value for result | + | // you should use this notification only for debugging your code |
| + | // for the final solution please remove this notification | ||
| + | |||
| + | | ||
| // for every update issued by producer, read the value and add to sum | // for every update issued by producer, read the value and add to sum | ||
| - | // return pointer to result | + | // return pointer to result |
| } | } | ||
| void* consumer_interruptor_routine(void* arg) { | void* consumer_interruptor_routine(void* arg) { | ||
| - | // wait for consumer | + | // wait for consumers |
| + | // you should use this waiting only for debugging your code | ||
| + | // for the final solution please remove this waiting | ||
| - | // interrupt consumer while producer is running | + | // interrupt |
| } | } | ||
| int run_threads() { | int run_threads() { | ||
| - | // start 3 threads and wait until they' | + | // start N threads and wait until they' |
| - | // return sum of update | + | // return |
| return 0; | return 0; | ||
courses/high_performance_computing/producer_consumer.1490679688.txt.gz · Last modified: 2017/03/28 08:41 by kel