Thread-Local Storage
in Pthreads |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
__thread
specifier
immediately after the static
or extern
specifier (note that there are two underscores)#include "grader.h" #include <pthread.h> #include <string.h> static __thread char letter[2]; char *letter_grade(float number_grade) { if (number_grade >= 90.0) strncpy(letter, "A", 1); else if (number_grade >= 80.0) strncpy(letter, "B", 1); else if (number_grade >= 70.0) strncpy(letter, "C", 1); else if (number_grade >= 60.0) strncpy(letter, "D", 1); else strncpy(letter, "F", 1); letter[1] = '\0'; return letter; }