|
From: <sv...@va...> - 2008-03-13 17:36:46
|
Author: bart
Date: 2008-03-13 17:34:43 +0000 (Thu, 13 Mar 2008)
New Revision: 7669
Log:
The dots (one per detached thread) are again all printed on one line instead of one per line. Changed termination test from a counter protected by a mutex to a counting semaphore.
Modified:
trunk/exp-drd/tests/pth_detached.c
trunk/exp-drd/tests/pth_detached.stdout.exp
trunk/exp-drd/tests/pth_detached2.stdout.exp
Modified: trunk/exp-drd/tests/pth_detached.c
===================================================================
--- trunk/exp-drd/tests/pth_detached.c 2008-03-13 17:32:41 UTC (rev 7668)
+++ trunk/exp-drd/tests/pth_detached.c 2008-03-13 17:34:43 UTC (rev 7669)
@@ -5,15 +5,15 @@
#include <assert.h>
#include <pthread.h>
+#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "../drd_clientreq.h"
-static int s_finished_count;
-static int s_set_thread_name;
-static pthread_mutex_t s_mutex;
+static int s_set_thread_name;
+static sem_t s_sem;
static void set_thread_name(const char* const fmt, const int arg)
@@ -29,26 +29,15 @@
}
}
-void increment_finished_count()
+static void increment_finished_count()
{
- pthread_mutex_lock(&s_mutex);
- s_finished_count++;
- pthread_mutex_unlock(&s_mutex);
+ sem_post(&s_sem);
}
-int get_finished_count()
-{
- int result;
- pthread_mutex_lock(&s_mutex);
- result = s_finished_count;
- pthread_mutex_unlock(&s_mutex);
- return result;
-}
-
static void* thread_func1(void* arg)
{
set_thread_name("thread_func1[%d]", *(int*)arg);
- write(STDOUT_FILENO, ".\n", 2);
+ write(STDOUT_FILENO, ".", 1);
increment_finished_count();
return 0;
}
@@ -57,7 +46,7 @@
{
set_thread_name("thread_func2[%d]", *(int*)arg);
pthread_detach(pthread_self());
- write(STDOUT_FILENO, ".\n", 2);
+ write(STDOUT_FILENO, ".", 1);
increment_finished_count();
return 0;
}
@@ -79,7 +68,7 @@
for (i = 0; i < count1 || i < count2; i++)
thread_arg[i] = i;
- pthread_mutex_init(&s_mutex, 0);
+ sem_init(&s_sem, 0, 0);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
@@ -105,15 +94,14 @@
pthread_attr_destroy(&attr);
// Wait until all detached threads have written their output to stdout.
- while (get_finished_count() < count1 + count2)
+ for (i = 0; i < count1 + count2; i++)
{
- struct timespec delay = { 0, 1 * 1000 * 1000 };
- nanosleep(&delay, 0);
+ sem_wait(&s_sem);
}
- printf("\n");
+ write(STDOUT_FILENO, "\n", 1);
- pthread_mutex_destroy(&s_mutex);
+ sem_destroy(&s_sem);
return 0;
}
Modified: trunk/exp-drd/tests/pth_detached.stdout.exp
===================================================================
--- trunk/exp-drd/tests/pth_detached.stdout.exp 2008-03-13 17:32:41 UTC (rev 7668)
+++ trunk/exp-drd/tests/pth_detached.stdout.exp 2008-03-13 17:34:43 UTC (rev 7669)
@@ -1,3 +1 @@
-.
-.
-
+..
Modified: trunk/exp-drd/tests/pth_detached2.stdout.exp
===================================================================
--- trunk/exp-drd/tests/pth_detached2.stdout.exp 2008-03-13 17:32:41 UTC (rev 7668)
+++ trunk/exp-drd/tests/pth_detached2.stdout.exp 2008-03-13 17:34:43 UTC (rev 7669)
@@ -1,21 +1 @@
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-.
-
+....................
|