Ruby thread Knowledge Point Analysis

Source: Internet
Author: User

Ruby is a fully object-oriented interpreted scripting language. For such a new programming language, its features are very attractive to programmers. Let's take a look at the concepts related to Ruby threads.

  • How to call win32ole IN Ruby
  • Ruby basic code Experience Sharing
  • Summary of several common Ruby core concepts
  • Ruby experience in configuring Mysql DBI
  • Easily implement Ruby to create XML

Today I read the Ruby thread section. The thread and Process Section of the first version of "Programming Ruby" in HTML is detailed. After reading it, it seems like I have retried this part of the operating system. Especially in the Spawning New Processes section, I really don't know what he said if I haven't learned the operating system.

IO. popen, where the popen should be "piped open. The Ruby thread code,

  1. Pipe = IO. popen ("-", "w + ")
  2. If pipe
  3. Pipe. puts "Get a job! "
  4. $ Stderr. puts "Child says
    '# {Pipe. gets. chomp }'"
  5. Else
  6. $ Stderr. puts "Dad says
    '# {Gets. chomp }'"
  7. Puts "OK"
  8. End

Similar to the fork code example in Unix, the Parent and Child processes share the same piece of code. Programming Ruby explains this code as "There's one more twist to popen. if the command you pass it is a single minus sign (''--''), popen will fork a new Ruby interpreter. both this and the original interpreter will continue running by returning from the popen. the original process will receive an IO object back, while the child will receive nil. ".

For the first time, I didn't see what he was talking about. After reading the code, I did not think about fork for a while. It took 10 minutes for Ling Guang to know what was going on. Reading English is not easy, comrades!

The Ruby thread is quite easy to learn. The functions of Ruby threads are self-implemented. It has nothing to do with the operating system. In order to achieve platform independence, I think this sacrifice is a little big. Not to mention how much effort the author has to spend on development. It does not have the advantages of local threads. For example, thread hunger. Below I have written an example of a producer-consumer of the exercise nature. To be honest, it is much longer than the example in thread. rb in Ruby ...... The advantage is that this solves the issue of redirection during screen output.

 
 
  1. require 'thread'  
  2. class Consumer  
  3. def initialize(queue, 
    stdout_mutex)  
  4. @queuequeue = queue  
  5. @stdout_mutexstdout_mutex 
    = stdout_mutex  
  6. end  
  7. def consume  
  8. product = @queue.pop  
  9. @stdout_mutex.synchronize {  
  10. puts "Product #{product} 
    consumed."  
  11. $stdout.flush  
  12. }  
  13. end  
  14. end  
  15. class Producer  
  16. def initialize(queue, stdout_mutex)  
  17. @queuequeue = queue  
  18. end  
  19. def produce  
  20. product = rand(10)  
  21. @queue.push(product)  
  22. @stdout_mutex.synchronize {  
  23. puts "Product #{product} produced."  
  24. $stdout.flush  
  25. }  
  26. end  
  27. end  
  28. sized_queue = SizedQueue.new(10)  
  29. stdout_mutex = Mutex.new  
  30. consumer_threads = []  
  31. 100.times {  
  32. consumer_threads << Thread.new {  
  33. consumer = Consumer.new(sized_
    queue, stdout_mutex)  
  34. consumer.consume  
  35. }  
  36. Thread.new {  
  37. producer = Producer.new(sized_
    queue, stdout_mutex)  
  38. producer.produce  
  39. }  
  40. }  
  41. consumer_threads.each { 
    |thread| thread.join } 

The above is a detailed description of the concepts related to Ruby threads, and I hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.