unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Noam Postavsky <npostavs@users.sourceforge.net>
To: Eric Abrahamsen <eric@ericabrahamsen.net>
Cc: Emacs developers <emacs-devel@gnu.org>
Subject: Re: Example of threads and concurrency?
Date: Sat, 18 Feb 2017 14:23:22 -0500	[thread overview]
Message-ID: <CAM-tV-9P+_E_B2-wFAzvkkfG0_q+bON2OSa=vFc5g2LnYdWX-g@mail.gmail.com> (raw)
In-Reply-To: <87h93r49wh.fsf@ericabrahamsen.net>

On Sat, Feb 18, 2017 at 1:43 PM, Eric Abrahamsen
<eric@ericabrahamsen.net> wrote:
>> I update the variable with a time-consuming/network-bound
>> computation. I want the computation to happen in a thread so it
>> doesn't block,

You're out of luck, only one thread runs at a time.

>> Is everything in the right place? It just occurs to me that If I put
>> `thread-join' at the bottom of `update-data-in-thread' then it will
>> effectively be synchronous after all! So I put that somewhere else, or I
>> don't call it at all.

Yes (although as I said above, there is no parallelism regardless).

>>
>> Other questions:
>>
>> 1. Other code just has to know that it can't touch
>>    `important-data-variable' without holding `important-data-mutex',
>>    right?

Yes.

>> 2. I want the timer to signal the thread to give up after ten seconds --
>>    do I need to put anything in `update-important-data' that handles
>>    that signal?

The thread needs to call something that would yield, otherwise the
cancelling signal call will never have a chance to be sent.  The thread
doesn't need to handle the signal (assuming you're fine with just
quitting the thread).

>> 3. What do condition vars actually do? The manual has an example that
>>    looks just like what I'm doing above:
>>
>>    (with-mutex mutex
>>      (setq global-variable (some-computation))
>>      (condition-notify cond-var))
>>
>>    What is cond-var doing here?

To unblock the other thread that is waiting for it (2 paragraphs up):

    (with-mutex mutex
      (while (not global-variable)
        (condition-wait cond-var)))

>> It seems like both mutexes and cond-vars are there so other code knows
>> when to keep its hands off `important-data-variable', but I don't
>> understand how they're actually used.

They are used to synchronize between threads.  But if your thread is
just doing a single computation and then ends, make-thread and
thread-join can be an alternative to them.



  reply	other threads:[~2017-02-18 19:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <87tw81sudh.fsf@ericabrahamsen.net>
2017-02-18 18:43 ` Example of threads and concurrency? Eric Abrahamsen
2017-02-18 19:23   ` Noam Postavsky [this message]
2017-02-18 19:41     ` Eli Zaretskii
2017-02-18 19:43       ` Noam Postavsky
2017-02-18 19:27   ` Eli Zaretskii
2017-02-18 20:40     ` Eric Abrahamsen
2017-02-19 16:02       ` Eli Zaretskii
2017-02-19 19:50         ` Eric Abrahamsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAM-tV-9P+_E_B2-wFAzvkkfG0_q+bON2OSa=vFc5g2LnYdWX-g@mail.gmail.com' \
    --to=npostavs@users.sourceforge.net \
    --cc=emacs-devel@gnu.org \
    --cc=eric@ericabrahamsen.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).