1 / 10

Threads & Processes

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

sally
Télécharger la présentation

Threads & Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 480/680 – Comparative Languages Threads & Processes

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

More Related