From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.help Subject: Example of threads and concurrency? Date: Fri, 10 Feb 2017 11:44:58 -0800 Message-ID: <87tw81sudh.fsf@ericabrahamsen.net> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1486755947 21919 195.159.176.226 (10 Feb 2017 19:45:47 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 10 Feb 2017 19:45:47 +0000 (UTC) User-Agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/26.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 10 20:45:37 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ccH8R-00052E-86 for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 20:45:35 +0100 Original-Received: from localhost ([::1]:45703 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ccH8W-000554-TG for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Feb 2017 14:45:40 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:45902) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ccH88-00054z-HO for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 14:45:17 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ccH85-0005DH-Cz for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 14:45:16 -0500 Original-Received: from [195.159.176.226] (port=47645 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ccH85-0005Cr-6P for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 14:45:13 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1ccH7t-0003GY-GO for help-gnu-emacs@gnu.org; Fri, 10 Feb 2017 20:45:01 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 64 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:n0FsnG0NK+MzvOrr18W6CmO5Gu8= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:112295 Archived-At: I've read the chapter on threads in the manual, and sort of understand it theoretically, but not really in practice. Can someone look at the below and tell me if it makes sense? I'm trying to make a simple system where there's a global variable, and 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, and I want no other code to touch the variable while the computation is happening. The following works when I run it, but I have no idea if this is actually how you do it. #+begin_src emacs-lisp (defvar important-data-variable nil "Important variable holding important data. Our goal is to update this data.") (defvar important-data-mutex (make-mutex)) (defun update-important-data () (with-mutex important-data-mutex (let ((result '(1 2 3))) ; Slow function here. (setq important-data-variable result)))) (defun update-data-in-thread () (let* ((thread (make-thread #'update-important-data)) (timer (run-at-time 10 nil `(lambda () (when (thread-alive-p ,thread) (thread-signal ,thread 'give-it-up)))))) (thread-join thread))) #+end_src 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. Other questions: 1. Other code just has to know that it can't touch `important-data-variable' without holding `important-data-mutex', right? 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? 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? 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. Any light shed would be very welcome! Eric