Skip to content

Commit

Permalink
added docs
Browse files Browse the repository at this point in the history
  • Loading branch information
amitmurthy committed Apr 1, 2015
1 parent fdcc82c commit 6813f44
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ Library improvements

* `readavailable` returns a byte vector instead of a string.

* `lock` and `unlock` which operate on `ReentrantLock`. Useful to lock a stream during
concurrent writes from multiple tasks


Deprecated or removed
---------------------

Expand Down
44 changes: 44 additions & 0 deletions doc/manual/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,50 @@ potential performance optimizations that can be achieved by other
means (e.g., using explicit loops), operators like ``+=`` and ``*=``
work by rebinding new values.

Asynchronous IO and concurrent synchronous writes
-------------------------------------------------

Why do concurrent writes to the same stream result in inter-mixed output?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

While the streaming I/O API is synchronous, the underlying implementation
is fully asynchronous.

The following::

@sync for i in 1:3
@async print(i, " Foo ", " Bar ")
end

results in::
123 Foo Foo Foo Bar Bar Bar

This is happening because, while ``print(i, " Foo ", " Bar ")`` is synchronous,
internally, the writing of each argument yields to other tasks while waiting for
that part of the I/O to complete.

``println`` to asynchronous streams like STDOUT, TcpSockets, "locks" the stream
during a call. Consequently changing ``print`` to ``println`` in the above example
results in::

1 Foo Bar
2 Foo Bar
3 Foo Bar

For other functions and streams, etc, you could lock your writes with a ``RentrantLock``
like this::

l = ReentrantLock()
@sync for i in 1:3
@async begin
lock(l)
try
print(i, " Foo ", " Bar ")
finally
unlock(l)
end
end


Julia Releases
----------------
Expand Down
16 changes: 16 additions & 0 deletions doc/stdlib/parallel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ Tasks
Block the current task for a specified number of seconds. The minimum sleep
time is 1 millisecond or input of ``0.001``.

.. function:: ReentrantLock()

Creates a reentrant lock. The same task can acquire the lock as many times
as required. Each lock must be matched with an unlock.

.. function:: lock(l::ReentrantLock)

Associates ``l`` with the current task. If ``l`` is already locked by a different
task, waits for it to become available. The same task can acquire the lock multiple
times. Each "lock" must be matched by an "unlock"

.. function:: unlock(l::ReentrantLock)

Releases ownership of the lock by the current task. If the lock had been acquired before,
it just decrements an internal counter and returns immediately.


General Parallel Computing Support
----------------------------------
Expand Down

0 comments on commit 6813f44

Please sign in to comment.