I am working on creating windows.h to creat tow threads, each thread takes tow files as input, the function that each thread excute is tested and work 100%,the problem is that one thread could work at atime ?
I mean one function is excuted this is the threading code i dont know what is the problem. I tried to pass different files to each thread but its still not working
*************** ************ - //----------------------------------------------// A function that represents Thread number 1 WM//----------------------------------------------DWORD WINAPI Thread_no_1( LPVOID S ) { data_files *My_files = (data_files *)(S); HANDLE hStdout = NULL; // Get Handle To screen. Else how will we print? if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE ) return 1; // Cast the parameter to the correct data type passed by callee i.e main() in our case. string text=My_files->text; string pattern=My_files->pattern; fw(text,pattern );} //----------------------------------------------// A function that represents Thread number 2 AC//----------------------------------------------DWORD WINAPI Thread_no_2( LPVOID W ) { data_files *fA_files = (data_files *)(W); HANDLE hStdout = NULL; // Get Handle To screen. Else how will we print? if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE ) return 1; // Cast the parameter to the correct data type passed by callee i.e main() in our case. string text=aA_files->text; string pattern=fA_files->pattern; fA(text,pattern ); } int _tmain(int argc, _TCHAR* argv[]) { string pattern,text; cout<<"Enter patterns file name.\n"; cin>>pattern; cout<<"Enter text file name.\n"; cin>>text; /////////////////// Threading // Data of Thread 1 data_files *Data_Of_Thread_1=new data_files ; Data_Of_Thread_1->text=text; Data_Of_Thread_1->pattern=pattern; LPVOID S; S = (LPVOID)Data_Of_Thread_1; //End Data of Thread 1 // Data of Thread 2 data_files *Data_Of_Thread_2=new data_files ; Data_Of_Thread_2->text=text; Data_Of_Thread_2->pattern=pattern; LPVOID H; H = (LPVOID)Data_Of_Thread_2; //End Data of Thread 2 HANDLE Handle_Of_Thread_1 = 0; // variable to hold handle of Thread 1 HANDLE Handle_Of_Thread_2 = 0; // variable to hold handle of Thread 2 HANDLE Array_Of_Thread_Handles[2]; // Aray to store thread handles // Create thread 1. Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1,S, 0, NULL); if ( Handle_Of_Thread_1 == NULL) ExitProcess(0); // Create thread 2. Handle_Of_Thread_2 = CreateThread( NULL, 0, Thread_no_2,H, 0, NULL); if ( Handle_Of_Thread_2 == NULL) ExitProcess(0); // Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects() Array_Of_Thread_Handles[0] = Handle_Of_Thread_1; Array_Of_Thread_Handles[1] = Handle_Of_Thread_2; // Wait until all threads have terminated. WaitForMultipleObjects( 2, Array_Of_Thread_Handles, TRUE, INFINITE); printf("Since All threads executed close their handles \n"); // Close all thread handles upon completion. CloseHandle(Handle_Of_Thread_1); CloseHandle(Handle_Of_Thread_2); }
-
//----------------------------------------------
-
// A function that represents Thread number 1
-
//----------------------------------------------
-
DWORD WINAPI Thread_no_1( LPVOID S )
-
{ data_files *My_files = (data_files *)(S);
-
-
-
HANDLE hStdout = NULL;
-
-
// Get Handle To screen. Else how will we print?
-
if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE )
-
return 1;
-
-
// Cast the parameter to the correct data type passed by callee i.e main() in our case.
-
string text=My_files->text;
-
string pattern=My_files->pattern;
-
-
fw(text,pattern );
-
}
-
//----------------------------------------------
-
// A function that represents Thread number 2
-
//----------------------------------------------
-
DWORD WINAPI Thread_no_2( LPVOID W )
-
{ data_files *fA_files = (data_files *)(W);
-
-
-
HANDLE hStdout = NULL;
-
-
// Get Handle To screen. Else how will we print?
-
if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE )
-
return 1;
-
-
// Cast the parameter to the correct data type passed by callee i.e main() in our case.
-
string text=aA_files->text;
-
string pattern=fA_files->pattern;
-
-
fA(text,pattern );
-
-
}
-
-
-
int _tmain(int argc, _TCHAR* argv[])
-
-
{
-
string pattern,text;
-
cout<<"Enter patterns file name.\n";
-
cin>>pattern;
-
-
cout<<"Enter text file name.\n";
-
cin>>text;
-
-
-
/////////////////// Threading
-
-
// Data of Thread 1
-
-
data_files *Data_Of_Thread_1=new data_files ;
-
-
Data_Of_Thread_1->text=text;
-
Data_Of_Thread_1->pattern=pattern;
-
LPVOID S;
-
-
S = (LPVOID)Data_Of_Thread_1;
-
-
//End Data of Thread 1
-
-
// Data of Thread 2
-
-
data_files *Data_Of_Thread_2=new data_files ;
-
Data_Of_Thread_2->text=text;
-
Data_Of_Thread_2->pattern=pattern;
-
LPVOID H;
-
-
H = (LPVOID)Data_Of_Thread_2;
-
-
//End Data of Thread 2
-
-
-
HANDLE Handle_Of_Thread_1 = 0; // variable to hold handle of Thread 1
-
HANDLE Handle_Of_Thread_2 = 0; // variable to hold handle of Thread 2
-
-
HANDLE Array_Of_Thread_Handles[2]; // Aray to store thread handles
-
-
// Create thread 1.
-
-
Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1,S, 0, NULL);
-
if ( Handle_Of_Thread_1 == NULL) ExitProcess(0);
-
-
// Create thread 2.
-
Handle_Of_Thread_2 = CreateThread( NULL, 0, Thread_no_2,H, 0, NULL);
-
if ( Handle_Of_Thread_2 == NULL) ExitProcess(0);
-
-
-
-
// Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects()
-
Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
-
Array_Of_Thread_Handles[1] = Handle_Of_Thread_2;
-
-
-
// Wait until all threads have terminated.
-
WaitForMultipleObjects( 2, Array_Of_Thread_Handles, TRUE, INFINITE);
-
-
printf("Since All threads executed close their handles \n");
-
-
// Close all thread handles upon completion.
-
CloseHandle(Handle_Of_Thread_1);
-
CloseHandle(Handle_Of_Thread_2);
-
-
-
-
-
-
-
}
3 1783
It isnt clear what you need help to. I supose you want the two threads to execute at the exactly SAME time? i think that's handled by the OS itself.
yes, the problem is when i run this code one thread is excuted then the system teriminate the threads and exit is it possible that this problem caused by line 88
"
" .
each program have to open the same files , they poth perform open file ....mybe this is the problem i dont know!!!!!!!!
You can't open the same file twice :l, maybe do a copy in plain text, to access them independently by each thread?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mike Goatly |
last post by:
Hi,
I need to be able to read the amount of processor time a thread has taken
up. So far I've found the following methods open to me:
1) Find the relevant ProcessThread in the current process'
ProcessThreadCollection and read the TotalProcessorTime.
2) Find the PerformanceCounter for the Thread category that relates to the
current thread, and use the NextSample() method to read the processor time
|
by: vee_kay |
last post by:
Ihave a written aprogram in C which implements _beginthread(to create a
thread) and _endthread(to end a thread).The program need to write a
string of date n time to a file for each succesful thread created. I
had put a delay of a second so that the thread and io operation will
occur after a second. Now i need to implement another thing which i
need to make sure the run was actually a second. This is because if i
add another delay of...
|
by: Romulo Carneiro |
last post by:
Hi all,
I want to calculate a time of any routine in micro second.I know there
is a function utime.h in the programming linux.But I'm using a OS
Windows. If somebody know the soluction, let me know, please. Thanks!!
|
by: Audrey |
last post by:
Please why when I write :
while(1){
Console.writeln("date= {0:HH:mm:ss.ffff}", DateTime.Now);
System.Threadind.Thread.Sleep(40);
}
I obtain
date= 16:04:35.6250
date= 16:04:35.6718
date= 16:04:35.7187
date= 16:04:35.7656
|
by: Ronald S. Cook |
last post by:
In a .NET Windows app, if I set somehting like the title of the form to
"MyApp" at run-time, will that make the app run slightly slower than if I
had set the title at design-time?
Thanks,
Ron
| |
by: gagonm |
last post by:
HI
I am working on Engineering application where we need thread.sleep() function many times. But since sleep interval is not accurate we need an alternative which can mimick functionality of thread.sleep() .
we have multimedia timer in our code so Please lemme know if I can use that in someway for this purpose.
Any Help would be greatly appreciated.
|
by: Steve |
last post by:
Hi All,
I've been trying to come up with a good way to run a certain process
at a timed interval (say every 5 mins) using the SLEEP command and a
semaphore flag. The basic thread loop was always sitting in the sleep
command and not able to be interrupted. When the time came to set the
semaphore flag to false (stopping the thread), my program would have
to wait up to the entire sleep time to break out of the loop.
I have finally found...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |