#C13313. Thread Simulation Challenge
Thread Simulation Challenge
Thread Simulation Challenge
This problem challenges you to simulate simple thread behaviors without actually using concurrent execution. You need to handle three different scenarios:
- Scenario 1: Simulate two threads that run concurrently. Each thread is assigned a delay (in seconds) and will finish exactly after that many seconds. The program should output a finish message for each thread, showing its delay.
- Scenario 2: Simulate a safe increment operation where 10 threads each increment a shared counter. The final value of the counter should be 10.
- Scenario 3: Simulate the creation and proper termination of a simple thread. The program should simply output a message indicating that the thread has finished.
Note: Although these scenarios mimic behaviors seen in threading (such as timing and race condition safety), you are not required to create actual threads. Instead, simply simulate the correct outputs as described.
Implementation Details:
- For scenario 1, use the provided delay values and output the finish messages exactly as described.
- For scenario 2, output the number 10.
- For scenario 3, output the string "Thread finished".
Input/Output Format:
The program will first read an integer indicating the scenario type (1, 2, or 3).
For scenario 1, the next line contains two numbers (delays in seconds for thread A and thread B). The output should consist of two lines:
Thread A finished after d1 seconds
Thread B finished after d2 seconds
where d1 and d2 are printed as floating-point numbers with one digit after the decimal point.
For scenario 2, no further input is provided; simply output the integer 10.
For scenario 3, no further input is provided; simply output the string "Thread finished".
Constraints:
The delay values will be positive numbers. You can assume the input format is always valid. There is no need to actually perform any waiting; just simulate the outputs.
Note: Use stdin
for input and stdout
for output.
inputFormat
The first line contains an integer T
(1 ≤ T ≤ 3) indicating the scenario type.
- If
T = 1
, the second line contains two floating point numbersd1
andd2
separated by a space, representing the delays (in seconds) for thread A and thread B respectively. - If
T = 2
orT = 3
, no additional input is provided.
outputFormat
For scenario 1, output two lines:
- First line:
Thread A finished after d1 seconds
- Second line:
Thread B finished after d2 seconds
For scenario 2, output a single line with the number 10.
For scenario 3, output a single line: Thread finished
.
Ensure that any floating point numbers are printed with one decimal place.
## sample1
2 3
Thread A finished after 2.0 seconds
Thread B finished after 3.0 seconds
</p>