1. Find definitions for eight terms and concepts used in threaded programming:
Thread Synchronisation: Thread synchronisation keeps threads in order and working together coherently so as to accomplish a certain set of tasks which regards to acting and shielding against multithreading issues such as corrupted data, file locks, and deadlocks.
Locks: A lock is a sychronisation mechanism for enforcing limits on access to a resource in an environment where many threads or processes are executing (Wikipedia, 2010). An example is a thread accessing a file to write to. First the thread must obtain a lock before it can begin writing, once it has finished writing it can release the lock to another thread. This stops concurrent issues from occuring such as two threads trying to write to the same file at the same time.
Deadlock: A deadlock is a situation whereby two competing processes or threads are waiting for the other to finish and thus are stuck in a situation whereby they will be waiting forever.
Semaphores: A semaphore is a protected variable (or abstract data type) and constitutes the classic method for restricting access to shared resources in a multi-processing environment (Economic Expert, n.d.). Semaphore will generally takes one of two forms: binary or counting. A binary semaphore is a simple true/false flag that controls access to a single resource, and a counting semaphore is a counter for a set of available resources (Wikipedia, 2010).
Mutex (mutual exclusion): Mutex which is short for mutual exclusion object. In programming, a mutex is a program object that allows multiple program threads to share the same resource (i.e. file access) but not simultaneously. It is used for concurrent programming when accessing common resources simultaneously (Wikipedia, 2010).
Thread: In our topic guide is states that "a thread is a flow of control through the process". A process can have many threads which can be initiated by an event the process to do something concurrently. Threads are really just pieces of code or logic you want to run concurrently to accomplish something for the process.
Event: Wikipedia (2010) states that a event in computing is an action that is usually initiated outside the scope of a program and that is handled by a piece of code inside the program. Typically events are handled synchronous with the program flow, that is, the program has one or more dedicated places where events are handled. Typical sources of events include the user who presses the keyboard or hardware devices such as a timer.
So an example of this might be a user in the application clicks a button "Submit". An event of onclick is fired within the program flow to execute the "Submit" function within the code.
Waitable Timer: A waitable timer object is a sychronisation object whose state is set to signaled when the specified due time arrives. There are two types of waitable timers that can be created: manual-reset and sychronisation which both can be used as a periodic timer (Microsoft, 2010)
2. A simple demonstration of the threading module in Python (threaddemo.py) tat uses both a lock and semaphore to control concurrency is by Ted Herman at the University of Iowa. The code and the sample output below are worth a look. Report your findings.
The threaddemo.py script which was provided gives us a chance to see how threading can be utilised. The script itself declares at first the total number of tasks that can be run at 10. But there is a limitation semaphore of 3 tasks than can only be run concurrently. There is also the use of a mutex object whereby only 1 of the 3 tasks can only update a certain running variable at a time, and if not waits for the other operation to finish.
No comments:
Post a Comment