Here is a simple program using pthreads and controlling access to a common global array (string).

Without the mutex, the threads would alter the string at the same time and create undesired results.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  // create a global initializer
char GlobalString[] = "Hello World";

void* ThreadFunc (void *num)
int j;
int i= *(int*)num;  // function accepts a pointer to any type 
                    //  but we need to cast it to an int
for (j = 1; j<i; j++)
sleep (rand() % 3); // wait 0 to 2 seconds
pthread_mutex_lock (&mutex); // lock the mutex
if (i == 10)
GlobalString[0]='a';  // change some letters in the string
GlobalString[0]='b'; // change letters to something else.
printf ("%s\n", GlobalString);
pthread_mutex_unlock (&mutex);  // release variable

int main ()
int i = 10;
pthread_t tid1, tid2;
pthread_mutex_init (&mutex, NULL); // use the global initializer to start to create a thread
pthread_create (&tid1, NULL, ThreadFunc, &i); // create a thread to run our thread function, pass it the value 10
int j = 13;
pthread_create (&tid2, NULL, ThreadFunc, &j); // create a nother thread with value 13

// pthread_join (tid1, NULL);  // wait for thread to finish before continuing, not nec.
// pthread_join (tid2, NULL);

return 0;