JMU
Thread-Local Storage
in Pthreads


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Overview
Usage
An Example Using Thread-Local Storage
unixexamples/threadspecificdata/grader_threadlocal.c
        #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;
}