Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: The Limited Drop Lockdown | Multithreading vs. Multiprocessing
Python Structural Programming
Секція 3. Розділ 7
single

single

Challenge: The Limited Drop Lockdown

Свайпніть щоб показати меню

Завдання

Проведіть, щоб почати кодувати

A famous brand is releasing a limited edition hoodie with only 10 available. There are 50 threads simulating customers trying to buy the hoodie at the same time. Your task is to ensure that exactly 10 hoodies are sold and no more, using a lock to prevent race conditions.

  • Use the provided lock object to synchronize access to the shared variable hoodies_in_stock.
  • In the buy_hoodie function, acquire the lock before checking and updating the stock.
  • Simulate payment processing by adding a tiny delay (e.g., time.sleep(0.01)) while holding the lock.
  • If a hoodie is available, decrement hoodies_in_stock and add the customer_id to the dropped_customers list.
  • After all threads finish, exactly 10 customer IDs should be in dropped_customers, and hoodies_in_stock should be 0.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 7
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

some-alt