CSC/ECE 517 Fall 2013/ch1 1w13 aa: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 48: Line 48:


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.
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.
== Thread lifecycle ==
There is no need to start a thread once it is created. It begins running automatically when CPU resources become available.
There are a number of methods in the thread class to manipulate the thread while it is running.
The ‘value’ method of the Thread object returns the value of the last method that is computed in the thread if the thread has run to completion. Otherwise, the method blocks and does not return until the thread has completed. The method Thread.current returns the Thread object that represents the current thread. The method Thread.main returns the Thread object that represents the main thread which is the initial thread of execution when the program started. Thread.status and Thread.alive give the status of a particular thread.
A Ruby program can terminate even if its child threads are still in execution. In order to avoid such a situation, the 'join' method can be used. Join blocks the calling process until the thread that it is called on runs to completion. Alternatively a join timeout parameter can also be passed as an argument to this method to specify the amount of time to block the calling thread.

Revision as of 21:57, 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.

Thread lifecycle

There is no need to start a thread once it is created. It begins running automatically when CPU resources become available. There are a number of methods in the thread class to manipulate the thread while it is running.

The ‘value’ method of the Thread object returns the value of the last method that is computed in the thread if the thread has run to completion. Otherwise, the method blocks and does not return until the thread has completed. The method Thread.current returns the Thread object that represents the current thread. The method Thread.main returns the Thread object that represents the main thread which is the initial thread of execution when the program started. Thread.status and Thread.alive give the status of a particular thread. A Ruby program can terminate even if its child threads are still in execution. In order to avoid such a situation, the 'join' method can be used. Join blocks the calling process until the thread that it is called on runs to completion. Alternatively a join timeout parameter can also be passed as an argument to this method to specify the amount of time to block the calling thread.