VPython Multithreading: Difference between revisions
Line 78: | Line 78: | ||
==Connectedness== | ==Connectedness== | ||
#How is this topic connected to something that you are interested in? | #How is this topic connected to something that you are interested in? | ||
::With coding, I know first hand that better performance is often needed--especially when running complex simulations. I thought it would be fun to demonstrate multithreading in relation to VPython. | |||
#How is it connected to your major? | #How is it connected to your major? | ||
As a CS major, I spend a lot of my time coding. I run into situations that need multithreading quite often. | |||
#Is there an interesting industrial application? | #Is there an interesting industrial application? | ||
Yes! Almost every commercial application needs multithreading! It's a core requirement to when working on applications that are nontrivial! | |||
==History== | ==History== |
Revision as of 15:40, 4 December 2015
Claimed by Philip Bale
Here we discuss multithreading as it relates to advanced VPython physics computations
Overview
Multithreading is the ability to execute multiple processes (or threads) concurrently. Instead of a single execution flow, multiple program paths may be followed at once. With multithreading, programs may no longer be executed strictly in a linear manner. The notion of multithreaded parallelism in computing is similar to multitasking in real life--the idea is to get more done at once.
Multithreading is very important to VPython. Imagine a Solar System model. Though it would be possible to model and update the entire system in a linear fashion, when it comes to movement and animation, the computation can get incredibly intense. Since your computer shares it's process cycles with various other programs, you may notice significant lag while trying to achieve your task. Multithreading, however, would allow you to model and update each Planet (or even subsystems of a planet, etc.) concurrently. For a system of thousands of relevant objects, the speed improvement could be many orders of magnitude with multithreading.
As an analogy, think of multithreading as a single employee vs. an assembly line. A car could simply be worked on by a singular person. It may also, however, be worked on by multiple people performing their own tasks at the same time. The idea of multiple tasks being executed at the same time is the essence of multithreading.
Advantages
Multithreading allows advanced and complex computations to be performed more efficiently due to concurrency. The asynchronous potential of multithreaded programs supports smoother graphical renderings and more accurate user input as well. While one thread is performing mathematical computations, another could be rendering models on screen, while another is taking user input. The modularity of threading allows users to break down processes as they see fit. Furthermore, various threads may share global objects. This allows for mutual cooperation and sharing when necessary. Threads are also light-weight and cheaper than processes. Since threads can be interrupted and put on hold, they also afford a good amount of flexibility.
Disadvantages
With multithreading comes risks and disadvantage--though not to be thought as zero-sum. There are cases when breaking up a single process into multiple threads is more costly due to memory and resource allocations. From a programming standpoint, multithreaded applications are also more complex in terms of time and logic. Thread scheduling can also cause program locks if not executed correctly.
Implementation
Threading Library
- threading.currentThread()
- Returns the count of threads under the control of the caller
- threading.activeCount()
- Returns the count of total active threads
- threading.enumerate()
- Returns a list of all active threads as Thread objects
Thread instance methods
- run()
- Method for the starting point of a thread
- start()
- Calls the run method of a thread
- getName()
- Returns the name of a thread
- setName()
- Sets the name of a thread
- isAlive()
- Boolean value indicating whether the thread is still running or not
- join()
- Function that waits for other threads to terminate before continuing
Thread Creation
Creating a thread is a 3 step process.
- Create a Thread object with a target
- Cache your thread for maintenance
- Start your thread
- import threading
- def ourTarget():
- print 'Thread Started'
- return
- threads = []
- t = threading.thread(target=ourTarget)
- threads.append(t)
- t.start()
Thread Synchronization
Examples
Be sure to show all steps in your solution and include diagrams whenever possible
Simple
Middling
Difficult
A Computational Model
How do we visualize or predict using this topic. Consider embedding some vpython code here Teach hands-on with GlowScript
Connectedness
- How is this topic connected to something that you are interested in?
- With coding, I know first hand that better performance is often needed--especially when running complex simulations. I thought it would be fun to demonstrate multithreading in relation to VPython.
- How is it connected to your major?
As a CS major, I spend a lot of my time coding. I run into situations that need multithreading quite often.
- Is there an interesting industrial application?
Yes! Almost every commercial application needs multithreading! It's a core requirement to when working on applications that are nontrivial!
History
Multithreading first appeared in 1967 in OS/360 Multiprogramming with a Variable Number of Tasks or MVT. This time-sliced system allowed "tasks" to be run simultaneously. Timesharing at this point referred to multiple users sharing a single machine at different times. This MVT, however, allowed for a new definition of timesharing applications.[1]
See also
Are there related topics or categories in this wiki resource for the curious reader to explore? How does this topic fit into that context?
External links
References
This section contains the the references you used while writing this page