110 likes | 247 Views
CS 480/680 – Comparative Languages. Threads & Processes. Threads. A thread , sometimes called a lightweight process is a separate line of execution Separate threads run in parallel On multiprocessor machines, or Pentiums with HT, threads can be truly parallel
E N D
CS 480/680 – Comparative Languages Threads & Processes
Threads • A thread, sometimes called a lightweight process is a separate line of execution • Separate threads run in parallel • On multiprocessor machines, or Pentiums with HT, threads can be truly parallel • On other machines they are timesliced • Threads are useful for I/O, reading and writing from the network, and other “slow” events Threads & Processes
Ruby Threads • In Ruby, a thread is an object (surprise!) created via Thread.new • Thread.new accepts an associated block, which is the code for the thread to execute • Local variables in the block are unique to each thread • Local, instance, and global variables in existence before the block are shared by all threads • When the calling program exits, all running threads are killed Threads & Processes
Examples • See threads.rb require 'net/http' pages = %w( www.rubycentral.com www.awl.com www.pragmaticprogrammer.com ) threads = [] for page in pages threads << Thread.new(page) { |myPage| h = Net::HTTP.new(myPage, 80) puts "Fetching: #{myPage}" resp, data = h.get('/', nil ) puts "Got #{myPage}: #{resp.message}" } end threads.each { |aThread| aThread.join } Threads & Processes
Manipulating Threads • Thread.current – returns a Thread object for the currently executing thread • Thread.list – returns an array of Thread objects for all currently executing threads • aThread.status and aThread.alive? – return the status of a thread object • Thread.stop – stop the current thread • aThread.run – arrange for a thread to be run • Thread.pass – deschedules the current thread Threads & Processes
Threads and Exceptions • The thread class maintains an abort_on_exception flag • If false, an unhandled exception only kills the current thread (default) • If true, all running threads are killed • Thread.abort_on_exception = true Threads & Processes
Thread Variables • Shared Data: All variables in scope when the thread is created are available to all threads • Local Data: All variables created in the thread block are local • How can values be “returned” from a thread to the outside world? • The thread object exists after thread execution ends: • It can be treated as a hash for local variables • Thread.current[’somedata’] = 7.2 Threads & Processes
Accessing Shared Variables • Class Mutex allows safe access to shared data • Example: Choose next item from an array and process it • Example 2: Next slide • Class ConditionVariable facilitates sharing of resources without starvation/deadlock Threads & Processes
require 'thread' mutex = Mutex.new count1 = count2 = 0 difference = 0 counter = Thread.new do loop do mutex.synchronize do count1 += 1 count2 += 1 end end end spy = Thread.new do loop do mutex.synchronize do difference += (count1 - count2).abs end end end
Processes • Spawning a new process: • system(”mediaplayer #{wavfile}”) • Runs command, output goes to standard out • result = ‘date‘ • Runs command in backquotes, output assigned to variable result • IO.popen opens a pipe: pig = IO.popen("pig", "w+") pig.puts "ice cream after they go to bed" pig.close_write puts pig.gets Threads & Processes