GNU bug report logs - #26711
Multithreading segfaults

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: guile; Reported by: Jacek Swiergocki <jswiergo@HIDDEN>; dated Sat, 29 Apr 2017 16:56:01 UTC; Maintainer for guile is bug-guile@HIDDEN.

Message received at 26711 <at> debbugs.gnu.org:


Received: (at 26711) by debbugs.gnu.org; 30 Apr 2017 22:00:03 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sun Apr 30 18:00:03 2017
Received: from localhost ([127.0.0.1]:48180 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1d4wss-0004PN-Jv
	for submit <at> debbugs.gnu.org; Sun, 30 Apr 2017 18:00:03 -0400
Received: from mail-qk0-f173.google.com ([209.85.220.173]:35304)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <linasvepstas@HIDDEN>) id 1d4wsq-0004OT-I0
 for 26711 <at> debbugs.gnu.org; Sun, 30 Apr 2017 18:00:00 -0400
Received: by mail-qk0-f173.google.com with SMTP id q1so10525863qkd.2
 for <26711 <at> debbugs.gnu.org>; Sun, 30 Apr 2017 15:00:00 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
 h=mime-version:reply-to:from:date:message-id:subject:to;
 bh=Q1noS1YpgmghGjo9bGVv8eJCb1IOLL/hTzQHx562WvE=;
 b=u9YFjG+XkL57TVtj1lthn36ZPzW+bsBq9NCjoA0gMjawMHRhaqSWY15snKRSIy0x3d
 NO05pWTEaXan3beojgYXlvwAISMqgrhehM07kqCF/zfXxMEXs4aLJ5O9TaVUiEt47GtZ
 Fm1UVpqvzzW36hygFtj3FeiXdJaRmOnCoGgIiaOIr9jHWlHabeUG1mo6Pt1A45dSFZas
 ro86MxVC+FmL7LIOSEdP0k4OOQElZPx/JKrq6K6RNqzV+332zDqcVXWa7jYUfVrpw6Lv
 zvQw9vP7PlaY3/WbT8Oll4teeAQwO+7nGT4VZd4jnOnUMTU6pY0kgdhIwbLjE0/PHYuu
 xNyA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:mime-version:reply-to:from:date:message-id
 :subject:to;
 bh=Q1noS1YpgmghGjo9bGVv8eJCb1IOLL/hTzQHx562WvE=;
 b=DsJw6lakgmGndz9tEc0neh3PEV5F8l0RNNKniEDesCh/Y0qQRk3+qRMv4igfP4cCmP
 U2AgqUak+v25kShwCG8mvQrGlOKuCb/Usa+O5XUCckoPHYuS4zjwYVVMqviQaIEUAgRD
 4X1ZMDRWLpBSGLxoSPTCV90N2QuVltv2suES/UvOgzdSFFN/0RJU7kvKT8ZiZrlCR1NO
 Sz/TGS+nPQ3WRLq4tgVDpW+i4Luzf/Mb4+Tzb/a9hC37zZng9Tbs0QhXhr4EsYK7SSpA
 ngMIfB2cjy2NeDmjlHPnbxDlRc5eWA3RzKosGeRZsMLzMHk46dldOk57DRraLiakkx0B
 OX7w==
X-Gm-Message-State: AN3rC/7c6YnKgyZAkgakyzZL9OK+cyVWXzW+5+RXFvcm5zhsmy8rau6n
 hKHilQVc2ukhyXIHpBq3KQ1V1np30g==
X-Received: by 10.55.23.96 with SMTP id i93mr21550506qkh.114.1493589594885;
 Sun, 30 Apr 2017 14:59:54 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.12.135.88 with HTTP; Sun, 30 Apr 2017 14:59:34 -0700 (PDT)
From: Linas Vepstas <linasvepstas@HIDDEN>
Date: Sun, 30 Apr 2017 16:59:34 -0500
Message-ID: <CAHrUA34dgQDbKvSMEDz-jtHXSvU_WcEa8dmd5Zsbs2Z7F8dq7g@HIDDEN>
Subject: Example1 is buggy
To: 26711 <at> debbugs.gnu.org
Content-Type: multipart/alternative; boundary=001a1146d04e3e550c054e696d73
X-Spam-Score: 0.5 (/)
X-Debbugs-Envelope-To: 26711
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Reply-To: linasvepstas@HIDDEN
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: 0.5 (/)

--001a1146d04e3e550c054e696d73
Content-Type: text/plain; charset=UTF-8

Example1.cc is has a work-around -- main() needs to call scm_init_guile()
or scm_with_guile().  If this is done, the problem goes away.

The problem with example1 is that the first thread to initialize guile is
eventually destroyed. However, the first thread to call guile never ever
sets "needs_unregister" in libguile/threads.c and thus, bdwgc never finds
out that this thread no longer exists. Sooner or later, bdwgc touches this
non-existent thread, and crashes.

If its OK to initialize guile for the first time ever in a transient
thread, then there's a bug in guile; else there's a bug in the example.

I'm now looking into example2.

--linas

--001a1146d04e3e550c054e696d73
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div><div><div><div>Example1.cc is has a work-around =
-- main() needs to call scm_init_guile() or scm_with_guile().=C2=A0 If this=
 is done, the problem goes away.<br></div><br></div>The problem with exampl=
e1 is that the first thread to initialize guile is eventually destroyed. Ho=
wever, the first thread to call guile never ever sets &quot;needs_unregiste=
r&quot; in libguile/threads.c and thus, bdwgc never finds out that this thr=
ead no longer exists. Sooner or later, bdwgc touches this non-existent thre=
ad, and crashes.<br><br></div>If its OK to initialize guile for the first t=
ime ever in a transient thread, then there&#39;s a bug in guile; else there=
&#39;s a bug in the example.<br><br></div>I&#39;m now looking into example2=
.<br><br></div>--linas<br></div>

--001a1146d04e3e550c054e696d73--




Information forwarded to bug-guile@HIDDEN:
bug#26711; Package guile. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 29 Apr 2017 16:55:14 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Apr 29 12:55:14 2017
Received: from localhost ([127.0.0.1]:46544 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1d4VeM-00072x-0H
	for submit <at> debbugs.gnu.org; Sat, 29 Apr 2017 12:55:14 -0400
Received: from eggs.gnu.org ([208.118.235.92]:35767)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <jswiergo@HIDDEN>) id 1d4QMU-0005gi-2g
 for submit <at> debbugs.gnu.org; Sat, 29 Apr 2017 07:16:26 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
 (envelope-from <jswiergo@HIDDEN>) id 1d4QMN-0005px-28
 for submit <at> debbugs.gnu.org; Sat, 29 Apr 2017 07:16:20 -0400
X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org
X-Spam-Level: 
X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_50,FREEMAIL_FROM,
 HTML_MESSAGE,T_DKIM_INVALID autolearn=disabled version=3.3.2
Received: from lists.gnu.org ([2001:4830:134:3::11]:32887)
 by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32)
 (Exim 4.71) (envelope-from <jswiergo@HIDDEN>) id 1d4QMM-0005ps-UL
 for submit <at> debbugs.gnu.org; Sat, 29 Apr 2017 07:16:18 -0400
Received: from eggs.gnu.org ([2001:4830:134:3::10]:54957)
 by lists.gnu.org with esmtp (Exim 4.71)
 (envelope-from <jswiergo@HIDDEN>) id 1d4QML-0000wf-5n
 for bug-guile@HIDDEN; Sat, 29 Apr 2017 07:16:18 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
 (envelope-from <jswiergo@HIDDEN>) id 1d4QMJ-0005oe-HY
 for bug-guile@HIDDEN; Sat, 29 Apr 2017 07:16:17 -0400
Received: from mail-qt0-x22b.google.com ([2607:f8b0:400d:c0d::22b]:35692)
 by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16)
 (Exim 4.71) (envelope-from <jswiergo@HIDDEN>) id 1d4QMJ-0005oN-Cj
 for bug-guile@HIDDEN; Sat, 29 Apr 2017 07:16:15 -0400
Received: by mail-qt0-x22b.google.com with SMTP id y33so66714781qta.2
 for <bug-guile@HIDDEN>; Sat, 29 Apr 2017 04:16:15 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
 h=mime-version:from:date:message-id:subject:to;
 bh=2RSnOsSr1v8OPSHbs8hRuIKG05rn+krg/A6QglI9t6w=;
 b=unH9EJkVOyW0BtJkX1KAP/AvSK6H29eTnrEjoLJFHp5nGsdBIxFVb+hLulxwk69Dn/
 qxtT0jCm9SlqA2f697uIIWUNtBqiSiae1D7n4Eotbfhox3ibUAO7sxulCqm1E/hqSYWm
 27wFzB+zSwE80V6FLylj0EhjDhDmdMNvNCCDmmAXmOQJTraWOQpBKRy4BhqdZtWNdLlZ
 ANPqUBWlBVbmhwR/1W4pbNwOwtFsi7S1IZaVYMH0iwRh1tyTfUYOkRGo/gvppdwKDwV+
 aZfWespVJuqKo2x3VTFXlwGxH/tC2cMgPjQGh0CnIlgvR8mYx+riffYFNUbnzwxa2rNw
 waYA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:mime-version:from:date:message-id:subject:to;
 bh=2RSnOsSr1v8OPSHbs8hRuIKG05rn+krg/A6QglI9t6w=;
 b=i6DlOQakT4PZnAuoUG165FS6wepZ6tIdjX5RMGFVT4EoWJZYwb4EXPCoT62GB7HG/K
 Vqf0iBda1DgioC9rM6J53FFJ7iopKApk+Ib+x9sZDamZOlxPAssLYeWkrwIGeV03bxkH
 ddAoYHsWgZvU/qeNh1vIPKvXw8qI/EvtNcxKFANiCBsI4vmWrJfLcb6VXSUNICmr+8wI
 Bgx0z26LHgacvJ5uhKZVfOM6YnCdOfDRRCeBolLTG1yYTzD1K/5+wvnQrVW+y86UYF22
 OhxukuBknXvL8vfZ510fvv+jWUzvsILql5G58cO//LXFL1yTRXfj4XNaUTkdLJh7wMh0
 rWYA==
X-Gm-Message-State: AN3rC/7bVVcPaoMxYwza93X2xP/shfOayE5wFsZxnyf90dncJTE408ux
 oujdg2tOs9teOPYxACquN72Aw3CTkzC6
X-Received: by 10.237.38.135 with SMTP id q7mr13681619qtd.115.1493464574466;
 Sat, 29 Apr 2017 04:16:14 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.12.150.123 with HTTP; Sat, 29 Apr 2017 04:16:13 -0700 (PDT)
From: Jacek Swiergocki <jswiergo@HIDDEN>
Date: Sat, 29 Apr 2017 13:16:13 +0200
Message-ID: <CAPLvL3_rXNC7L-vQ8ANsFsHqmW3mQPsga1LDsZf5czsJZU6QTA@HIDDEN>
Subject: Multithreading segfaults
To: bug-guile@HIDDEN
Content-Type: multipart/alternative; boundary=94eb2c125580722374054e4c51cf
X-detected-operating-system: by eggs.gnu.org: Genre and OS details not
 recognized.
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x
X-Received-From: 2001:4830:134:3::11
X-Spam-Score: -4.0 (----)
X-Debbugs-Envelope-To: submit
X-Mailman-Approved-At: Sat, 29 Apr 2017 12:55:13 -0400
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -4.0 (----)

--94eb2c125580722374054e4c51cf
Content-Type: text/plain; charset=UTF-8

Hi all,

I have two examples of multi threading code that crash with segmentation
fault. If there is a bug in guile please fix it. If there is a problem only
in my code please help me how it can be workaround.

I am using Ubuntu 14.04 and guile compiled from the repository. The
examples are compiled by:
g++ demo.cc -Wall -std=c++11 -pthread -I/usr/local/test/include/guile/2.2
-L/usr/local/test/lib -lguile-2.2 -lgc

The first example has started to segfault since guile tagged v2.1.7. I have
not encountered problems for v2.1.6 and older versions. However for recent
version v2.2.2 it requires much more iterations to fail than v2.1.7 that
fails instantly.

////////////////////////////////////////////////////////////////////////////
// Example 1.

#include <libguile.h>

#include <thread>
#include <vector>
#include <atomic>
#include <mutex>

static volatile bool hold = true;
static std::atomic_int start_cnt(0);

static std::mutex init_once_mtx;
static bool start_inited_once = false;
static bool is_inited_once = false;

static std::mutex gc_mtx;

class Eval
{
public:
    Eval();
    ~Eval();
};

void* c_wrap_init(void*)
{
    return nullptr;
}

Eval::Eval()
{
    scm_with_guile(c_wrap_init, this);
}

Eval::~Eval()
{
    std::lock_guard<std::mutex> lck(gc_mtx);
    scm_gc();
}

void* c_wrap_init_only_once(void*)
{
    is_inited_once = true;
    return nullptr;
}

void init_only_once()
{
    std::lock_guard<std::mutex> lck(init_once_mtx);
    if (!start_inited_once)
    {
        start_inited_once = true;
        scm_with_guile(c_wrap_init_only_once, nullptr);
    }
    while (!is_inited_once); // spin;
}

void threadedInit(int thread_id)
{
    start_cnt ++;
    while (hold) {} // spin

    init_only_once();
    Eval* ev = new Eval();

    delete ev;
}

void test_init_race()
{
    int n_threads = 120;
    start_cnt = 0;
    hold = true;

    std::vector<std::thread> thread_pool;
    for (int i = 0; i < n_threads; i++)
        thread_pool.push_back(std::thread(&threadedInit, i));

    while (start_cnt != n_threads) {}  // spin
    printf("Done creating %d threads\n", n_threads);
    hold = false;

    for (std::thread& t : thread_pool) t.join();
    printf("Done joining %d threads\n", n_threads);
}

int main()
{
    for (int k = 0; k < 10000; k++)
    {
        test_init_race();
        printf("------------------ done iteration %d\n", k);
}

The second example requires much more iterations to crash with segfault.
Sometimes hundreds, sometimes thousands, it seems to be random. You need to
wait over a dozen of minutes, sometimes you need try again and restart. I
have found this problem for old versions e.g. v2.0.11 as well as for recent
version v2.2.2, so it seems to be an old problem.

////////////////////////////////////////////////////////////////////////////
// Example 2.

#include <libguile.h>

#include <thread>
#include <vector>
#include <atomic>
#include <mutex>

static volatile bool hold = true;
static std::atomic_int start_cnt(0);

static std::mutex init_once_mtx;
static bool start_inited_once = false;
static bool is_inited_once = false;

void* c_wrap_init_only_once(void*)
{
    is_inited_once = true;
    return nullptr;
}

void* c_wrap_eval(void*)
{
    return nullptr;
}

void init_only_once()
{
    std::lock_guard<std::mutex> lck(init_once_mtx);
    if (!start_inited_once)
    {
        start_inited_once = true;
        scm_with_guile(c_wrap_init_only_once, nullptr);
    }
    while (!is_inited_once); // spin;
}

void threadedInit(int thread_id)
{
    start_cnt ++;
    while (hold) {} // spin

    init_only_once();
    for (int i = 0; i < 100; ++i)
    {
        scm_with_guile(c_wrap_eval, nullptr);
    }
}

void test_init_race()
{
    int n_threads = 120;
    start_cnt = 0;
    hold = true;

    std::vector<std::thread> thread_pool;
    for (int i = 0; i < n_threads; i++)
        thread_pool.push_back(std::thread(&threadedInit, i));

    while (start_cnt != n_threads) {}  // spin
    printf("Done creating %d threads\n", n_threads);
    hold = false;

    for (std::thread& t : thread_pool) t.join();
    printf("Done joining %d threads\n", n_threads);
}

int main()
{
    for (int k = 0; k < 10000; k++)
    {
        test_init_race();
        printf("------------------ done iteration %d\n", k);
    }
}

--
Jacek

--94eb2c125580722374054e4c51cf
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Hi all,</div><div><br></div><div>I have two examples =
of multi threading code that crash with segmentation fault. If there is a b=
ug in guile please fix it. If there is a problem only in my code please hel=
p me how it can be workaround.</div><div><br></div><div>I am using Ubuntu 1=
4.04 and guile compiled from the repository. The examples are compiled by:<=
/div><div>g++ demo.cc -Wall -std=3Dc++11 -pthread -I/usr/local/test/include=
/guile/2.2 -L/usr/local/test/lib -lguile-2.2 -lgc</div><div><br></div><div>=
The first example has started to segfault since guile tagged v2.1.7. I have=
 not encountered problems for v2.1.6 and older versions. However for recent=
 version v2.2.2 it requires much more iterations to fail than v2.1.7 that f=
ails instantly.</div><div><br></div><div>//////////////////////////////////=
//////////////////////////////////////////</div><div>// Example 1.</div><di=
v><br></div><div>#include &lt;libguile.h&gt;</div><div><br></div><div>#incl=
ude &lt;thread&gt;</div><div>#include &lt;vector&gt;</div><div>#include &lt=
;atomic&gt;</div><div>#include &lt;mutex&gt;</div><div><br></div><div>stati=
c volatile bool hold =3D true;</div><div>static std::atomic_int start_cnt(0=
);</div><div><br></div><div>static std::mutex init_once_mtx;</div><div>stat=
ic bool start_inited_once =3D false;</div><div>static bool is_inited_once =
=3D false;</div><div><br></div><div>static std::mutex gc_mtx;</div><div><br=
></div><div>class Eval</div><div>{</div><div>public:</div><div>=C2=A0 =C2=
=A0 Eval();</div><div>=C2=A0 =C2=A0 ~Eval();</div><div>};</div><div><br></d=
iv><div><div><div>void* c_wrap_init(void*)</div><div>{</div><div>=C2=A0 =C2=
=A0 return nullptr;</div><div>}</div></div><div><br></div></div><div>Eval::=
Eval()<br></div><div>{</div><div>=C2=A0 =C2=A0 scm_with_guile(c_wrap_init, =
this);</div><div>}</div><div><br></div><div>Eval::~Eval()</div><div>{</div>=
<div>=C2=A0 =C2=A0 std::lock_guard&lt;std::mutex&gt; lck(gc_mtx);</div><div=
>=C2=A0 =C2=A0 scm_gc();</div><div>}</div><div><div><br></div></div><div>vo=
id* c_wrap_init_only_once(void*)</div><div>{</div><div>=C2=A0 =C2=A0 is_ini=
ted_once =3D true;</div><div>=C2=A0 =C2=A0 return nullptr;</div><div>}</div=
><div><br></div><div>void init_only_once()</div><div>{</div><div>=C2=A0 =C2=
=A0 std::lock_guard&lt;std::mutex&gt; lck(init_once_mtx);</div><div>=C2=A0 =
=C2=A0 if (!start_inited_once)</div><div>=C2=A0 =C2=A0 {</div><div>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 start_inited_once =3D true;</div><div>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 scm_with_guile(c_wrap_init_only_once, nullptr);</div><div>=C2=
=A0 =C2=A0 }</div><div>=C2=A0 =C2=A0 while (!is_inited_once); // spin;</div=
><div>}</div><div><br></div><div>void threadedInit(int thread_id)</div><div=
>{</div><div>=C2=A0 =C2=A0 start_cnt ++;</div><div>=C2=A0 =C2=A0 while (hol=
d) {} // spin</div><div><br></div><div>=C2=A0 =C2=A0 init_only_once();</div=
><div>=C2=A0 =C2=A0 Eval* ev =3D new Eval();</div><div><br></div><div>=C2=
=A0 =C2=A0 delete ev;</div><div>}</div><div><br></div><div>void test_init_r=
ace()</div><div>{</div><div>=C2=A0 =C2=A0 int n_threads =3D 120;</div><div>=
=C2=A0 =C2=A0 start_cnt =3D 0;</div><div>=C2=A0 =C2=A0 hold =3D true;</div>=
<div><br></div><div>=C2=A0 =C2=A0 std::vector&lt;std::thread&gt; thread_poo=
l;</div><div>=C2=A0 =C2=A0 for (int i =3D 0; i &lt; n_threads; i++)</div><d=
iv>=C2=A0 =C2=A0 =C2=A0 =C2=A0 thread_pool.push_back(std::thread(&amp;threa=
dedInit, i));</div><div><br></div><div>=C2=A0 =C2=A0 while (start_cnt !=3D =
n_threads) {} =C2=A0// spin</div><div>=C2=A0 =C2=A0 printf(&quot;Done creat=
ing %d threads\n&quot;, n_threads);</div><div>=C2=A0 =C2=A0 hold =3D false;=
</div><div><br></div><div>=C2=A0 =C2=A0 for (std::thread&amp; t : thread_po=
ol) t.join();</div><div>=C2=A0 =C2=A0 printf(&quot;Done joining %d threads\=
n&quot;, n_threads);</div><div>}</div><div><br></div><div>int main()</div><=
div>{</div><div>=C2=A0 =C2=A0 for (int k =3D 0; k &lt; 10000; k++)</div><di=
v>=C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 test_init_race();</=
div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 printf(&quot;------------------ done i=
teration %d\n&quot;, k);</div><div>}</div><div><br></div><div>The second ex=
ample requires much more iterations to crash with segfault. Sometimes hundr=
eds, sometimes thousands, it seems to be random. You need to wait over a do=
zen of minutes, sometimes you need try again and restart. I have found this=
 problem for old versions e.g. v2.0.11 as well as for recent version v2.2.2=
, so it seems to be an old problem.</div><div><br></div><div>//////////////=
//////////////////////////////////////////////////////////////</div><div>//=
 Example 2.</div><div><br></div><div>#include &lt;libguile.h&gt;</div><div>=
<br></div><div>#include &lt;thread&gt;</div><div>#include &lt;vector&gt;</d=
iv><div>#include &lt;atomic&gt;</div><div>#include &lt;mutex&gt;</div><div>=
<br></div><div>static volatile bool hold =3D true;</div><div>static std::at=
omic_int start_cnt(0);</div><div><br></div><div>static std::mutex init_once=
_mtx;</div><div>static bool start_inited_once =3D false;</div><div>static b=
ool is_inited_once =3D false;</div><div><br></div><div>void* c_wrap_init_on=
ly_once(void*)</div><div>{</div><div>=C2=A0 =C2=A0 is_inited_once =3D true;=
</div><div>=C2=A0 =C2=A0 return nullptr;</div><div>}</div><div><br></div><d=
iv>void* c_wrap_eval(void*)</div><div>{</div><div>=C2=A0 =C2=A0 return null=
ptr;</div><div>}</div><div><br></div><div>void init_only_once()</div><div>{=
</div><div>=C2=A0 =C2=A0 std::lock_guard&lt;std::mutex&gt; lck(init_once_mt=
x);</div><div>=C2=A0 =C2=A0 if (!start_inited_once)</div><div>=C2=A0 =C2=A0=
 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 start_inited_once =3D true;</div><=
div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 scm_with_guile(c_wrap_init_only_once, nullp=
tr);</div><div>=C2=A0 =C2=A0 }</div><div>=C2=A0 =C2=A0 while (!is_inited_on=
ce); // spin;</div><div>}</div><div><br></div><div>void threadedInit(int th=
read_id)</div><div>{</div><div>=C2=A0 =C2=A0 start_cnt ++;</div><div>=C2=A0=
 =C2=A0 while (hold) {} // spin</div><div><br></div><div>=C2=A0 =C2=A0 init=
_only_once();</div><div>=C2=A0 =C2=A0 for (int i =3D 0; i &lt; 100; ++i)</d=
iv><div>=C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 scm_with_guil=
e(c_wrap_eval, nullptr);</div><div>=C2=A0 =C2=A0 }</div><div>}</div><div><b=
r></div><div>void test_init_race()</div><div>{</div><div>=C2=A0 =C2=A0 int =
n_threads =3D 120;</div><div>=C2=A0 =C2=A0 start_cnt =3D 0;</div><div>=C2=
=A0 =C2=A0 hold =3D true;</div><div><br></div><div>=C2=A0 =C2=A0 std::vecto=
r&lt;std::thread&gt; thread_pool;</div><div>=C2=A0 =C2=A0 for (int i =3D 0;=
 i &lt; n_threads; i++)</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 thread_pool.p=
ush_back(std::thread(&amp;threadedInit, i));</div><div><br></div><div>=C2=
=A0 =C2=A0 while (start_cnt !=3D n_threads) {} =C2=A0// spin</div><div>=C2=
=A0 =C2=A0 printf(&quot;Done creating %d threads\n&quot;, n_threads);</div>=
<div>=C2=A0 =C2=A0 hold =3D false;</div><div><br></div><div>=C2=A0 =C2=A0 f=
or (std::thread&amp; t : thread_pool) t.join();</div><div>=C2=A0 =C2=A0 pri=
ntf(&quot;Done joining %d threads\n&quot;, n_threads);</div><div>}</div><di=
v><br></div><div>int main()</div><div>{</div><div>=C2=A0 =C2=A0 for (int k =
=3D 0; k &lt; 10000; k++)</div><div>=C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0=
 =C2=A0 =C2=A0 test_init_race();</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 prin=
tf(&quot;------------------ done iteration %d\n&quot;, k);</div><div>=C2=A0=
 =C2=A0 }</div><div>}</div><div><br></div><div>--</div><div>Jacek</div><div=
><br></div></div>

--94eb2c125580722374054e4c51cf--




Acknowledgement sent to Jacek Swiergocki <jswiergo@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-guile@HIDDEN. Full text available.
Report forwarded to bug-guile@HIDDEN:
bug#26711; Package guile. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Mon, 25 Nov 2019 12:00:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.