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. -
-
#include <windows.h>
-
#include <stdio.h>
-
-
#define BUF_SIZE 255
-
-
//------------------------------------------
-
// A function to Display the message indicating in which tread we are
-
//------------------------------------------
-
void DisplayMessage (char *ThreadName, int Data, int Count, char input[80])
-
{
-
printf("Executing iteration %02d of %s having data = %02d and %s \n", Count, ThreadName, Data, input);
-
Sleep(1000);
-
}
-
-
//-------------------------------------------
-
// A function that represents Thread number 1
-
//-------------------------------------------
-
DWORD WINAPI Thread_no_1( LPVOID lpParam )
-
{
-
int Data = 0;
-
int count = 0;
-
-
char input[80];
-
strcpy(input, "thread1");
-
-
// Cast the parameter to the correct data type passed by callee i.e main() in our case.
-
Data = *((int*)lpParam);
-
-
for (count = 0; count <= 4; count++ )
-
{
-
DisplayMessage ("Thread_no_1", Data, count, input);
-
}
-
-
return 0;
-
}
-
-
//-------------------------------------------
-
// A function that represents Thread number 2
-
//-------------------------------------------
-
DWORD WINAPI Thread_no_2( LPVOID lpParam )
-
{
-
int Data = 0;
-
int count = 0;
-
-
char input[80];
-
strcpy(input, "thread2");
-
-
// Cast the parameter to the correct data type passed by callee i.e main() in our case.
-
Data = *((int*)lpParam);
-
-
for (count = 0; count <= 7; count++ )
-
{
-
DisplayMessage ("Thread_no_2", Data, count, input);
-
}
-
-
return 0;
-
}
-
-
void main()
-
{
-
// Data of Threads
-
int Data_Of_Thread_1 = 6;
-
int Data_Of_Thread_2 = 2;
-
-
// variable to hold handle of Threads
-
HANDLE Handle_Of_Thread_1 = 0;
-
HANDLE Handle_Of_Thread_2 = 0;
-
-
// Array to store thread handles
-
HANDLE Array_Of_Thread_Handles[2];
-
-
// Create thread 1.
-
Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, &Data_Of_Thread_1, 0, NULL);
-
if ( Handle_Of_Thread_1 == NULL)
-
ExitProcess(Data_Of_Thread_1);
-
-
// Create thread 2.
-
Handle_Of_Thread_2 = CreateThread( NULL, 0, Thread_no_2, &Data_Of_Thread_2, 0, NULL);
-
if ( Handle_Of_Thread_2 == NULL)
-
ExitProcess(Data_Of_Thread_2);
-
-
// 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 lets close their handles \n");
-
-
// Close all thread handles upon completion.
-
CloseHandle(Handle_Of_Thread_1);
-
CloseHandle(Handle_Of_Thread_2);
-
}
-
-
2 1745
I got it to work, yay. I would delete the post, but it's not letting me.
You should be using CreateThread(). You use _beginthreadx() only if are using errno, etc. Windows doesn't use that.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| | |