Coder Profile - Show off your skills, get a coder profile.
 
 
 
Quick Threading Tutorial - C++
Programming
Threading In C++, By Otoom

Hello, I will show you how to thread in C++.

First you need to create the thread, so if we place this into our editor...
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. DWORD WINAPI Threadname (LPVOID lPData)
  2. {
  3.  
  4.  
  5. }
  6.  
  7. int main()
  8. {
  9.  
  10.  
  11. }
Okay, and that is the basic layout.
Now we have declared the thread, of course change Threadname to whatever you would like to call it.

What we can now do is, call on that thread within the main.
So if we add a little bit of code...
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. DWORD WINAPI Threadname (LPVOID lpData)
  2. {
  3.  
  4.  
  5. }
  6.  
  7. int main()
  8. {
  9.  
  10.        HANDLE Threadname = CreateThread(NULL,0, Threadname, NULL,0,0);
  11. }
Okay, so, What have we done here?
What we have now done is call upon the thread we declared. Using a handle.

But after all this, i bet you think what can threads even do.
Okay well. Threads let you run something else while running main.
It is like a background running program but within the same application.

For example.
We can show the lyrics of a song, while making the beeps of it.

I hope you get that.
So... Now.
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. DWORD WINAPI Beeps (LPVOID lpData)
  2. {
  3.        while(1)
  4.        {
  5.               cout<<"\a";
  6.        }
  7. }
  8.  
  9. int main()
  10. {
  11.        HANDLE Beeps = CreateThread(NULL, 0, Beeps, NULL, 0,0);
  12.  
  13.        cout<<"You can hear the beeps...\n";
  14.        cout<<"While looking at this text...\n";
  15. }
Just on a side note, the lpData is not needed.
I just included it as thats the way i learned it.

Otoom
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
 
otoom     Posted 2.86 Years Ago
 
 
Yeah i wrote this the day i learn about threading in C++. And as you see, only in
windows.
And now, i don't code in C++ so i am not going to learn about anymore of this,
 
closure     Posted 2.87 Years Ago
 
 
Hi,
i'm missing some information. First of all this is windows only, it would be
great to supply alternatives like pthread. Secondly what do all the parameters to
CreateThread() mean? And why do you say that the void-pointer is not needed?
If i want to pass arguments to my thread-function i can sure use this pointer.

Also it would be great if you would say sth about problems that arise when
you're in a multithreaded environment like race-conditions and other
synchronization-problems.

Beside that the article is ok, but it won't get anybody very far. But as a
starting point it's suitable i guess.

cheers
Page 1 of 1
More Articles By This Author
Quick Threading Tutorial - C++
First look at C
Cheat Sheet Pack
[PHP] How to make a log file.
HTML Cheat Sheet!
[C++] Comments
[C++] Functions
[C++]Taking In User Input,
Your First C++ Program
Ant (17)
United Kingdom, West Midlands
otoom has 8 fans
become a fan
� Applications
Articles Articles (9)
Source Codes Source Codes (18)
Code Pin Board Code Pin Board (2)
� About Me
About Me About Me
Portfolio Portfolio (2)
Friends Friends (159)
Interview Interview
� Misc
Overview Overview
Send A Friend Invite
Send Message Send A Message
RSS Whole Profile Feed
 
 
Latest News About Coder Profile
Coder Profile Poll
Why do you get bored with programming?

Not enough time to do something productive
I run out of ideas
Too hard to show people my creations
Everything i do has too many errors, and it's too hard
I don't get bored!!!


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
2.53 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright � 2007 - 2011, Scott Thompson, All Rights Reserved