Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++ 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 30th, 2009, 03: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
Reply With Quote
  #2 (permalink)  
Old November 4th, 2009, 04:55 AM
Authorized User
 
Join Date: Mar 2008
Location: china nantong, HOMETOWN, China.
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 05:08 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 05:20 AM



All times are GMT -4. The time now is 12:48 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.