日期:2014-05-16 浏览次数:20717 次
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>
static int thread_exit = 0; /* running state of a thread */
void *showtime(void *arg)
{ /* this is a long-time test */
int *i = arg;
time_t p;
thread_exit = 0;
while (--(*i) > 0) {
printf("---%d---\n", *i);
time(&p);
printf("%s", ctime(&p));
sleep(1);
}
thread_exit = 1;
return ((void *)0);
}
int main(int argc, char **argv)
{
pthread_t tid;
pthread_attr_t attr;
void *tret;
char cmd[16] = { 0 };
int err, times = 10; /* default: test costs 10 secs */
int flag;
/* Use pthread */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
err = pthread_create(&tid, NULL, showtime, ×); /* showtime is a long-time test */
if (err != 0) {
perror("Can't create thread.\n");
exit(1);
}
/* pthread_join(tid,&tret); *//* wait thread to terminate, pthread_join will block the main thread, so we gi