Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Thu, 10 Aug 2023 14:22:01 +0800
From: "祁金全" <qijinquan@...hong.com>
To: "musl" <musl@...ts.openwall.com>
Subject: pthread_cond_wait may has a error

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;
}

Content of type "text/html" skipped

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.