title: Windows Thread Synchronization with create semaphore
body: <p>I am trying on a problem with writer and reader. I am trying with windows semaphore functionality.</p>
  
  <p>It is very simple as follows</p>
  
  <pre><code>      char n[200];
        volatile HANDLE hSem=NULL; // handle to semaphore
  </code></pre>
  
  <p>The write function for console. Which release the semaphore for the read process.  </p>
  
  <pre><code>     void * write_message_function ( void *ptr )
        {
  
  
        /* do the work */
        while(1){
              printf("Enter a string");
              scanf("%s",n);
  
              ReleaseSemaphore(hSem,1,NULL); // unblock all the threads
          }
  
        pthread_exit(0); /* exit */
        } 
  </code></pre>
  
  <p>The print message wait for the release from the write message to print the message.</p>
  
  <pre><code>      void * print_message_function ( void *ptr )
        {
  
  
         while(1){
              WaitForSingleObject(hSem,INFINITE);
              printf("The string entered is :");
              printf("==== %s\n",n);
  
           }
  
         pthread_exit(0); /* exit */
        } 
  </code></pre>
  
  <p>The main function launch the application.</p>
  
  <pre><code>     int main(int argc, char *argv[])
       {
  
       hSem=CreateSemaphore(NULL,0,1,NULL);
       pthread_t thread1, thread2;  /* thread variables */
       /* create threads 1 and 2 */    
       pthread_create (&amp;thread1, NULL, print_message_function, NULL);
       pthread_create (&amp;thread2, NULL, write_message_function, NULL);
  
       pthread_join(thread1, NULL);
       pthread_join(thread2, NULL);
  
       /* exit */  
       CloseHandle(hSem); 
  
       }
  </code></pre>
  
  <p>The program executes but does not show the string input console.</p>