Threads and Synchronization in Ruby..!
Ruby No Comments »I was just free this weekend,so just gotta a chance and dropped myself with a little program on threads and synchronization in ruby.This program generates a secret number which is implemented as sync_server_thread on the server side and then assigns the same variable on the the client side.
In this program , I took care of things like mutual exclusion and deadlocks.In the very first line I am requiring thread class and creating a new object for Mutex(a class which handles stuff related to mutual exclusion of threads), a instance of condition variable (a condition variable is a semaphore which is mainly used in accordance with mutex class,its main purpose is to take of processes in the critical section which are stuck in themselves while waiting for some external resources like non ruby programs).The sync_var_1 and sync_var_2 are local variables which will contain the secret number that will be generated at the server side and will be assigned to the client side program.
The first thread is the server side which runs on the server side and is responsible for generating the secret number on the server side. In The server side thread we can see that the condition variable sync_prevention.wait, there is no such implication on the presence of this statement in the program,but I have actually kept it if in case we include any functionality in the program which generally calls some external program to run as a part of the thread for example if I use a third party secret number generator for generating a secure secret number which may take a ample of time to generate the secret number.The wait method of condition variable generally tends to release the current thread under mutex and re invokes that process later when the external program has responded.
Similarly in the second thread, the signal method of the condition variable tends to wake up the first thread waiting for the external resource.By doing this, we can maintain a balance of load on the server where multiple requests for generating the secret number.
The join method is one of the coolest and a must use method in the Thread class,I call it a must use method because when a ruby program containing some code related to thread runs,All the threads creating during the course of execution of the program will and must be killed once the program exits out of its execution.So To make sure that these threads complete at their whole, we use the associate them the join method.And finally the sync_diff is make sure that the same values at the server as well as the client remain intact at any point of time and the difference between them should always be zero.
There are some implcations in the existing program, that the client and server run on the same file. In my coming posts you will see that the
the server thread runs under the server enviornment like webbrick or mongrel or may be a backgroundDRB…!…and the client thread will be running as a controller process in case of our rails app and will be synchronized with the server’s secret value.








