For fun I wrote this Ruby socket server which actually works quite nicely. I'm plannin on using it for the backend of an iOS App. My question for now is, when in the thread do I need a Mutex? Will I need one when accessing a shared variable such as @clients?
require 'rubygems'
require 'socket'
module Server
    @server = Object.new
    @clients = []
    @sessions
    def self.run(port=3000)
        @server = TCPServer.new port
        while (socket=@server.accept)
            @clients << socket
            Thread.start(socket) do |socket|
                begin
                    loop do
                        begin
                            msg = String.new
                            while(data=socket.read_nonblock(1024))
                      msg << data
                                  break if data.to_s.length < 1024
                            end
                            @clients.each do |client| client.write "#{socket} says: #{msg}" unless client == socket end
                        rescue
                        end
                    end
                rescue => e
                    @clients.delete socket
                    puts e
                    puts "Killed client #{socket}"
                    Thread.kill self
                end
            end
        end
    end
end
Server.run
--Edit--
According to the answer from John Bollinger I need to synchronize the thread any time that a thread needs to access a shared resource. Does this apply to database queries? Can I read/write from a postgres database with ActiveRecord ORM inside multiple threads at once?
 
     
     
    