title: Ruby multithreading
body: <p>I have code that is running in a manner similar to the sample code below. There are two threads that loop at certain time intervals. The first thread sets a flag and depending on the value of this flag the second thread prints out a result. My question is in a situation like this where only one thread is changing the value of the resource (@flag), and the second thread is only accessing its value but not changing it, is a mutex lock required? Any explanations? </p>
  
  <pre><code>Class Sample
  
    def initialize
      @flag=""
      @wait_interval1 = 20
      @wait_interval2 = 5
    end
  
    def thread1(x)
      Thread.start do
      loop do
        if x.is_a?(String)
          @flag = 0
        else
          @flag = 1
        sleep @wait_interval
      end
      end
    end
  
    def thread2(y)
      Thread.start do
      loop do
        if @flag == 0
          if y.start_with?("a")
            puts "yes"
          else
            puts "no"
          end
        end
      end
     end
     end
  
  end
  </code></pre>