Need help with threads in C++

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Need help with threads in C++

 
0
  #1
Mar 26th, 2008
Hi I need an example of how threads are coded and implemented in Dev-C++, any help would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Need help with threads in C++

 
0
  #2
Mar 26th, 2008
Try this and see if it works..... But the problem is some libraries does not support
(#include "XYLock.h")


  1. #include <stdio.h>
  2. #include <iostream>
  3. #include "XYLock.h"
  4.  
  5. XYCriticalSection myCriticalSection;
  6.  
  7. void ThreadProc(void *dummy)
  8. {
  9. while(true)
  10. {
  11. try
  12. {
  13. XYLock myLock(&myCriticalSection);
  14. printf("Thread '%X' has the lock\n",::GetCurrentThreadId());
  15. throw "a test";
  16. }
  17. catch(char* p)
  18. {
  19. printf("Caught: %s\n",p);
  20. }
  21. ::Sleep(1000);
  22. }
  23. }
  24.  
  25. void main()
  26. {
  27. for(int i=0;i<100;i++)
  28. {
  29. if(_beginthread(ThreadProc,0,NULL)==-1)
  30. {
  31. printf("Failed to create a thread\n");
  32. return;
  33. }
  34. }
  35.  
  36. while(true)
  37. {
  38. try
  39. {
  40. XYLock myLock(&myCriticalSection);
  41. printf("The main thread '%X' has the lock\n",::GetCurrentThreadId());
  42. throw "a test from the main thread";
  43. }
  44. catch(char* p)
  45. {
  46. printf("Caught: %s\n",p);
  47. }
  48. ::Sleep(1000);
  49. }
  50. }
Last edited by Traicey; Mar 26th, 2008 at 11:08 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,455
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Need help with threads in C++

 
0
  #3
Mar 26th, 2008
Threads are not natively supported by the c++ language but there are several api functions and libraries that you can use. One of them is win32 api function CreateThread()

Also see this thread

[edit]^^^ Tracey's example is excellent too.[/edit]
Last edited by Ancient Dragon; Mar 26th, 2008 at 11:11 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Re: Need help with threads in C++

 
0
  #4
Apr 5th, 2008
Thanks tracey, but I need help with a few things. First of all, I don't understand the code (I'm new a C++) so could you explain it to me? Second, what s the code supoosed to do? Thirdly, if I wanted to wait on an input from the user for a limited amount of ime, would I have to use threads and how could this be accomplished? Oh, one more thing my library doesn't have XYLock.h, so if I were to get a copy from you and link it to my library, would it work? Thank a lot! for your time! (^ _ ^)
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Need help with threads in C++

 
1
  #5
Apr 5th, 2008
As AncientDragon said, CreateThread() can be used in windows. Check it out on MSDN. Following is some code to show you how to use it. Note that the thread proc should be of the form DWORD WINAPI myThreadProc(void*someData) . someData is a pointer passed by CreateThread to your thread proc (I've set it to 0 in the following example).
  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. DWORD WINAPI myThreadProc(void*someData){
  7. while(1){
  8. cout << "myThreadProc says hi" << endl;
  9. Sleep(1000);
  10. }
  11. }
  12.  
  13. main(){
  14. CreateThread(0, 0, myThreadProc, 0, 0, 0);
  15.  
  16. for(int i = 0; i < 10; i++){
  17. cout << "main says hi" << endl;
  18.  
  19. Sleep(1500);
  20. }
  21. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Re: Need help with threads in C++

 
0
  #6
Apr 6th, 2008
Hey doug, thanx for your suggestion, it complied and worked perfectly, but I don't really understand how the program works and I was wondering, how an example like this would work if an input from the user was required.

Correct me if I'm wrong, but based on the output that I'm getting, and looking at the code itself, it seems like the computer is switvching between tasks. Am I right?

(^ _ ^) Thanx!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,455
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Need help with threads in C++

 
0
  #7
Apr 6th, 2008
>>it seems like the computer is switvching between tasks. Am I right?
Yes. Unless you have a duel processing computer then the os can only do one thing at the same exact time. It is just an illusion that it looks like two or more things run at the same time. The operating system contains a task switcher that gives only a small amount of CPU time to each program at a time. It has an array of threads that have to be serviced and switches the CPU between them in round-robin fashion. So when you call CreateThread you just add another thread to that list in the task scheduler.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 65
Reputation: CodeBoy101 is an unknown quantity at this point 
Solved Threads: 0
CodeBoy101's Avatar
CodeBoy101 CodeBoy101 is offline Offline
Junior Poster in Training

Re: Need help with threads in C++

 
0
  #8
Apr 17th, 2008
Hey doug, thanks for the code, worked like a charm. Now I know how to use threads! Yeehaw!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC