aboutsummaryrefslogtreecommitdiff
path: root/linuxthreads/Examples/ex4.c
diff options
context:
space:
mode:
authorAndreas Jaeger <aj@suse.de>2000-12-27 17:14:56 +0000
committerAndreas Jaeger <aj@suse.de>2000-12-27 17:14:56 +0000
commita375a533a2445079390907b962464a379d25f5d5 (patch)
treed781d06615c1d76d029c48e1d7eb69dc21ecbf3d /linuxthreads/Examples/ex4.c
parent21112857299d8a7b9c04fcb2027ae5186acfcf4f (diff)
downloadglibc-a375a533a2445079390907b962464a379d25f5d5.tar
glibc-a375a533a2445079390907b962464a379d25f5d5.tar.gz
glibc-a375a533a2445079390907b962464a379d25f5d5.tar.bz2
glibc-a375a533a2445079390907b962464a379d25f5d5.zip
* Examples/ex13.c: Make local functions static.
* ecmutex.c: Likewise. * Examples/ex14.c: Likewise. * Examples/ex2.c: Make local functions static; reformat. * Examples/ex1.c: Likewise. * Examples/ex4.c: Likewise. * Examples/ex5.c: Likewise. * Examples/ex7.c: Likewise. CVS ----------------------------------------------------------------------
Diffstat (limited to 'linuxthreads/Examples/ex4.c')
-rw-r--r--linuxthreads/Examples/ex4.c84
1 files changed, 46 insertions, 38 deletions
diff --git a/linuxthreads/Examples/ex4.c b/linuxthreads/Examples/ex4.c
index 86584de70d..5c8b929e22 100644
--- a/linuxthreads/Examples/ex4.c
+++ b/linuxthreads/Examples/ex4.c
@@ -14,10 +14,11 @@
#if 0
-char * str_accumulate(char * s)
+char *
+str_accumulate (char *s)
{
static char accu[1024] = { 0 };
- strcat(accu, s);
+ strcat (accu, s);
return accu;
}
@@ -35,73 +36,80 @@ static pthread_key_t str_key;
static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT;
/* Forward functions */
-static void str_alloc_key(void);
-static void str_alloc_destroy_accu(void * accu);
+static void str_alloc_key (void);
+static void str_alloc_destroy_accu (void *accu);
/* Thread-safe version of str_accumulate */
-char * str_accumulate(const char * s)
+static char *
+str_accumulate (const char *s)
{
- char * accu;
+ char *accu;
/* Make sure the key is allocated */
- pthread_once(&str_alloc_key_once, str_alloc_key);
+ pthread_once (&str_alloc_key_once, str_alloc_key);
/* Get the thread-specific data associated with the key */
- accu = (char *) pthread_getspecific(str_key);
+ accu = (char *) pthread_getspecific (str_key);
/* It's initially NULL, meaning that we must allocate the buffer first. */
- if (accu == NULL) {
- accu = malloc(1024);
- if (accu == NULL) return NULL;
- accu[0] = 0;
- /* Store the buffer pointer in the thread-specific data. */
- pthread_setspecific(str_key, (void *) accu);
- printf("Thread %lx: allocating buffer at %p\n", pthread_self(), accu);
- }
+ if (accu == NULL)
+ {
+ accu = malloc (1024);
+ if (accu == NULL)
+ return NULL;
+ accu[0] = 0;
+ /* Store the buffer pointer in the thread-specific data. */
+ pthread_setspecific (str_key, (void *) accu);
+ printf ("Thread %lx: allocating buffer at %p\n", pthread_self (), accu);
+ }
/* Now we can use accu just as in the non thread-safe code. */
- strcat(accu, s);
+ strcat (accu, s);
return accu;
}
/* Function to allocate the key for str_alloc thread-specific data. */
-static void str_alloc_key(void)
+static void
+str_alloc_key (void)
{
- pthread_key_create(&str_key, str_alloc_destroy_accu);
- printf("Thread %lx: allocated key %d\n", pthread_self(), str_key);
+ pthread_key_create (&str_key, str_alloc_destroy_accu);
+ printf ("Thread %lx: allocated key %d\n", pthread_self (), str_key);
}
/* Function to free the buffer when the thread exits. */
/* Called only when the thread-specific data is not NULL. */
-static void str_alloc_destroy_accu(void * accu)
+static void
+str_alloc_destroy_accu (void *accu)
{
- printf("Thread %lx: freeing buffer at %p\n", pthread_self(), accu);
- free(accu);
+ printf ("Thread %lx: freeing buffer at %p\n", pthread_self (), accu);
+ free (accu);
}
/* Test program */
-void * process(void * arg)
+static void *
+process (void *arg)
{
- char * res;
- res = str_accumulate("Result of ");
- res = str_accumulate((char *) arg);
- res = str_accumulate(" thread");
- printf("Thread %lx: \"%s\"\n", pthread_self(), res);
+ char *res;
+ res = str_accumulate ("Result of ");
+ res = str_accumulate ((char *) arg);
+ res = str_accumulate (" thread");
+ printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
return NULL;
}
-int main(int argc, char ** argv)
+int
+main (int argc, char **argv)
{
- char * res;
+ char *res;
pthread_t th1, th2;
- res = str_accumulate("Result of ");
- pthread_create(&th1, NULL, process, (void *) "first");
- pthread_create(&th2, NULL, process, (void *) "second");
- res = str_accumulate("initial thread");
- printf("Thread %lx: \"%s\"\n", pthread_self(), res);
- pthread_join(th1, NULL);
- pthread_join(th2, NULL);
+ res = str_accumulate ("Result of ");
+ pthread_create (&th1, NULL, process, (void *) "first");
+ pthread_create (&th2, NULL, process, (void *) "second");
+ res = str_accumulate ("initial thread");
+ printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
+ pthread_join (th1, NULL);
+ pthread_join (th2, NULL);
return 0;
}