Describe a time you implemented multithreading using a producer-consumer pattern and handled synchronization correctly.
Implement the pattern as producer_consumer(tasks, worker_count, buffer_size). A producer must place integer tasks into a bounded buffer, workers must consume them concurrently and return each task squared, and synchronization must prevent lost, duplicated, or reordered results. Return results in the same order as the input, including for empty input and when the buffer capacity is one.
Signature: def producer_consumer(tasks, worker_count, buffer_size):
def producer_consumer(tasks, worker_count, buffer_size):