FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Database
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
January 01, 2003

Multithreaded Programming with the Command Pattern

(Page 3 of 6)
Multithreaded Programming with the Command Pattern

Listing 2 Thread synchronization using mutex variables



#include <windows.h>

#include <iostream>

HANDLE mutex;

void thread_entry ()
{
  WaitForSingleObject ( mutex, INFINITE );
  for ( int i = 10; i > 0; --i )
  {
    Sleep ( 200 );
    std::cout << "world" << std::endl;
  }
  ReleaseMutex ( mutex );
}

int main ()
{
  mutex = CreateMutex ( 0, NULL, 0 );
  HANDLE thread = CreateThread ( NULL, 0,
                                 ( LPTHREAD_START_ROUTINE ) thread_entry,
                                 NULL, 0, NULL );


  WaitForSingleObject ( mutex, INFINITE );
  for ( int i = 10; i > 0; --i )
  {
    Sleep ( 100 );
    std::cout << "hello" << std::endl;
  }
  ReleaseMutex ( mutex );

  WaitForSingleObject ( thread, INFINITE );

  return ( 0 );
}

Previous Page | 1 | 2 | 3 | 4 | 5 | 6 Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK