Example: Space location locks in Pthread programs

This example shows a Pthread program that dynamically initializes an integer using a space location lock for synchronization.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/*
Filename: ATEST19.QCSRC
The output of this example is as follows:
 Enter Testcase - LIBRARY/ATEST19
 Hold Lock to prevent access to shared data
 Create/start threads
 Thread 00000000 00000025: Entered
 Wait a bit until we are 'done' with the shared data
 Thread 00000000 00000026: Entered
 Thread 00000000 00000027: Entered
 Unlock shared data
 Wait for the threads to complete, and release their resources
 Thread 00000000 00000025: Start critical section, holding lock
 Thread 00000000 00000025: End critical section, release lock
 Thread 00000000 00000026: Start critical section, holding lock
 Thread 00000000 00000026: End critical section, release lock
 Thread 00000000 00000027: Start critical section, holding lock
 Thread 00000000 00000027: End critical section, release lock
 Main completed
*/
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <mih/milckcom.h>     /* Lock types           */
#include <mih/locksl.h>       /* LOCKSL instruction   */
#include <mih/unlocksl.h>     /* UNLOCKSL instruction */
 
#define checkResults(string, val) {             \
 if (val) {                                     \
   printf("Failed with %d at %s", val, string); \
   exit(1);                                     \
 }                                              \
}
 
#define                 NUMTHREADS   3
int                     sharedData=0;
int                     sharedData2=0;
 
void *theThread(void *parm)
{
   int   rc;
   printf("Thread %.8x %.8x: Entered\n", pthread_getthreadid_np());
   locksl(&sharedData, _LENR_LOCK);   /* Lock Exclusive, No Read */
   /********** Critical Section *******************/
   printf("Thread %.8x %.8x: Start critical section, holding lock\n",
          pthread_getthreadid_np());
   /* Access to shared data goes here */
   ++sharedData; --sharedData2;
   printf("Thread %.8x %.8x: End critical section, release lock\n",
          pthread_getthreadid_np());
   unlocksl(&sharedData, _LENR_LOCK); /* Unlock Exclusive, No Read */
   /********** Critical Section *******************/
   return NULL;
}
 
int main(int argc, char **argv)
{
  pthread_t             thread[NUMTHREADS];
  int                   rc=0;
  int                   i;
 
  printf("Enter Testcase - %s\n", argv[0]);
 
  printf("Hold Lock to prevent access to shared data\n");
  locksl(&sharedData, _LENR_LOCK);   /* Lock Exclusive, No Read */
 
  printf("Create/start threads\n");
  for (i=0; i <NUMTHREADS; ++i) {
     rc = pthread_create(&thread[i], NULL, theThread, NULL);
     checkResults("pthread_create()\n", rc);
  }
 
  printf("Wait a bit until we are 'done' with the shared data\n");
  sleep(3);
  printf("Unlock shared data\n");
  unlocksl(&sharedData, _LENR_LOCK); /* Unlock Exclusive, No Read */
 
  printf("Wait for the threads to complete, and release their resources\n");
  for (i=0; i <NUMTHREADS; ++i) {
 rc = pthread_join(thread[i], NULL);
     checkResults("pthread_join()\n", rc);
  }
 
  printf("Main completed\n");
  return 0;
}