CSC/ECE 517 Fall 2013/ch1 1w13 aa: Difference between revisions
No edit summary |
No edit summary |
||
Line 10: | Line 10: | ||
The following code illustrates multithreading in Ruby | The following code illustrates multithreading in Ruby | ||
<pre> | |||
def function_1 | def function_1 | ||
count = 0 | count = 0 | ||
Line 32: | Line 32: | ||
thread1.join | thread1.join | ||
thread2.join | thread2.join | ||
</pre> | |||
Two threads are created and functions function_1 and fucntion_2 are passed as parameters. thread1 executes fucntion_1 and thread2 executes fucntion_2 . The following interleaved output is produced. | Two threads are created and functions function_1 and fucntion_2 are passed as parameters. thread1 executes fucntion_1 and thread2 executes fucntion_2 . The following interleaved output is produced. |
Revision as of 21:49, 17 September 2013
Multithreading in Rails
Traditional programs have a single-thread of execution. The statements or instructions that comprise the program are executed sequentially until the program terminates. The threads are in-process and are implemented by the Ruby interpreter. As interpreted code is independent of the operating system, ruby threads are completely portable. On the other hand, there are some disadvantages when compared to native threads, such as deadlock and starvation. If a thread makes a call to the operating system that takes a long time to complete, all threads will hang until the interpreter gets control back. As the threads run within a single process, it cannot take advantage of multiple processors to achieve parallelism. Despite having so many disadvantages, Ruby threads are an efficient and lightweight way of achieving parallelism in code.
Creating Ruby Threads
The Thread library of Ruby allows concurrent programming. It provides multiple threads of execution in a single process that share the same memory space and execute concurrently. The Thread class represents user-level threads.To start a new thread, the Thread.new call is used and a block is passed as a parameter to it. This block of code runs in the thread.
The following code illustrates multithreading in Ruby
def function_1 count = 0 while count<=2 puts "Inside function_1" count += count end end def function_2 count = 0 while count<=2 puts "Inside function_2" count += count end end thread1 = Thread.new{function_1()} thread2 = Thread.new{function_2()} thread1.join thread2.join
Two threads are created and functions function_1 and fucntion_2 are passed as parameters. thread1 executes fucntion_1 and thread2 executes fucntion_2 . The following interleaved output is produced.
Inside function_1 Inside fucntion_1 Inside function_2 Inside function_2 Inside function_1 Inside fucntion_2
Threads share the global, instance and local variables that are in existence at the time the thread starts. Local variables created within a thread’s block are truly local to that thread. Each thread will have its own copy of these variables. Any number of arguments can be passed to the thread as parameters to Thread.new.