473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

multithreading in C, need help

175 100+
I'm trying to learn multithreading in C. I've never done it before, and I've spent several hours reading about it, and looking at examples. I found this example and have been messing with it (I've made a lot of changes, but it still works the same). What my goal is, is to have one thread constantly listening for incoming connections and have another thread where a user can actually send messages (using sockets...I have that stuff working fine).

What I'm asking is, with my extremely limited knowledge of threads, can I accomplish this with this code? I know that _beginthreadex is preferred over CreateThread (from what i've read), but at this moment, I'm interested in CreateThread.

this is a completely new concept to me and i'd appreciate any help or tips.


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <windows.h> 
  3. #include <stdio.h>
  4.  
  5. #define BUF_SIZE 255
  6.  
  7. //------------------------------------------
  8. // A function to Display the message indicating in which tread we are
  9. //------------------------------------------
  10. void DisplayMessage (char *ThreadName, int Data, int Count, char input[80])
  11. {
  12.     printf("Executing iteration %02d of %s having data = %02d and %s \n", Count, ThreadName, Data, input); 
  13.     Sleep(1000);
  14. }
  15.  
  16. //-------------------------------------------
  17. // A function that represents Thread number 1
  18. //-------------------------------------------
  19. DWORD WINAPI Thread_no_1( LPVOID lpParam ) 
  20. {
  21.     int     Data = 0;
  22.     int     count = 0;
  23.  
  24.     char input[80];
  25.     strcpy(input, "thread1");
  26.  
  27.     // Cast the parameter to the correct data type passed by callee i.e main() in our case.
  28.     Data = *((int*)lpParam); 
  29.  
  30.     for (count = 0; count <= 4; count++ )
  31.     {
  32.        DisplayMessage ("Thread_no_1", Data, count, input);
  33.     }
  34.  
  35.     return 0; 
  36.  
  37. //-------------------------------------------
  38. // A function that represents Thread number 2
  39. //-------------------------------------------
  40. DWORD WINAPI Thread_no_2( LPVOID lpParam ) 
  41. {
  42.     int     Data = 0;
  43.     int     count = 0;
  44.  
  45.     char input[80];
  46.     strcpy(input, "thread2");
  47.  
  48.     // Cast the parameter to the correct data type passed by callee i.e main() in our case.
  49.     Data = *((int*)lpParam); 
  50.  
  51.     for (count = 0; count <= 7; count++ )
  52.     {
  53.       DisplayMessage ("Thread_no_2", Data, count, input);
  54.     }
  55.  
  56.     return 0; 
  57.  
  58. void main()
  59. {
  60.     // Data of Threads
  61.     int Data_Of_Thread_1 = 6;
  62.     int Data_Of_Thread_2 = 2;
  63.  
  64.     // variable to hold handle of Threads
  65.     HANDLE Handle_Of_Thread_1 = 0;
  66.     HANDLE Handle_Of_Thread_2 = 0;
  67.  
  68.     // Array to store thread handles 
  69.     HANDLE Array_Of_Thread_Handles[2];
  70.  
  71.     // Create thread 1.
  72.     Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, &Data_Of_Thread_1, 0, NULL);  
  73.     if ( Handle_Of_Thread_1 == NULL)
  74.         ExitProcess(Data_Of_Thread_1);
  75.  
  76.     // Create thread 2.
  77.     Handle_Of_Thread_2 = CreateThread( NULL, 0, Thread_no_2, &Data_Of_Thread_2, 0, NULL);  
  78.     if ( Handle_Of_Thread_2 == NULL)
  79.         ExitProcess(Data_Of_Thread_2);
  80.  
  81.     // Store Thread handles in Array of Thread
  82.     // Handles as per the requirement of WaitForMultipleObjects() 
  83.     Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
  84.     Array_Of_Thread_Handles[1] = Handle_Of_Thread_2;
  85.  
  86.     // Wait until all threads have terminated.
  87.     WaitForMultipleObjects( 2, Array_Of_Thread_Handles, TRUE, INFINITE);
  88.  
  89.     printf("Since All threads executed lets close their handles \n");
  90.  
  91.     // Close all thread handles upon completion.
  92.     CloseHandle(Handle_Of_Thread_1);
  93.     CloseHandle(Handle_Of_Thread_2);
  94. }
  95.  
  96.  
Sep 15 '07 #1
2 1745
manontheedge
175 100+
I got it to work, yay. I would delete the post, but it's not letting me.
Sep 15 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You should be using CreateThread(). You use _beginthreadx() only if are using errno, etc. Windows doesn't use that.
Sep 16 '07 #3

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

Similar topics

10
by: gianguz | last post by:
The question is about the possible use of inlining to improve performance in a heavy multithreading environment (200+ threads). If we have to work with applications in which threads aren't I/O...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
7
by: noid droid | last post by:
Greetings. I received 4 VB .NET books and looking through the indices and tables of contents, I see that none of them addresses multithreading in VB ..NET. I just bought a bunch of books because...
2
by: SStory | last post by:
Here is the situation. I want to display Icons, Type of file etc from a file extension. Upon initial program load I may only need icons for certain files. But other operations will require...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
2
by: shonend | last post by:
**** sorry about the length of the message. If you can't read the whole thing and still willing to help, read the last 2 paragraphs where the main problem is described. The introduction story is...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
7
darlene
by: darlene | last post by:
Hi, I need some help in creating an application in Visual C++ which should make use of MFC and multithreading. The application is supposed to consist in a number of threads representing factories....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.