POC Concurrency & Multithreading 1 — Questions and Answers
Question 1: What is the Global Interpreter Lock (GIL) in Python?
- A mutex that prevents multiple threads from executing Python bytecodes simultaneously (Correct answer)
- A global variable that tracks the state of all active threads
- A lock mechanism that automatically prevents deadlocks
- A garbage collection barrier used to free thread-local storage
Correct answer: A mutex that prevents multiple threads from executing Python bytecodes simultaneously
The GIL is a mutex in CPython that allows only one thread to execute Python bytecode at a time, even on multi-core processors.
Question 2: Which Python module provides the Thread class for creating and managing threads?
- multiprocessing
- threading (Correct answer)
- concurrent
- asyncio
Correct answer: threading
The `threading` module provides the Thread class along with synchronization primitives like Lock, Event, and Semaphore.
Question 3: What is the key difference between threading and multiprocessing in Python?
- Threading uses separate memory spaces; multiprocessing shares memory
- Threading shares memory space; multiprocessing uses separate memory spaces per process (Correct answer)
- Both threading and multiprocessing use the same shared memory space
- Both threading and multiprocessing use fully isolated memory spaces
Correct answer: Threading shares memory space; multiprocessing uses separate memory spaces per process
Threads within a process share the same memory space, while multiprocessing spawns separate processes each with their own memory, bypassing the GIL for true parallelism.
Question 4: Which method must be called to actually start executing a thread after creating a Thread object?
- thread.run()
- thread.execute()
- thread.start() (Correct answer)
- thread.begin()
Correct answer: thread.start()
thread.start() initializes the thread and calls run() internally; calling run() directly executes it in the current thread instead of a new one.
Question 5: What does calling thread.join() accomplish in a Python program?
- Merges two threads into a single execution flow
- Blocks the calling thread until the joined thread finishes execution (Correct answer)
- Immediately terminates the target thread
- Restarts the thread from the beginning
Correct answer: Blocks the calling thread until the joined thread finishes execution
join() causes the calling thread (usually the main thread) to wait until the target thread completes, ensuring orderly execution.
Question 6: What Python keyword is used to define an asynchronous function (coroutine)?
- async (Correct answer)
- await
- coroutine
- asynchronous
Correct answer: async
The `async def` syntax defines a coroutine function; `await` is used inside it to suspend execution until an awaitable completes.
Question 7: What is a race condition in the context of multithreaded Python programs?
- A performance issue caused by threads competing for CPU time
- A bug where program output depends on the unpredictable timing and ordering of thread execution (Correct answer)
- A memory error caused by creating too many threads simultaneously
- A deadlock scenario where threads block each other indefinitely
Correct answer: A bug where program output depends on the unpredictable timing and ordering of thread execution
A race condition occurs when multiple threads access shared data concurrently and the result depends on the non-deterministic order in which they execute.
What is the Global Interpreter Lock (GIL) in Python?