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
  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
  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
  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 76 Days 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 77 Days 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.
[VB] Hello World
HTML Cheat Sheet!
[C++] Comments
[C++] Functions
[C++]Taking In User Input,
Your First C++ Program
Recently Posted "Programming" Articles
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
Quick Threading Tutorial - C++
C++ And Me (A Love Story)
First look at C
Introduction to Pseudo Code
Integrating your website with PHP
Recently Rated "Programming" Articles
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
N-Queens-Series Part I (Only the fittest survive)
[PHP] Create A Unique Page Hits Counter
Currying in JavaScript: Fun for the Whole Family!
Actionscript Events
First look at C
if/else, switch/case and ?/: Statements In Actionscript 2.0
Actionscript API
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Operating Systems (3)
Other (2)
Programming (48)
Security (10)
Software Development (5)
Web Development (15)
search Search Inside
Programming
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved