From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 11485 invoked from network); 10 Aug 2023 21:52:39 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 10 Aug 2023 21:52:39 -0000 Received: (qmail 21759 invoked by uid 550); 10 Aug 2023 21:52:33 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 21708 invoked from network); 10 Aug 2023 21:52:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691704340; x=1692309140; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=4dwugKctZi1MwV0tiykMYTDB8FZXhZh/SpCCB2PUYCY=; b=FtuDJxXsgNYrVsaxJFGpgbhVGm3zdDjfz9GG0lAMuKeOkjC8huMlC5nGBz1Z9Ogxud iBFYrLGqw0/9P6xYEDhjQcpSVEzS+XFlXCnbTwxek0HSXtcWBhqtB+MhLNx/7HXFM8uK r+gP9KPGA1cknimVnQlbHD9z+Fs/UvC3tmRKC4PKbpA6GNCov/no07cZ3kyySC4Ae5ts cf1H6rh8rxMXIWMZl2o2sWzOmEl6+sGY+bwGvrD62CeIDd6IU9hp4Iha9uQtREnssd7V 0OYbtSjos0KyuH27WOlHct6uELV3pQy90VkDO1iLkBmmIyBqRKNMjy3FElKx/HKV/YtA shQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691704340; x=1692309140; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4dwugKctZi1MwV0tiykMYTDB8FZXhZh/SpCCB2PUYCY=; b=FTN5AJwuPgCMRAskO1qTIUoi+FnD5mIwU6F6t6atTyBgZIn9sKeWn+8RyFj1KWaiUn Ki/FfKL+MnvoEBQMM0s1HPl673MbIabYPCsBH577fWxTLOFwz4HFapnBS0JmHaSsllNE UDp/R0G45lfKyG0xCdej2sTEM2BtOEvQ3kxek14RJP6OV5g0tCGCVvVBjnUdYffls0YG rS6LiQFH70JVjpW3q7ldd3c4BGimASk8sSMUxKOs6mppemat6b2PLIVTuQx3IG/8ZkCx SkISIr2e3OGz3Neb0hAE6pPVDSz1E9C/O1UYbUH7AQTYzfjFeI0bw4SMa6EEBBM/OHtK mWDQ== X-Gm-Message-State: AOJu0Yxq/e1D/a2IybcOhq3zu2ktDd/gwb4ByoGcC/1hJ7Oa2wOjjLDO AfsbdHVi3Rsh3MHibwT6ECszcmFTX0WmNgI9fZC3Ul9G X-Google-Smtp-Source: AGHT+IFOkq44GFcMOn7QG9c6SF7PVJSMwCGr9GcipmxQmypMM5sjk0yRzvVNg4V9sFDqWdaQexXRBMxeMGtCAPqgF8k= X-Received: by 2002:a67:f950:0:b0:443:5af5:8128 with SMTP id u16-20020a67f950000000b004435af58128mr191900vsq.0.1691704340537; Thu, 10 Aug 2023 14:52:20 -0700 (PDT) MIME-Version: 1.0 References: <73135a17-ed7a-4749-aa6e-75faa1c85077.qijinquan@kaihong.com> In-Reply-To: <73135a17-ed7a-4749-aa6e-75faa1c85077.qijinquan@kaihong.com> From: Patrick Oppenlander Date: Fri, 11 Aug 2023 07:52:09 +1000 Message-ID: To: musl@lists.openwall.com Content-Type: multipart/alternative; boundary="0000000000004863e40602989c31" Subject: Re: [musl] pthread_cond_wait may has a error --0000000000004863e40602989c31 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, 10 Aug 2023, 23:05 =E7=A5=81=E9=87=91=E5=85=A8, wrote: > > Hi, > In my demo, I create 3 threads, the first and the second thread are wait > for the global pthread_cond with different pthread_mutex, the last thread > will call pthread_cond_broadcast. > > When I build without musl, the output like this: > task 3 pthread_cond_broadcast end time 0.000054 > task 2 pthread_cond_wait end time 0.000566 > task 1 pthread_cond_wait end time 0.000612 > task 2 pthread_cond_broadcast end time 1.012233 > task 1 pthread_cond_broadcast end time 1.012247 > > But when I build with static lib of musl(libc.a) and run , the output lik= e > this: > task 3 pthread_cond_broadcast end time 0.000026 > task 1 pthread_cond_wait end time 0.000074 > task 1 pthread_cond_broadcast end time 1.013168 > task 2 pthread_cond_wait end time 1.013245 > task 2 pthread_cond_broadcast end time 2.022992 > > it seems that the second wakeup thead is waiting until the first wakeup > thead unlock the mutex(different mute in the two threads). > > > > > > musl version : 1.2.4 > > pthread_cond_t g_cond =3D PTHREAD_COND_INITIALIZER; > pthread_mutex_t g_mutex1 =3D PTHREAD_MUTEX_INITIALIZER; > pthread_mutex_t g_mutex2 =3D PTHREAD_MUTEX_INITIALIZER; > auto tStart =3D std::chrono::steady_clock::now(); > void* ThreadTaskOne(void* arg) > { > pthread_mutex_lock(&g_mutex1); > pthread_cond_wait(&g_cond, &g_mutex1); > auto tEnd =3D std::chrono::steady_clock::now(); > auto diff =3D std::chrono::duration(tEnd - tStart); > printf("task 1 pthread_cond_wait end time %f\n", diff.count()); > > sleep(1); > pthread_mutex_unlock(&g_mutex1); > tEnd =3D std::chrono::steady_clock::now(); > diff =3D std::chrono::duration(tEnd - tStart); > printf("task 1 pthread_cond_broadcast end time %f\n", diff.count()); > return nullptr; > } > > void* ThreadTaskTwo(void* arg) > { > pthread_mutex_lock(&g_mutex2); > pthread_cond_wait(&g_cond, &g_mutex2); > auto tEnd =3D std::chrono::steady_clock::now(); > auto diff =3D std::chrono::duration(tEnd - tStart); > printf("task 2 pthread_cond_wait end time %f\n", diff.count()); > > sleep(1); > pthread_mutex_unlock(&g_mutex2); > tEnd =3D std::chrono::steady_clock::now(); > diff =3D std::chrono::duration(tEnd - tStart); > printf("task 2 pthread_cond_broadcast end time %f\n", diff.count()); > return nullptr; > } > > void* BroadcastNotifyMutex(void* arg) > { > tStart =3D std::chrono::steady_clock::now(); > pthread_cond_broadcast(&g_cond); > auto tEnd =3D std::chrono::steady_clock::now(); > auto diff =3D std::chrono::duration(tEnd - tStart); > printf("task 3 pthread_cond_broadcast end time %lf\n", diff.count()); > return nullptr; > } > int main() > { > pthread_t threadOne, threadTwo, threadThree; > pthread_create(&threadOne, nullptr, ThreadTaskOne, nullptr); > pthread_create(&threadTwo, nullptr, ThreadTaskTwo, nullptr); > sleep(3); > pthread_create(&threadThree, nullptr, BroadcastNotifyMutex, nullptr); > pthread_join(threadOne, nullptr); > pthread_join(threadTwo, nullptr); > pthread_join(threadThree, nullptr); > return 0; > } > I don't think that's a valid test: https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_cond_wait.html "The effect of using more than one mutex for concurrent *pthread_cond_wait()* or *pthread_cond_timedwait()* operations on the same condition variable is undefined; that is, a condition variable becomes bound to a unique mutex when a thread waits on the condition variable, and this (dynamic) binding ends when the wait returns." Patrick --0000000000004863e40602989c31 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, 10 Aug 2023, 23:05 =E7=A5=81=E9=87=91=E5=85=A8, <qijinquan@kaihong.com> wrote:=

Hi,
In my demo, I create 3 threads, the first and the secon= d thread are wait for the global=C2=A0pthread_cond= with different pthread_mutex, the last thread will call=C2=A0pthread_cond_broadcast.=C2=A0

When I build without musl, the out= put like this:
task 3 pthread_cond_bro= adcast end time 0.000054
task 2 pthread= _cond_wait end time 0.000566
task 1 pthr= ead_cond_wait end time 0.000612
task 2 p= thread_cond_broadcast end time 1.012233
task 1 pthread_cond_= broadcast end time 1.012247

But when I build with static lib of musl(l= ibc.a) and run , the output like this:
task 3 pthread_cond_broadcast end time 0.000026
task 1 pthread_cond_wait end time 0.000074
task 1 pthread_cond_broadcast end time 1.0= 13168
task 2 pthread_cond_wait end time = 1.013245
task 2 pthread_cond_broadcast end time 2.02299= 2

it= seems that the second wakeup thead is waiting until the first wakeup thead= unlock the mutex(different mute in the two threads).





m= usl version : 1.2.4

pthread_cond= _t g_cond =3D PTHREAD_COND_INITIALIZER;
pthread_mutex_t g_mutex1 =3D PTHREAD_MUTEX_INITIALIZE= R;
pthread_mutex_t g_mutex2 =3D = PTHREAD_MUTEX_INITIALIZER;
auto tStart = =3D std::chrono::steady_clock::now();
vo= id* ThreadTaskOne(void* arg)
{
=
=C2=A0 =C2=A0 pthread_mutex_lock(&g_mutex1);<= br>
=C2=A0 =C2=A0 pthread_cond_wait(&g_c= ond, &g_mutex1);
=C2=A0 =C2=A0 auto = tEnd =3D std::chrono::steady_clock::now();
=C2=A0 =C2=A0 auto diff =3D std::chrono::duration<double>(tEnd - t= Start);
=C2=A0 =C2=A0 printf("task = 1 pthread_cond_wait end time %f\n", diff.count());

=C2=A0 =C2=A0 sleep(1);=
=C2=A0 =C2=A0 pthread_mutex_unlock(&= ;g_mutex1);
=C2=A0 =C2=A0 tEnd =3D std::= chrono::steady_clock::now();
=C2=A0 =C2= =A0 diff =3D std::chrono::duration<double>(tEnd - tStart);
<= div style=3D"clear:both">=C2=A0 =C2=A0 printf("task 1 pthread_cond_bro= adcast end time %f\n", diff.count());
=C2=A0 =C2=A0 return nullptr;
}

void* Thr= eadTaskTwo(void* arg)
{
=C2=A0 =C2=A0 pthread_mutex_lock(&g_mutex2);
=C2=A0 =C2=A0 pthread_cond_wait(&g_cond, &a= mp;g_mutex2);
=C2=A0 =C2=A0 auto tEnd = =3D std::chrono::steady_clock::now();
= =C2=A0 =C2=A0 auto diff =3D std::chrono::duration<double>(tEnd - tSta= rt);
=C2=A0 =C2=A0 printf("task 2 p= thread_cond_wait end time %f\n", diff.count());

=C2=A0 =C2=A0 sleep(1);
=C2=A0 =C2=A0 pthread_mutex_unlock(&g_= mutex2);
=C2=A0 =C2=A0 tEnd =3D std::chr= ono::steady_clock::now();
=C2=A0 =C2=A0 = diff =3D std::chrono::duration<double>(tEnd - tStart);
=C2=A0 =C2=A0 printf("task 2 pthread_cond_broadca= st end time %f\n", diff.count());
= =C2=A0 =C2=A0 return nullptr;
}

void* Broadc= astNotifyMutex(void* arg)
{
=C2=A0 =C2=A0 tStart =3D std::chrono::steady_clock::= now();
=C2=A0 =C2=A0 pthread_cond_broadc= ast(&g_cond);
=C2=A0 =C2=A0 auto tEn= d =3D std::chrono::steady_clock::now();
= =C2=A0 =C2=A0 auto diff =3D std::chrono::duration<double>(tEnd - tSta= rt);
=C2=A0 =C2=A0 printf("task 3 p= thread_cond_broadcast end time %lf\n", diff.count());
=C2=A0 =C2=A0 return nullptr;
}
int main()
{
=C2=A0 =C2=A0 pthread= _t threadOne, threadTwo, threadThree;
= =C2=A0 =C2=A0 pthread_create(&threadOne, nullptr, ThreadTaskOne, nullpt= r);
=C2=A0 =C2=A0 pthread_create(&th= readTwo, nullptr, ThreadTaskTwo, nullptr);
=C2=A0 =C2=A0 sleep(3);
=C2=A0 =C2=A0= pthread_create(&threadThree, nullptr, BroadcastNotifyMutex, nullptr);<= br>
=C2=A0 =C2=A0 pthread_join(threadOne, nu= llptr);
=C2=A0 =C2=A0 pthread_join(threa= dTwo, nullptr);
=C2=A0 =C2=A0 pthread_jo= in(threadThree, nullptr);
=C2=A0 =C2=A0 = return 0;
}
=


I don't think that's a valid test:


=
"Th= e effect of using more than one mutex for concurrent=C2=A0pthread_cond_wait()=C2=A0or=C2=A0pthread_cond_timedwait()=C2=A0operations on the same condition variabl= e is undefined; that is, a condition variable becomes bound to a unique mut= ex when a thread waits on the condition variable, and this (dynamic) bindin= g ends when the wait returns."

Patrick
--0000000000004863e40602989c31--