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

static void wait(void)
{
    time_t start_time = time(NULL);
 
    while (time(NULL) == start_time)
    {
        /* do nothing except chew CPU slices for up to one second */
    }
}

static void *thread_func(void* vptr_args)
{
    char string[] = "Second thread!\n";
    int len = sizeof(string);
    int i;

    for(i=0; i<len; i++)
    {
        putchar(string[i]);
        wait();
    }
}

int main()
{
    char string[] = "Main thread!\n";
    int len = sizeof(string);
    pthread_t thread;
    int i;
    // Start second thread
    if (pthread_create(&thread, NULL, thread_func, NULL) != 0)
    {
        return EXIT_FAILURE;
    }

    for(i=0; i<len; i++)
    {
        putchar(string[i]);
        wait();
    }

    // Wait for second thread to finish execution
    if (pthread_join(thread, NULL) != 0)
    {
        return EXIT_FAILURE;
    }

    return 0;
}
