Thread problem

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
   hello, i want to allow one thread to print to a file then sleep for 2
seconds and give control to another thread, i wrote the code but it did
n t work,i didnt know the forum rules earlier,thts why i didnt post code ,here is the code <blockquote><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" align=left cellpadding=0 cellspacing=0><tbody><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman #include "stdafx.h" </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman #include "stdafx.h" </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman #include </font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman fstream</font><font style="color:blue;font-size:14px" face="Times New Roman ></font><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman #include </font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman iostream</font><font style="color:blue;font-size:14px" face="Times New Roman ></font><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman #include </font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman windows.h</font><font style="color:blue;font-size:14px" face="Times New Roman ></font><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman #include </font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman time.h</font><font style="color:blue;font-size:14px" face="Times New Roman ></font><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman using namespace std; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman HANDLE hEvent1 , hEvent2; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman //thread function declaration </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman void WriteToFile1(); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman int </font><font style="color:red;font-size:14px" face="Times New Roman flag</font><font style="font-size:14px" face="Times New Roman  =</font><font style="color:blue;font-size:14px" face="Times New Roman 1</font><font style="font-size:14px" face="Times New Roman ; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman int _tmain(int argc, _TCHAR* argv[]) </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman { </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     DWORD threadid[2],flag; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     HANDLE h1[2]; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     </font><font style="color:red;font-size:14px" face="Times New Roman hEvent1</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman CreateEvent</font><font style="font-size:14px" face="Times New Roman (NULL,TRUE,FALSE,NULL); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     </font><font style="color:red;font-size:14px" face="Times New Roman hEvent2</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman CreateEvent</font><font style="font-size:14px" face="Times New Roman (NULL,TRUE,FALSE,NULL); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     SetEvent(hEvent1);  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     SetEvent(hEvent2); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     h1[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteToFile1, NULL , CREATE_SUSPENDED, &threadid[0]); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     h1[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteToFile1, NULL, CREATE_SUSPENDED, &threadid[1]); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     ::ResumeThread(h1[0]); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     ::ResumeThread(h1[1]); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     //dont let this main  thread  die. </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     while(1) </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     { </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         </font><font style="color:red;font-size:14px" face="Times New Roman flag</font><font style="font-size:14px" face="Times New Roman =::WaitForMultipleObjects(2,h1,TRUE,INFINITE); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     } </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     ::CloseHandle(h1[0]); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     ::CloseHandle(h1[1]); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     return 0; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman } </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman // thread function definition </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman void WriteToFile1( ) </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman {    </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     while(1) </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     { </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman         if(flag) </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         { </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             ::WaitForSingleObject(hEvent2,INFINITE); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             ::ResetEvent(hEvent2); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             struct tm *current; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             time_t now; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             time(&now); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             //now contents current  time </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             //convert current time to local time </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             </font><font style="color:red;font-size:14px" face="Times New Roman current</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman localtime</font><font style="font-size:14px" face="Times New Roman (&now); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             //cout</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             //cout</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"i am inside the shared resource i.e a common filen"; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             cout</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"current thread is:"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <::GetCurrentThreadId()</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"tHH:MIN:</font><font style="color:red;font-size:14px" face="Times New Roman SEC</font><font style="font-size:14px" face="Times New Roman =t"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"n"; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             fstream file; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             file.open("c:\common.txt", fstream::out | fstream::app); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             file</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"</font><font style="color:red;font-size:14px" face="Times New Roman thread_id</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman "<<::GetCurrentThreadId()<<"</font><font style="font-size:14px" face="Times New Roman tHH:MIN:</font><font style="color:red;font-size:14px" face="Times New Roman SEC</font><font style="font-size:14px" face="Times New Roman =t"; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             file</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"n"; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             file.close(); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             ::SetEvent(hEvent1); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             </font><font style="color:red;font-size:14px" face="Times New Roman flag</font><font style="font-size:14px" face="Times New Roman =</font><font style="color:blue;font-size:14px" face="Times New Roman 0</font><font style="font-size:14px" face="Times New Roman ; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             Sleep(2000); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         else </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman         { </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             ::WaitForSingleObject(hEvent1,INFINITE); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             ::ResetEvent(hEvent1); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             struct tm *current; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             time_t now; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             time(&now); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             //now contents current  time </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             //convert current time to local time </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             </font><font style="color:red;font-size:14px" face="Times New Roman current</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman localtime</font><font style="font-size:14px" face="Times New Roman (&now); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             cout</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"current thread is:"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <::GetCurrentThreadId()</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"tHH:MIN:</font><font style="color:red;font-size:14px" face="Times New Roman SEC</font><font style="font-size:14px" face="Times New Roman =t"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"n"; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             fstream file; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             file.open("c:\common.txt", fstream::out | fstream::app); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             file</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"</font><font style="color:red;font-size:14px" face="Times New Roman thread_id</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman "<<::GetCurrentThreadId()<<"</font><font style="font-size:14px" face="Times New Roman tHH:MIN:</font><font style="color:red;font-size:14px" face="Times New Roman SEC</font><font style="font-size:14px" face="Times New Roman =t"; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             file</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"n"; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             file.close(); </font><font style="color:red;font-size:14px" face="Times New Roman flag</font><font style="font-size:14px" face="Times New Roman =</font><font style="color:blue;font-size:14px" face="Times New Roman 1</font><font style="font-size:14px" face="Times New Roman ; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             ::SetEvent(hEvent2); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman } </font></td></tr></tbody></table></blockquote><blockquote><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" align=left cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap <tbody><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman #include "stdafx.h" </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman #include "stdafx.h" </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman #include </font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman fstream</font><font style="color:blue;font-size:14px" face="Times New Roman ></font><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman #include </font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman iostream</font><font style="color:blue;font-size:14px" face="Times New Roman ></font><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman #include </font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman windows.h</font><font style="color:blue;font-size:14px" face="Times New Roman ></font><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman #include </font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman time.h</font><font style="color:blue;font-size:14px" face="Times New Roman ></font><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman using namespace std; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman HANDLE hEvent1 , hEvent2; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman //thread function declaration </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman void WriteToFile1(); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman int </font><font style="color:red;font-size:14px" face="Times New Roman flag</font><font style="font-size:14px" face="Times New Roman  =</font><font style="color:blue;font-size:14px" face="Times New Roman 1</font><font style="font-size:14px" face="Times New Roman ; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman int _tmain(int argc, _TCHAR* argv[]) </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman { </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     DWORD threadid[2],flag; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     HANDLE h1[2]; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     </font><font style="color:red;font-size:14px" face="Times New Roman hEvent1</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman CreateEvent</font><font style="font-size:14px" face="Times New Roman (NULL,TRUE,FALSE,NULL); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     </font><font style="color:red;font-size:14px" face="Times New Roman hEvent2</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman CreateEvent</font><font style="font-size:14px" face="Times New Roman (NULL,TRUE,FALSE,NULL); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     SetEvent(hEvent1);  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     SetEvent(hEvent2); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     h1[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteToFile1, NULL , CREATE_SUSPENDED, &threadid[0]); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     h1[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteToFile1, NULL, CREATE_SUSPENDED, &threadid[1]); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     ::ResumeThread(h1[0]); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     ::ResumeThread(h1[1]); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     //dont let this main  thread  die. </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     while(1) </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     { </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         </font><font style="color:red;font-size:14px" face="Times New Roman flag</font><font style="font-size:14px" face="Times New Roman =::WaitForMultipleObjects(2,h1,TRUE,INFINITE); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     } </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     ::CloseHandle(h1[0]); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     ::CloseHandle(h1[1]); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     return 0; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman } </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman // thread function definition </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman void WriteToFile1( ) </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman {    </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman     while(1) </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     { </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman         if(flag) </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         { </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             ::WaitForSingleObject(hEvent2,INFINITE); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             ::ResetEvent(hEvent2); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             struct tm *current; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             time_t now; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             time(&now); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             //now contents current  time </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             //convert current time to local time </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             </font><font style="color:red;font-size:14px" face="Times New Roman current</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman localtime</font><font style="font-size:14px" face="Times New Roman (&now); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             //cout</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             //cout</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"i am inside the shared resource i.e a common filen"; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             cout</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"current thread is:"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <::GetCurrentThreadId()</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"tHH:MIN:</font><font style="color:red;font-size:14px" face="Times New Roman SEC</font><font style="font-size:14px" face="Times New Roman =t"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"n"; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             fstream file; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             file.open("c:\common.txt", fstream::out | fstream::app); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             file</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"</font><font style="color:red;font-size:14px" face="Times New Roman thread_id</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman "<<::GetCurrentThreadId()<<"</font><font style="font-size:14px" face="Times New Roman tHH:MIN:</font><font style="color:red;font-size:14px" face="Times New Roman SEC</font><font style="font-size:14px" face="Times New Roman =t"; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             file</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"n"; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             file.close(); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             ::SetEvent(hEvent1); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             </font><font style="color:red;font-size:14px" face="Times New Roman flag</font><font style="font-size:14px" face="Times New Roman =</font><font style="color:blue;font-size:14px" face="Times New Roman 0</font><font style="font-size:14px" face="Times New Roman ; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             Sleep(2000); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         else </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman         { </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             ::WaitForSingleObject(hEvent1,INFINITE); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             ::ResetEvent(hEvent1); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             struct tm *current; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             time_t now; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             time(&now); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             //now contents current  time </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             //convert current time to local time </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             </font><font style="color:red;font-size:14px" face="Times New Roman current</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman localtime</font><font style="font-size:14px" face="Times New Roman (&now); </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             cout</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"current thread is:"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <::GetCurrentThreadId()</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"tHH:MIN:</font><font style="color:red;font-size:14px" face="Times New Roman SEC</font><font style="font-size:14px" face="Times New Roman =t"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"n"; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             fstream file; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             file.open("c:\common.txt", fstream::out | fstream::app); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             file</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"</font><font style="color:red;font-size:14px" face="Times New Roman thread_id</font><font style="font-size:14px" face="Times New Roman  = </font><font style="color:blue;font-size:14px" face="Times New Roman "<<::GetCurrentThreadId()<<"</font><font style="font-size:14px" face="Times New Roman tHH:MIN:</font><font style="color:red;font-size:14px" face="Times New Roman SEC</font><font style="font-size:14px" face="Times New Roman =t"; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             file</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_hour</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <</font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_min</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <":"</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman < </font><font style="color:black;font-weight:bold;font-size:14px" face="Times New Roman current-</font><font style="font-size:14px" face="Times New Roman >tm_sec</font><font style="color:blue;font-size:14px" face="Times New Roman <</font><font style="font-size:14px" face="Times New Roman <"n"; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman             file.close(); </font><font style="color:red;font-size:14px" face="Times New Roman flag</font><font style="font-size:14px" face="Times New Roman =</font><font style="color:blue;font-size:14px" face="Times New Roman 1</font><font style="font-size:14px" face="Times New Roman ; </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman             ::SetEvent(hEvent2); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman         } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman     } </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman } </font></td></tr><tr><td><font style="font-size:14px" face="Times New Roman  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman  </font></td></tr></tbody></table></blockquote><div style="overflow:auto;background-color:white;width:4px;height:2px;line-height:100% ! important;font-family:Courier New;font-size:11px <table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><tbody><tr align=left><td><font style="font-size:14px" face="Times New Roman
</font></td></tr><tr align=left><td style="background-color:rgb(247, 247, 247) <font style="font-size:14px" face="Times New Roman
</font></td></tr><tr><td>
</td></tr><tr><td style="background-color:rgb(247, 247, 247)
</td></tr><tr><td>
</td></tr><tr><td style="background-color:rgb(247, 247, 247)
</td></tr><tr><td>    DWORD threadid1;DWORD  *tid1; </td></tr><tr><td style="background-color:rgb(247, 247, 247)     <font style="color:red tid1</font><font style="font-size:11px =&threadid1; </font></td></tr><tr><td>    DWORD threadid2;DWORD *tid2; </td></tr><tr><td style="background-color:rgb(247, 247, 247)     <font style="color:red tid2</font><font style="font-size:11px =&threadid2; </font></td></tr><tr><td>    HANDLE hMutex; </td></tr><tr><td style="background-color:rgb(247, 247, 247)     <font style="color:red hMutex</font><font style="font-size:11px  = </font><font style="color:blue CreateMutex</font><font style="font-size:11px (NULL,FALSE,NULL);                       </font></td></tr><tr><td>    // unnamed mutex </td></tr><tr><td style="background-color:rgb(247, 247, 247)     if (<font style="color:red hMutex</font><font style="font-size:11px  == NULL)  </font></td></tr><tr><td>    { </td></tr><tr><td style="background-color:rgb(247, 247, 247)         cout<font style="color:blue <</font><font style="font-size:11px <"CreateMutex error: "</font><font style="color:blue <</font><font style="font-size:11px <::GetLastError(); </font></td></tr><tr><td>    } </td></tr><tr><td style="background-color:rgb(247, 247, 247)  </td></tr><tr><td>    __try </td></tr><tr><td style="background-color:rgb(247, 247, 247)     { </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)     } </td></tr><tr><td>    __finally </td></tr><tr><td style="background-color:rgb(247, 247, 247)     { </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)     } </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)     HANDLE h1,h2; </td></tr><tr><td>    <font style="color:red h1</font><font style="font-size:11px  = </font><font style="color:blue CreateThread</font><font style="font-size:11px (NULL, 0, (LPTHREAD_START_ROUTINE)WriteToFile, hMutex , CREATE_SUSPENDED, tid1); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)     <font style="color:red h2</font><font style="font-size:11px  = </font><font style="color:blue CreateThread</font><font style="font-size:11px (NULL, 0, (LPTHREAD_START_ROUTINE)WriteToFile, hMutex, CREATE_SUSPENDED, tid2); </font></td></tr><tr><td>    ::ResumeThread(h1); </td></tr><tr><td style="background-color:rgb(247, 247, 247)     ::ResumeThread(h2); </td></tr><tr><td>    //Sleep(200000); </td></tr><tr><td style="background-color:rgb(247, 247, 247)     /*while(1) </td></tr><tr><td>    { </td></tr><tr><td style="background-color:rgb(247, 247, 247)         //Sleep(2000); </td></tr><tr><td>        WaitForSingleObject(h1,2000);    </td></tr><tr><td style="background-color:rgb(247, 247, 247)         //::Wai </td></tr><tr><td>         </td></tr><tr><td style="background-color:rgb(247, 247, 247)         //Sleep(2000); </td></tr><tr><td>        WaitForSingleObject(h2,2000);  </td></tr><tr><td style="background-color:rgb(247, 247, 247)          </td></tr><tr><td>        //Sleep(2000); </td></tr><tr><td style="background-color:rgb(247, 247, 247)         //cout<font style="color:blue <</font><font style="font-size:11px <"writing loop which writes to a file after every 2 secondsn"; </font></td></tr><tr><td>    }*/ </td></tr><tr><td style="background-color:rgb(247, 247, 247)     while(1) </td></tr><tr><td>    { </td></tr><tr><td style="background-color:rgb(247, 247, 247)         ::Sleep(2000); </td></tr><tr><td>    } </td></tr><tr><td style="background-color:rgb(247, 247, 247)     //cout<font style="color:blue <</font><font style="font-size:11px <"in main thread of executionn"; </font></td></tr><tr><td>    ::CloseHandle(h1); </td></tr><tr><td style="background-color:rgb(247, 247, 247)     ::CloseHandle(h2); </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)     return 0; </td></tr><tr><td>} </td></tr><tr><td style="background-color:rgb(247, 247, 247)  </td></tr><tr><td>void WriteToFile(HANDLE hMutex ) </td></tr><tr><td style="background-color:rgb(247, 247, 247) { </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)  </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)     DWORD dwWaitResult;  </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)     // Request ownership of mutex. </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)     <font style="color:red dwWaitResult</font><font style="font-size:11px  = </font><font style="color:blue WaitForSingleObject</font><font style="font-size:11px ( </font></td></tr><tr><td>        hMutex,   // handle to mutex </td></tr><tr><td style="background-color:rgb(247, 247, 247)         5000L);   // five-second time-out interval </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)     switch (dwWaitResult)  </td></tr><tr><td>    { </td></tr><tr><td style="background-color:rgb(247, 247, 247)         // The thread got mutex ownership. </td></tr><tr><td>    case WAIT_OBJECT_0:  </td></tr><tr><td style="background-color:rgb(247, 247, 247)         __try  </td></tr><tr><td>        {  </td></tr><tr><td style="background-color:rgb(247, 247, 247)              </td></tr><tr><td>             </td></tr><tr><td style="background-color:rgb(247, 247, 247)             struct tm *current; </td></tr><tr><td>            time_t now; </td></tr><tr><td style="background-color:rgb(247, 247, 247)             time(&now); </td></tr><tr><td>            //now contents current  time </td></tr><tr><td style="background-color:rgb(247, 247, 247)             //convert current time to local time </td></tr><tr><td>            <font style="color:red current</font><font style="font-size:11px  = </font><font style="color:blue localtime</font><font style="font-size:11px (&now); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)             //cout<font style="color:blue <</font><font style="font-size:11px <</font><font style="color:black;font-weight:bold current-</font><font style="font-size:11px >tm_hour</font><font style="color:blue <</font><font style="font-size:11px <</font><font style="color:black;font-weight:bold current-</font><font style="font-size:11px >tm_min</font><font style="color:blue <</font><font style="font-size:11px < </font><font style="color:black;font-weight:bold current-</font><font style="font-size:11px >tm_sec </font></td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)             cout<font style="color:blue <</font><font style="font-size:11px <"i am inside the shred resource i.e a common filen"; </font></td></tr><tr><td>            cout<font style="color:blue <</font><font style="font-size:11px <"current thread is:"</font><font style="color:blue <</font><font style="font-size:11px <::GetCurrentThreadId(); </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)             fstream file; </td></tr><tr><td>            file.open("c:\common.txt", fstream::out | fstream::app); </td></tr><tr><td style="background-color:rgb(247, 247, 247)             file<font style="color:blue <</font><font style="font-size:11px <"hello"; </font></td></tr><tr><td>            file<font style="color:blue <</font><font style="font-size:11px <</font><font style="color:black;font-weight:bold current-</font><font style="font-size:11px >tm_hour</font><font style="color:blue <</font><font style="font-size:11px <":"</font><font style="color:blue <</font><font style="font-size:11px <</font><font style="color:black;font-weight:bold current-</font><font style="font-size:11px >tm_min</font><font style="color:blue <</font><font style="font-size:11px <":"</font><font style="color:blue <</font><font style="font-size:11px < </font><font style="color:black;font-weight:bold current-</font><font style="font-size:11px >tm_sec; </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)             file.close(); </td></tr><tr><td>             </td></tr><tr><td style="background-color:rgb(247, 247, 247)              </td></tr><tr><td>        }  </td></tr><tr><td style="background-color:rgb(247, 247, 247)  </td></tr><tr><td>        __finally {  </td></tr><tr><td style="background-color:rgb(247, 247, 247)             // Release ownership of the mutex object. </td></tr><tr><td>            if (! ReleaseMutex(hMutex))  </td></tr><tr><td style="background-color:rgb(247, 247, 247)             {  </td></tr><tr><td>                // Deal with error. </td></tr><tr><td style="background-color:rgb(247, 247, 247)                 cout<font style="color:blue <</font><font style="font-size:11px <"cant give ownership of mutexn"; </font></td></tr><tr><td>            }  </td></tr><tr><td style="background-color:rgb(247, 247, 247)  </td></tr><tr><td>            //break;  </td></tr><tr><td style="background-color:rgb(247, 247, 247)         }  </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)         // Cannot get mutex ownership due to time-out. </td></tr><tr><td>    case WAIT_TIMEOUT:  </td></tr><tr><td style="background-color:rgb(247, 247, 247)         cout<font style="color:blue <</font><font style="font-size:11px <"time out for ownership of mutex variablen"; </font></td></tr><tr><td>        return ;  </td></tr><tr><td style="background-color:rgb(247, 247, 247)          </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)         // Got ownership of the abandoned mutex object. </td></tr><tr><td>    case WAIT_ABANDONED:  </td></tr><tr><td style="background-color:rgb(247, 247, 247)         cout<font style="color:blue <</font><font style="font-size:11px <"owner ship of abonded mutex objectn"; </font></td></tr><tr><td>        return ;  </td></tr><tr><td style="background-color:rgb(247, 247, 247)          </td></tr><tr><td>    } </td></tr><tr><td style="background-color:rgb(247, 247, 247)     cout<font style="color:blue <</font><font style="font-size:11px <"done with mutex having shred reourcen"; </font></td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)     return ;  </td></tr><tr><td>} </td></tr></tbody></table>

View the full article
 
Back
Top