Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the knowledge points related to Ruby threads

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you the analysis of Ruby thread-related knowledge points. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Ruby language is a fully object-oriented interpretive scripting language. For such a new programming language, its features are very attractive to programmers.

I saw the Ruby threading section today. The thread and process section of the HTML version of "Programming Ruby" is described in great detail. After watching it, I feel as if I have relived this part of the operating system again. Especially in the Spawning New Processes section, if you haven't learned the operating system, I really don't know what he said.

IO.popen, of which popen, I understand, should mean "piped open".

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

Almost like the fork code example in Unix, the parent and child processes share the same piece of code. "Programming Ruby" interprets 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 had no idea what he was talking about. After reading the code, I didn't think about fork for a moment. As a result, it took ten minutes for a flash of inspiration to know what had happened. Comrades, it is not easy to read things in English!

The function of the Ruby thread is self-implemented. In order to achieve platform independence, I think this kind of sacrifice is a bit big. Regardless of how much effort it takes for the author to develop. Even if it is used, it does not have the advantages of local threads. Below I wrote an example of a producer-consumer of a practical nature. To be honest, it's much longer than the example in thread.rb in Ruby. The advantage is that the problem of crossover in screen output is solved here.

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 {

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report