/* alarm3.cpp an alarm example using multiple threads */ #include #include #include #include #include typedef struct alarm_tag { int seconds; char message[64]; } alarm_t; void * alarm_thread( void * ); int main(int argc, char *argv[]) { char line[128]; alarm_t *alarm; pthread_t thread; int status; while(1) { printf("Alarm>"); if (fgets(line, sizeof (line), stdin) == NULL) exit(0); if (strlen (line) <=1) continue; alarm = (alarm_t *)malloc(sizeof(alarm_t)); if ( alarm == NULL ) { printf("malloc error\n"); exit(1); } /* parse input line into seconds (%d) and a message * (%64[^\n]), consisting of up to 64 characters * separated from the seconds by whitespace. */ if (sscanf(line, "%d %64[^\n]", &alarm->seconds, alarm->message ) <2 ) { fprintf( stderr, "Bad Command\n"); free( alarm ); } else { status = pthread_create ( &thread, NULL, alarm_thread, alarm ); if ( status !=0 ) { printf("create alarm thread error!\n"); exit(1); } } } } void *alarm_thread(void *arg) { alarm_t *alarm = (alarm_t *) arg; int status; status = pthread_detach(pthread_self()); if (status != 0) { printf(" Detach thread error. \n"); pthread_exit(NULL); } sleep( alarm->seconds); printf("(%d) %s\n", alarm->seconds, alarm->message); free(alarm); return NULL; }