???????????????????????????????????????sleep()???????????????????????????????????????Ч??????????????????????

????1??usleep

?????????????????????????????????????????????????????????????????????????????????????????

????2??select

????????????????ù????????????????????

????3??pthread_cond_timedwait

????????pthread_cond_timedwait(pthread_cond_t* cond?? pthread_mutex_t *mutex?? const struct timespec *abstime)???????????????????????????????cond???????????????????????????????cond?????????????????????????????????demo??

?????????????δ???thr_fn????????????

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

int flag = 1;
void * thr_fn(void * arg) {
  while (flag){
    printf("****** ");
    sleep(10);
  }
  printf("sleep test thread exit ");
}
 
int main() {
  pthread_t thread;
  if (0 != pthread_create(&thread?? NULL?? thr_fn?? NULL)) {
    printf("error when create pthread??%d "?? errno);
    return 1;
  }
 
  char c ;
  while ((c = getchar()) != 'q');
 
  printf("Now terminate the thread! ");
  flag = 0;
  printf("Wait for thread to exit ");
  pthread_join(thread?? NULL);
  printf("Bye ");
  return 0;
}
 


????????q???????????sleep??????????????????????????????????????10s???????join??????sleep???????????????????