Mutex: Difference between revisions

Content added Content deleted
(Added Erlang)
(Add Logtalk implementation)
Line 512: Line 512:


The "synchronized" keyword actually is a form of [[monitor]], which was a later-proposed solution to the same problems that mutexes and semaphores were designed to solve. More about synchronization may be found on Sun's website - http://java.sun.com/docs/books/tutorial/essential/concurrency/sync.html , and more about monitors may be found in any decent operating systems textbook.
The "synchronized" keyword actually is a form of [[monitor]], which was a later-proposed solution to the same problems that mutexes and semaphores were designed to solve. More about synchronization may be found on Sun's website - http://java.sun.com/docs/books/tutorial/essential/concurrency/sync.html , and more about monitors may be found in any decent operating systems textbook.

=={{header|Logtalk}}==
Logtalk provides a synchronized/0 directive for synchronizing all object (or category) predicates using the same implicit mutex and a synchronized/1 directive for synchronizing a set of predicates using the same implicit mutex. Follow an usage example of the synchronized/1 directive (inspired by the Erlang example):
<lang logtalk>
:- object(mutex_example).

:- threaded.

:- public(start/0).

:- private([slow_print_abc/0, slow_print_123/0]).
:- synchronized([slow_print_abc/0, slow_print_123/0]).

start :-
% launch two threads, running never ending goals
threaded((
repeat_abc,
repeat_123
)).

repeat_abc :-
repeat, slow_print_abc, fail.

repeat_123 :-
repeat, slow_print_123, fail.

slow_print_abc :-
write(a), thread_sleep(0.2),
write(b), thread_sleep(0.2),
write(c), nl.

slow_print_123 :-
write(1), thread_sleep(0.2),
write(2), thread_sleep(0.2),
write(3), nl.

:- end_object.
</lang>
Sample output:
<lang text>
?- mutex_example::start.
abc
123
abc
123
abc
123
abc
123
abc
...
</lang>


=={{header|Objective-C}}==
=={{header|Objective-C}}==