// // child.c // two threads and they are competing the console io ! // when u try input, the output thread suspends. // // #include <conio.h> #include <iostream> #include <windows.h> #include <process.h> // needed for _beginthread() void silly( void * ); // function prototype void silly2( void * ); using namespace std; HANDLE hMutex; DWORD exitCode; int main() { printf( "in the main() function./n" ); hMutex = CreateMutex( NULL, FALSE, NULL );//create Mutex Objects!! sharing io need it! HANDLE hThread=(HANDLE)_beginthread( silly, 0, (void*)12 ); HANDLE hThread2=(HANDLE)_beginthread( silly2, 0,(void*)9); // GetExitCodeThread(hThread, &exitCode); //if ( exitCode != STILL_ACTIVE ) // return 0; WaitForSingleObject(hThread,INFINITE);// WaitForSingleObject(hThread2,INFINITE);// system("pause"); return 0; } void silly( void *arg ) { int i=0; bool flag = true; char unch; string a; printf( "in the silly() function./n" ); while(flag) if(_kbhit()) { WaitForSingleObject( hMutex, INFINITE ); i++; char word[10]; cin >> word; cout<<"your input is :" <<word; cout <<'/n'<<i << '/r';//show increasing no. cout.flush(); //force to flush output buffer ReleaseMutex( hMutex ); } _endthread(); } void silly2( void *arg ) { printf( "in the silly2() function./n" ); //int i=0; for(int i=0;i<10000;i++){ WaitForSingleObject( hMutex, INFINITE ); //cout<<"The silly() function was passed" <<(INT_PTR)arg << '/r'<<'/n'; cout<<"The silly2() function was passed" <<i << '/r'<<'/n'; cout.flush(); ReleaseMutex( hMutex ); Sleep(1010); } _endthread(); }