By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

How to get more than one thread to execute at a time?

P: 11
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
***************************

Expand|Select|Wrap|Line Numbers
  1.  //----------------------------------------------// 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);      }
  2. //----------------------------------------------
  3. // A function that represents Thread number 1 
  4. //----------------------------------------------
  5. DWORD WINAPI Thread_no_1( LPVOID S ) 
  6. { data_files *My_files = (data_files *)(S);      
  7.  
  8.  
  9.     HANDLE  hStdout = NULL;
  10.  
  11.     // Get Handle To screen. Else how will we print?
  12.     if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE )  
  13.         return 1;
  14.  
  15.     // Cast the parameter to the correct data type passed by callee i.e main() in our case.
  16.     string text=My_files->text;
  17.     string pattern=My_files->pattern;
  18.  
  19.         fw(text,pattern );
  20. //----------------------------------------------
  21. // A function that represents Thread number 2 
  22. //----------------------------------------------
  23. DWORD WINAPI Thread_no_2( LPVOID W ) 
  24. { data_files *fA_files = (data_files *)(W);      
  25.  
  26.  
  27.     HANDLE  hStdout = NULL;
  28.  
  29.     // Get Handle To screen. Else how will we print?
  30.     if( (hStdout = GetStdHandle(STD_OUTPUT_HANDLE)) == INVALID_HANDLE_VALUE )  
  31.         return 1;
  32.  
  33.     // Cast the parameter to the correct data type passed by callee i.e main() in our case.
  34.     string text=aA_files->text;
  35.     string pattern=fA_files->pattern;
  36.  
  37.         fA(text,pattern );
  38.  
  39.  
  40.  
  41. int _tmain(int argc, _TCHAR* argv[]) 
  42.  
  43. {
  44.     string pattern,text;
  45.     cout<<"Enter patterns file name.\n";
  46.     cin>>pattern;
  47.  
  48.     cout<<"Enter text file name.\n";
  49.     cin>>text;
  50.  
  51.  
  52. /////////////////// Threading
  53.  
  54.     // Data of Thread 1
  55.  
  56.     data_files *Data_Of_Thread_1=new data_files ;           
  57.  
  58.     Data_Of_Thread_1->text=text;
  59.     Data_Of_Thread_1->pattern=pattern;
  60.     LPVOID S;
  61.  
  62.     S = (LPVOID)Data_Of_Thread_1;
  63.  
  64.     //End Data of Thread 1
  65.  
  66.     // Data of Thread 2
  67.  
  68.     data_files *Data_Of_Thread_2=new data_files ;           
  69.     Data_Of_Thread_2->text=text;
  70.     Data_Of_Thread_2->pattern=pattern;
  71.     LPVOID H;
  72.  
  73.     H = (LPVOID)Data_Of_Thread_2;
  74.  
  75.     //End Data of Thread 2
  76.  
  77.  
  78.     HANDLE Handle_Of_Thread_1 = 0;       // variable to hold handle of Thread 1
  79.     HANDLE Handle_Of_Thread_2 = 0;       // variable to hold handle of Thread 2 
  80.  
  81.      HANDLE Array_Of_Thread_Handles[2];   // Aray to store thread handles 
  82.  
  83.      // Create thread 1.
  84.  
  85.     Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1,S, 0, NULL);  
  86.     if ( Handle_Of_Thread_1 == NULL)  ExitProcess(0);
  87.  
  88.     // Create thread 2.
  89.     Handle_Of_Thread_2 = CreateThread( NULL, 0, Thread_no_2,H, 0, NULL);  
  90.     if ( Handle_Of_Thread_2 == NULL)  ExitProcess(0);
  91.  
  92.  
  93.  
  94.     // Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects() 
  95.     Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
  96.     Array_Of_Thread_Handles[1] = Handle_Of_Thread_2;
  97.  
  98.  
  99.     // Wait until all threads have terminated.
  100.     WaitForMultipleObjects( 2, Array_Of_Thread_Handles, TRUE, INFINITE);
  101.  
  102.     printf("Since All threads executed close their handles \n");
  103.  
  104.     // Close all thread handles upon completion.
  105.     CloseHandle(Handle_Of_Thread_1);
  106.     CloseHandle(Handle_Of_Thread_2);
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. }
Oct 25 '10 #1
Share this Question
Share on Google+
3 Replies


JavierL
P: 17
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.
Oct 25 '10 #2

P: 11
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
"
Expand|Select|Wrap|Line Numbers
  1.  ExitProcess(0); 
" .

each program have to open the same files , they poth perform open file ....mybe this is the problem i dont know!!!!!!!!
Oct 25 '10 #3

JavierL
P: 17
You can't open the same file twice :l, maybe do a copy in plain text, to access them independently by each thread?
Oct 25 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.