-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatch.c
executable file
·57 lines (43 loc) · 976 Bytes
/
stopwatch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* stopwatch_timer.c */
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
static clock_t _current_time = 0;
/* start the timing in another thread */
void* g_start_timer(void *secs)
{
//printf("Starting thread\n");
int *seconds = (int*)secs;
_current_time = clock() + (*seconds) * CLOCKS_PER_SEC;
/* loop until the 10 seconds has reached */
while(clock() < _current_time)
{
}
pthread_exit(NULL);
}
/* create the thread */
void start_timer(int seconds)
{
pthread_t thread_id;
int *secs;
*secs=seconds;
int rc = 0;
rc = pthread_create(&thread_id, NULL, g_start_timer, (void*) secs);
pthread_join(thread_id,NULL);
if(rc)
{
printf("=== Error Creating thread\n");
}
}
/*
int main()
{
printf("Start your timers\n");
//start the time and run for 5 seconds
start_timer(5);
//Check and display current time
printf("Timer over\n");
return 0;
}
*/