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,
- Pipe = IO. popen ("-", "w + ")
- If pipe
- Pipe. puts "Get a job! "
- $ Stderr. puts "Child says
'# {Pipe. gets. chomp }'"
- Else
- $ Stderr. puts "Dad says
'# {Gets. chomp }'"
- Puts "OK"
- 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.
- require 'thread'
- class Consumer
- def initialize(queue,
stdout_mutex)
- @queuequeue = queue
- @stdout_mutexstdout_mutex
= stdout_mutex
- end
- def consume
- product = @queue.pop
- @stdout_mutex.synchronize {
- puts "Product #{product}
consumed."
- $stdout.flush
- }
- end
- end
- class Producer
- def initialize(queue, stdout_mutex)
- @queuequeue = queue
- end
- def produce
- product = rand(10)
- @queue.push(product)
- @stdout_mutex.synchronize {
- puts "Product #{product} produced."
- $stdout.flush
- }
- end
- end
- sized_queue = SizedQueue.new(10)
- stdout_mutex = Mutex.new
- consumer_threads = []
- 100.times {
- consumer_threads << Thread.new {
- consumer = Consumer.new(sized_
queue, stdout_mutex)
- consumer.consume
- }
- Thread.new {
- producer = Producer.new(sized_
queue, stdout_mutex)
- producer.produce
- }
- }
- 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.