473,789 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

11 New Member
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
3 1783
JavierL
17 New Member
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
ekailan
11 New Member
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
17 New Member
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

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

Similar topics

5
459
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
6
8866
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...
4
1873
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!!
11
2315
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
7
2009
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
12
32242
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.
2
3373
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...
0
9657
marktang
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...
0
9502
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,...
0
10185
jinu1996
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...
1
10132
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,...
0
6753
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();...
0
5408
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...
0
5544
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4083
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
2
3684
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.