Merge pull request #3 from pizzocel/thread-safe-execute Added a mutex protecting execution of MPD commands.
diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb
index e6a1095..0f8c0f3 100644
--- a/lib/mpd_client.rb
+++ b/lib/mpd_client.rb
@@ -154,6 +154,7 @@ class MPDClient
end
def initialize
+ @mutex = Mutex.new
reset
end
@@ -203,12 +204,14 @@ class MPDClient
private
def execute(command, *args, retval)
- if !@command_list.nil?
- write_command(command, *args)
- @command_list << retval
- else
- write_command(command, *args)
- eval retval
+ @mutex.synchronize do
+ if !@command_list.nil?
+ write_command(command, *args)
+ @command_list << retval
+ else
+ write_command(command, *args)
+ eval retval
+ end
end
end