correct mutext test
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 20 Oct 2025 15:57:35 +0000 (17:57 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 20 Oct 2025 15:57:35 +0000 (17:57 +0200)
mutex.c

diff --git a/mutex.c b/mutex.c
index a73e63a9e3b232e96dee8e32e564ee43eccebb58..c399505c413a877c16335dedad277a475a69653f 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -19,6 +19,7 @@ void (*usage_ext) (FILE *) = NULL;
 int (*parse_arg_ext) (char *) = NULL;
 
 pthread_barrier_t *barrier = NULL;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 pthread_mutex_t test = PTHREAD_MUTEX_INITIALIZER;
 volatile int shared_flag = 0;
 ts_t ts1, ts2;
@@ -32,15 +33,23 @@ void *ping (__attribute__((unused)) void *arg)
 {
 
     pthread_barrier_wait (barrier);
+    printf ("Sending ping...\n");
+
+    pthread_mutex_lock (&test);
+    usleep (TIMER);
+
+    for (int i = 0; i < nb_measurements; i++) {
+
+        pthread_mutex_lock (&mutex);
+        pthread_mutex_unlock (&mutex);
+
+        usleep (TIMER / 2);
 
-    while (try < nb_measurements) {
-        pthread_mutex_lock (&test);
-        usleep (TIMER);
-        shared_flag = 1;
-        sys_timestamp (&ts1);
         pthread_mutex_unlock (&test);
-        //shared_flag = 0;
-        usleep (TIMER);
+        sys_timestamp (&ts1);
+
+        usleep (TIMER/2);
+        pthread_mutex_lock (&test);
     }
 
     pthread_exit (NULL);
@@ -51,19 +60,15 @@ void *pong (__attribute__((unused)) void *arg)
 
     pthread_barrier_wait (barrier);
 
-    while (try < nb_measurements) {
-        ts_t ts;
-        sys_timestamp (&ts);
+    for (int i = 0; i < nb_measurements; i++) {
+
+        pthread_mutex_lock (&mutex);
+        usleep (TIMER);
+        pthread_mutex_unlock (&mutex);
+
         pthread_mutex_lock (&test);
-        if (shared_flag) {
-            sys_timestamp (&ts2);
-            if (diff_timestamp (&ts2, &ts) < (9 * TIMER / 10)) {
-                nbtoofast++;
-            } else {
-                deltas[try++] = diff_timestamp (&ts2, &ts1);
-            }
-            shared_flag = 0;
-        }
+        sys_timestamp (&ts2);
+        deltas[i] = diff_timestamp (&ts2, &ts1);
         pthread_mutex_unlock (&test);
     }