numeric-linalg

Educational material on the SciPy implementation of numerical linear algebra algorithms

progress-bar.h (929B)

 1 #ifndef PROGRESS_BAR_H_
 2 #define PROGRESS_BAR_H_
 3 
 4 #include <stdio.h>
 5 #include <stdint.h>
 6 #include <pthread.h>
 7 
 8 #define PROGRESS_BAR_LENGTH 50
 9 
10 size_t progress_last_printed_count = 0;
11 size_t progress_count = 0;
12 pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER;
13 
14 void progress_bar_inc(void)
15 {
16   pthread_mutex_lock(&progress_mutex);
17 
18   progress_count++;
19   size_t filled_length = (PROGRESS_BAR_LENGTH*progress_count)/PROGRESS_BAR_TOTAL;
20   size_t empty_length = PROGRESS_BAR_LENGTH - filled_length;
21 
22   printf("\r[");
23   for (size_t i = 0; i < filled_length; i++) printf("=");
24   for (size_t i = 0; i < empty_length; i++)  printf(" ");
25 
26   size_t percent = (100 * progress_count) / PROGRESS_BAR_TOTAL;
27   if (percent == 100) printf("] %3zu%%\n", percent);
28   else printf("] %3zu%%", percent);
29 
30   progress_last_printed_count = progress_count;
31 
32   fflush(stdout);
33   pthread_mutex_unlock(&progress_mutex);
34 }
35 
36 #endif // PROGRESS_BAR_H_