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


Posted By 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 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.1 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
[PHP] How to make a log file.
HTML Cheat Sheet!
[C++] Comments
[C++] Functions
[C++]Taking In User Input,
Your First C++ Program
Recently Posted "Programming" Articles
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
My Experiences So Far with Languages
Selecting Your First Programming Language
[PHP] - Lets kill the complex IF
Basic PortScanner in VB6.0
N-Queens-Series Part I (Only the fittest survive)
Currying in JavaScript: Fun for the Whole Family!
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
Recently Rated "Programming" Articles
[PHP] - Lets kill the complex IF
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
Actionscript API
Actionscript Events
My Experiences So Far with Languages
Selecting Your First Programming Language
Vectors in C++
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (2)
Hardware (1)
Internet & Web Sites (3)
Life In General (2)
Networking (1)
Operating Systems (5)
Other (2)
Programming (47)
Security (10)
Software Development (5)
Web Development (14)
search Search Inside
Programming
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


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