threads.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:57:54 +0100
changeset 3 820ed7fb9314
parent 0 af501b0c1716
child 8 96d16dfe787a
permissions -rw-r--r--
database, gauss, lotto, mem2swap, prog_limit moved to demos 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;
}