Use of Semaphore

Posted on 1999-01-13
Last Modified: 2010-04-15
How does the CreateSemaphore(), ReleaseSemaphore() and WaitForSingleObject() works? And what are the pararmeters and conditions that are to be put in to implement Semaphore?
Question by:ho_cm

Author Comment

ID: 1255828
I am new in this area.. So please explain in simpler terms! Thanx!

Expert Comment

ID: 1255829
I din't get clearly what do you want especially pertaining to the

functions .Are you using the function names in general sense ? or

with respect to any specific functions as such ( I don't think so as such functions you have named exist or not in terms of system calls ! )

If you want know how to use a semaphore -- I mean starting from creation , release and syncronisation w.r.t given resources,then
I can explain you.Please let me know.

Accepted Solution

940961sl earned 10 total points
ID: 1255830
Those functions are usefull when you want to work with threads. A thread is a copy of a function that runs simultaniously with other treads of that function.
To say it in normal words  : your function works more than once at the same time.
Now lets say your function needs some variables from your main program. how does the main program tell which tread needs the variables, and how do the threads know that the variables are (not) for them??
thanks to semaphores :
1. All the treads have the same parameterlist.
2. Every thread has a unique semaphore, made with createsemaphore.

When the tread has nothing to do, it uses SetEvent to tell the main program. And it waits for a respons from the program using WaitForSingleObject.

3. The main program uses ReleaseSemaphore to tell the tread that the variables in the parameterlist are for him.

Check out the program I once have written to learn working with threads.
the main program starts 100 threads of the function "threadfunctie"
After that, the main program sends random numbers to random treads.

the treads receive the numbers and use them (sleep(number)), after that, they wait for a new number.

#include <memory.h>
#include <malloc.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <time.h>

const int maxthread = 99;
const int maxdelay = 20000;

typedef void *HANDLE;

typedef long              *An_Item_Ptr;
typedef An_Item_Ptr       An_Item_List[];

typedef struct
    int            Number;
    An_Item_List  *Items;
} An_Array;

typedef struct
      HANDLE                  threadhandle;
      HANDLE                  semaphorehandle;
      int                        number;
      int                        Info;
      int                        bussy;
} A_Thread;

typedef A_Thread            * A_Thread_Ptr;

typedef struct
      int            *number;
      int            *info;
      int            *bussy;
      HANDLE      *semaphorehandle;
} A_Parameter_List;

char *GetTime()
      char static ActualTime[12];
      struct _timeb timebuffer;
      div_t result;
      int hun;

      ActualTime[8] = '.';
      result = div(timebuffer.millitm, 10);
      (result.rem > 4 && result.quot < 99 ? hun = result.quot + 1 : hun = result.quot);
      _itoa(hun, &ActualTime[9], 10);
      if (hun < 10) ActualTime[10] = ' ';
      ActualTime[11] = '\0';
      return ActualTime;

void Append_Item ( An_Array * Array)
   An_Item_List    *Item_List;
   int              Current_Index = -1;
   int              Counter;

   if (Array->Number == 0)
      Array->Number = 1;
      Current_Index = Array->Number - 1;
      Item_List = (An_Item_List *) malloc (Array->Number * sizeof(An_Item_Ptr));
      Array->Items = Item_List;
      An_Item_List  *Old_Item_List;
      Item_List = Array->Items;
      Old_Item_List = Item_List;
      Current_Index = Array->Number;
      Item_List = (An_Item_List *) malloc (Array->Number * sizeof(An_Item_Ptr));
      for (Counter = 0; Counter < Array->Number - 1; Counter ++)
         (*Item_List)[Counter] = (*Old_Item_List)[Counter];
      free   (Old_Item_List);
      Array->Items = Item_List;

An_Array            threads;
int                        Job_Finished = 0;

void * threadfunctie(A_Parameter_List * Parameter_List)
      int            *info = Parameter_List->info;
      int            number = *(Parameter_List->number);  //number is altijd zelfde
      int            *bussy = Parameter_List->bussy;
      HANDLE      *semaphorehandle = Parameter_List->semaphorehandle;
      printf("%s Thread %2d : started\n", GetTime(), number);
      *semaphorehandle = CreateSemaphore ( NULL,0,100,NULL);
            WaitForSingleObject(*semaphorehandle, 300000);
            printf("%s Thread %2d : number %d received\n", GetTime(), number, *info);
            *bussy = 0;

void main (void)
      DWORD            Id;
      A_Thread_Ptr      thread;
      A_Parameter_List                  * Parameter_List;
      int r, threadnr, info;
      div_t result;

      printf("%s Program   : started\n", GetTime());
      for (int i = 0; i < maxthread; i++)
            thread = (A_Thread *) malloc (sizeof(A_Thread));
            memset (thread, 0, sizeof (A_Thread));
            (*threads.Items)[i] = (An_Item_Ptr) thread;
            thread->number = i;

            Parameter_List = (A_Parameter_List *)malloc (sizeof(A_Parameter_List));
            Parameter_List->number = &(thread->number);
            Parameter_List->semaphorehandle = &(thread->semaphorehandle);
            Parameter_List->info = &(thread->Info);
            thread->threadhandle=CreateThread (NULL, 32000, (LPTHREAD_START_ROUTINE) &threadfunctie, (LPVOID) Parameter_List, 0, &Id);


            r = rand();
            result = div(r, maxdelay);
            info = result.rem;
            result = div(r, maxthread);
            threadnr = result.rem;

            if (((A_Thread_Ptr) (*threads.Items)[threadnr])->bussy == 0)
                  ((A_Thread_Ptr) (*threads.Items)[threadnr])->Info = info;
                  ((A_Thread_Ptr) (*threads.Items)[threadnr])->bussy = 1;
                  ReleaseSemaphore    (((A_Thread_Ptr) (*threads.Items)[threadnr])->semaphorehandle, 1, NULL );
                  printf("%s Program   : send number %d to thread %d\n", GetTime(), info, threadnr);
                  printf("%s Program   : thread number %2d is bussy\n", GetTime(), threadnr);


hope this helps


Author Comment

ID: 1255831
If you want know how to use a semaphore -- I mean starting from creation , release and syncronisation w.r.t given resources,then
I can explain you.Please let me know...

Yup! Please explain to me in further details ..! thanx!

My application the use of Shared Memory.. to exchange data between applications.. and my client application is ran based on 6 phone lines ..sort of voice automated applications simultaneously... i.e calling the function to send message data... at unpredictable time... So how can it be implemented using semaphore.. thanx!
LVL 11

Expert Comment

ID: 1255832
Using SetEvent() on a semaphore handle?  Wow...

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question