threads.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 12:58:26 +0100
changeset 0 af501b0c1716
child 8 96d16dfe787a
permissions -rw-r--r--
demos cvs copy committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 *     $Id: threads.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $
 * $Source: /development/c/demos/threads.c,v $
 */

#include <stdio.h>
#include <stdlib.h>

#include <pthread.h>
#include <time.h>

void *thread_func (void *vptr_args);

void do_some_work (void)
{
    time_t start_time = time (NULL);

    while (time (NULL) == start_time);  /* busy-wait for 0-1 seconds */
}

int main (int argc, char **argv)
{
    int i;
    pthread_t thread;

    pthread_create (&thread, NULL, &thread_func, NULL);

    for (i = 0; i < 20; ++i) {
        fprintf (stdout, "a\n");

        do_some_work ();
    }

    pthread_join (thread, NULL);

    exit (EXIT_SUCCESS);
}

void *thread_func (void *vptr_args)
{
    int i;

    for (i = 0; i < 20; ++i) {
        fprintf (stderr, "  b\n");

        do_some_work ();
    }
    return NULL;
}