10
Threads & Processes Threads & Processes 480/680 – Comparative Languages 480/680 – Comparative Languages

Threads & Processes CS 480/680 – Comparative Languages

Embed Size (px)

Citation preview

Page 1: Threads & Processes CS 480/680 – Comparative Languages

Threads & ProcessesThreads & Processes

CS 480/680 – Comparative LanguagesCS 480/680 – Comparative Languages

Page 2: Threads & Processes CS 480/680 – Comparative Languages

Threads & Processes 2

ThreadsThreads 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

Page 3: Threads & Processes CS 480/680 – Comparative Languages

Threads & Processes 3

Ruby ThreadsRuby 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

Page 4: Threads & Processes CS 480/680 – Comparative Languages

Threads & Processes 4

ExamplesExamples 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}" }endthreads.each { |aThread| aThread.join }

Page 5: Threads & Processes CS 480/680 – Comparative Languages

Threads & Processes 5

Manipulating ThreadsManipulating 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

Page 6: Threads & Processes CS 480/680 – Comparative Languages

Threads & Processes 6

Threads and ExceptionsThreads 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

Page 7: Threads & Processes CS 480/680 – Comparative Languages

Threads & Processes 7

Thread VariablesThread 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

Page 8: Threads & Processes CS 480/680 – Comparative Languages

Threads & Processes 8

Accessing Shared VariablesAccessing 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

Page 9: Threads & Processes CS 480/680 – Comparative Languages

require 'thread'mutex = Mutex.new

count1 = count2 = 0difference = 0counter = Thread.new do loop do mutex.synchronize do count1 += 1 count2 += 1 end endendspy = Thread.new do loop do mutex.synchronize do difference += (count1 - count2).abs end endend

Page 10: Threads & Processes CS 480/680 – Comparative Languages

Threads & Processes 10

ProcessesProcesses 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_writeputs pig.gets