mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] pthread_cond_wait may has a error
@ 2023-08-10  6:22 祁金全
  2023-08-10 14:06 ` Alex Xu (Hello71)
  2023-08-10 21:52 ` Patrick Oppenlander
  0 siblings, 2 replies; 3+ messages in thread
From: 祁金全 @ 2023-08-10  6:22 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 2845 bytes --]

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 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.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 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t g_mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t g_mutex2 = PTHREAD_MUTEX_INITIALIZER;
auto tStart = std::chrono::steady_clock::now();
void* ThreadTaskOne(void* arg)
{
 pthread_mutex_lock(&g_mutex1);
 pthread_cond_wait(&g_cond, &g_mutex1);
 auto tEnd = std::chrono::steady_clock::now();
 auto diff = std::chrono::duration<double>(tEnd - tStart);
 printf("task 1 pthread_cond_wait end time %f\n", diff.count());
 sleep(1);
 pthread_mutex_unlock(&g_mutex1);
 tEnd = std::chrono::steady_clock::now();
 diff = std::chrono::duration<double>(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 = std::chrono::steady_clock::now();
 auto diff = std::chrono::duration<double>(tEnd - tStart);
 printf("task 2 pthread_cond_wait end time %f\n", diff.count());
 sleep(1);
 pthread_mutex_unlock(&g_mutex2);
 tEnd = std::chrono::steady_clock::now();
 diff = std::chrono::duration<double>(tEnd - tStart);
 printf("task 2 pthread_cond_broadcast end time %f\n", diff.count());
 return nullptr;
}
void* BroadcastNotifyMutex(void* arg)
{
 tStart = std::chrono::steady_clock::now();
 pthread_cond_broadcast(&g_cond);
 auto tEnd = std::chrono::steady_clock::now();
 auto diff = std::chrono::duration<double>(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;
}

[-- Attachment #2: Type: text/html, Size: 9355 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [musl] pthread_cond_wait may has a error
  2023-08-10  6:22 [musl] pthread_cond_wait may has a error 祁金全
@ 2023-08-10 14:06 ` Alex Xu (Hello71)
  2023-08-10 21:52 ` Patrick Oppenlander
  1 sibling, 0 replies; 3+ messages in thread
From: Alex Xu (Hello71) @ 2023-08-10 14:06 UTC (permalink / raw)
  To: qijinquan; +Cc: musl

Excerpts from 祁金全's message of August 10, 2023 2:22 am:
> 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

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

https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html:
> When a thread waits on a condition variable, having specified a 
> particular mutex to either the pthread_cond_timedwait() or the 
> pthread_cond_wait() operation, a dynamic binding is formed between 
> that mutex and condition variable that remains in effect as long as at 
> least one thread is blocked on the condition variable. During this 
> time, the effect of an attempt by any thread to wait on that condition 
> variable using a different mutex is undefined.

Cheers,
Alex.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [musl] pthread_cond_wait may has a error
  2023-08-10  6:22 [musl] pthread_cond_wait may has a error 祁金全
  2023-08-10 14:06 ` Alex Xu (Hello71)
@ 2023-08-10 21:52 ` Patrick Oppenlander
  1 sibling, 0 replies; 3+ messages in thread
From: Patrick Oppenlander @ 2023-08-10 21:52 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 3754 bytes --]

On Thu, 10 Aug 2023, 23:05 祁金全, <qijinquan@kaihong.com> 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 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.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 = PTHREAD_COND_INITIALIZER;
> pthread_mutex_t g_mutex1 = PTHREAD_MUTEX_INITIALIZER;
> pthread_mutex_t g_mutex2 = PTHREAD_MUTEX_INITIALIZER;
> auto tStart = std::chrono::steady_clock::now();
> void* ThreadTaskOne(void* arg)
> {
>     pthread_mutex_lock(&g_mutex1);
>     pthread_cond_wait(&g_cond, &g_mutex1);
>     auto tEnd = std::chrono::steady_clock::now();
>     auto diff = std::chrono::duration<double>(tEnd - tStart);
>     printf("task 1 pthread_cond_wait end time %f\n", diff.count());
>
>     sleep(1);
>     pthread_mutex_unlock(&g_mutex1);
>     tEnd = std::chrono::steady_clock::now();
>     diff = std::chrono::duration<double>(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 = std::chrono::steady_clock::now();
>     auto diff = std::chrono::duration<double>(tEnd - tStart);
>     printf("task 2 pthread_cond_wait end time %f\n", diff.count());
>
>     sleep(1);
>     pthread_mutex_unlock(&g_mutex2);
>     tEnd = std::chrono::steady_clock::now();
>     diff = std::chrono::duration<double>(tEnd - tStart);
>     printf("task 2 pthread_cond_broadcast end time %f\n", diff.count());
>     return nullptr;
> }
>
> void* BroadcastNotifyMutex(void* arg)
> {
>     tStart = std::chrono::steady_clock::now();
>     pthread_cond_broadcast(&g_cond);
>     auto tEnd = std::chrono::steady_clock::now();
>     auto diff = std::chrono::duration<double>(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

[-- Attachment #2: Type: text/html, Size: 10519 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-10 21:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-10  6:22 [musl] pthread_cond_wait may has a error 祁金全
2023-08-10 14:06 ` Alex Xu (Hello71)
2023-08-10 21:52 ` Patrick Oppenlander

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).