0
votes

I have created a new thread inside main thread and running while loop until my global variable changed. But if I change the global variable in main thread it is not reflected in new thread. What will be the problem.

Thread.new do     
  loop do  
p "login info"
p logged
mutex.synchronize do
if(!logged)
    break
  else
    begin
   #do something
   sleep 5          
    rescue => e 
    p  e     
    end
  end   
  end
end

I have set logged to false in logout method. But the loop is not breaking

module SessionsHelper

logged =false

# Logs in the given user.

def log_in(user)

session[:user_id] = user[:id]

storeAuthToken(user)

logged=true    

end

1
can you show where have you defined logged. - Saurabh
logged is local variable for the scope of the thread. You might want to use class/instance variable, but this is definitely a task for a Mutex. - Aleksei Matiushkin
logged is defined at class level - Dinu

1 Answers

0
votes

variable has to define as $logged = false