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.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@7669
This commit is contained in:
Bart Van Assche 2008-03-13 17:34:43 +00:00
parent f8635f5a22
commit 36893db5e6
3 changed files with 14 additions and 48 deletions

View File

@ -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 @@ static void set_thread_name(const char* const fmt, const int arg)
}
}
void increment_finished_count()
static void increment_finished_count()
{
pthread_mutex_lock(&s_mutex);
s_finished_count++;
pthread_mutex_unlock(&s_mutex);
}
int get_finished_count()
{
int result;
pthread_mutex_lock(&s_mutex);
result = s_finished_count;
pthread_mutex_unlock(&s_mutex);
return result;
sem_post(&s_sem);
}
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 @@ static void* thread_func2(void* arg)
{
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 @@ int main(int argc, char** argv)
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 @@ int main(int argc, char** argv)
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;
}

View File

@ -1,3 +1 @@
.
.
..

View File

@ -1,21 +1 @@
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
....................