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
*************** ************
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
*************** ************
Code:
//----------------------------------------------// 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);
}
Comment