Seksjon 3. Kapittel 7
single
Challenge: The Limited Drop Lockdown
Sveip for å vise menyen
Oppgave
Sveip for å begynne å kode
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
lockobject to synchronize access to the shared variablehoodies_in_stock. - In the
buy_hoodiefunction, 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_stockand add thecustomer_idto thedropped_customerslist. - After all threads finish, exactly 10 customer IDs should be in
dropped_customers, andhoodies_in_stockshould be 0.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 3. Kapittel 7
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår