Wrox Programmer Forums
|
Visual C++ 2005 For discussion of Visual C++ 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2009, 04:14 AM
Registered User
 
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question Thread problem

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,here is the code

Code:

#include "stdafx.h" #include "stdafx.h" #include <fstream> #include <iostream> #include <windows.h> #include <time.h> using namespace std; HANDLE hEvent1 , hEvent2; //thread function declaration void WriteToFile1(); int flag =1; int _tmain(int argc, _TCHAR* argv[]) { DWORD threadid[2],flag; HANDLE h1[2]; hEvent1 = CreateEvent(NULL,TRUE,FALSE,NULL); hEvent2 = CreateEvent(NULL,TRUE,FALSE,NULL); SetEvent(hEvent1); SetEvent(hEvent2); h1[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteToFile1, NULL , CREATE_SUSPENDED, &threadid[0]); h1[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteToFile1, NULL, CREATE_SUSPENDED, &threadid[1]); ::ResumeThread(h1[0]); ::ResumeThread(h1[1]); //don't let this main thread die. while(1) { flag=::WaitForMultipleObjects(2,h1,TRUE,INFINITE); } ::CloseHandle(h1[0]); ::CloseHandle(h1[1]); return 0; } // thread function definition void WriteToFile1( ) { while(1) { if(flag) { ::WaitForSingleObject(hEvent2,INFINITE); ::ResetEvent(hEvent2); struct tm *current; time_t now; time(&now); //now contents current time //convert current time to local time current = localtime(&now); //cout<<current->tm_hour<<current->tm_min<< current->tm_sec //cout<<"i am inside the shared resource i.e a common file\n"; cout<<"current thread is:"<<::GetCurrentThreadId()<<"\tHH:MIN:SEC=\t"<<current->tm_hour<<":"<<current->tm_min<<":"<< current->tm_sec<<"\n"; fstream file; file.open("c:\\common.txt", fstream::out | fstream::app); file<<"thread_id = "<<::GetCurrentThreadId()<<"\tHH:MIN:SEC=\t"; file<<current->tm_hour<<":"<<current->tm_min<<":"<< current->tm_sec<<"\n"; file.close(); ::SetEvent(hEvent1); flag=0; Sleep(2000); } else { ::WaitForSingleObject(hEvent1,INFINITE); ::ResetEvent(hEvent1); struct tm *current; time_t now; time(&now); //now contents current time //convert current time to local time current = localtime(&now); cout<<"current thread is:"<<::GetCurrentThreadId()<<"\tHH:MIN:SEC=\t"<<current->tm_hour<<":"<<current->tm_min<<":"<< current->tm_sec<<"\n"; fstream file; file.open("c:\\common.txt", fstream::out | fstream::app); file<<"thread_id = "<<::GetCurrentThreadId()<<"\tHH:MIN:SEC=\t"; file<<current->tm_hour<<":"<<current->tm_min<<":"<< current->tm_sec<<"\n"; file.close(); flag=1; ::SetEvent(hEvent2); } } }
thanks puneet vyas
 
Old November 4th, 2009, 05:55 AM
Authorized User
 
Join Date: Mar 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,I understand your code this way which may help.
at begining two thread wait for same object hEvent2,they may return at the same time sice you are not sure one of them will wait after another call ResetEvent;then they both reset event 2 and set event1 and so on...

possible solutions:
1.set one event and reset another(reset event 1 in your code sice you set your flag to 1)
2.sleep for 500 millisecond before resume the second thread ,the first thread may have neccessary time to reset event2...
3.forget to sleep in else statement.
__________________
fight now,date in future.

Last edited by Aiming; November 4th, 2009 at 06:08 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Thread.abort nelly78 .NET Framework 2.0 10 April 4th, 2008 01:12 AM
Which Thread to use and How peace2007 ASP.NET 1.0 and 1.1 Professional 0 September 23rd, 2007 03:24 AM
communiation between thread gunx Pro VB 6 3 May 10th, 2005 03:56 PM
Problem in Thread Atif Khan C# 0 April 8th, 2005 06:57 AM
terminate a thread problem lmadhavi Visual C++ 0 November 23rd, 2004 06:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.