dev/cpp/multithreading/pthread_ex.c

66 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
static void wait_thread(void)
{
time_t start_time = time(NULL);
while (time(NULL) == start_time)
{
/* do nothing except chew CPU slices for up to one second. */
//usleep(1);
//~ struct timespec
//~ {
//~ time_t tv_sec; /* секунды */
//~ long tv_nsec; /* наносекунды */
//~ };
//~ struct timespec ts;
//~ ts.tv_sec = 0;
//~ ts.tv_nsec = 10;
//~ nanosleep(&ts,NULL);
}
}
static void *thread_func(void *vptr_args)
{
int i;
for (i = 0; i < 10; i++)
{
fputs(" b\n", stderr);
wait_thread();
}
return NULL;
}
int main(void)
{
int i;
pthread_t thread;
if (pthread_create(&thread, NULL, thread_func, NULL) != 0)
{
return EXIT_FAILURE;
}
for (i = 0; i < 10; i++)
{
puts("a");
wait_thread();
}
if (pthread_join(thread, NULL) != 0)
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}