943,955 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1230
  • C++ RSS
Mar 26th, 2008
0

Need help with threads in C++

Expand Post »
Hi I need an example of how threads are coded and implemented in Dev-C++, any help would be greatly appreciated.
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Mar 26th, 2008
0

Re: Need help with threads in C++

Try this and see if it works..... But the problem is some libraries does not support
(#include "XYLock.h")


C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Mar 26th, 2008
0

Re: Need help with threads in C++

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 5th, 2008
0

Re: Need help with threads in C++

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! (^ _ ^)
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Apr 5th, 2008
1

Re: Need help with threads in C++

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).
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Apr 6th, 2008
0

Re: Need help with threads in C++

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!
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Apr 6th, 2008
0

Re: Need help with threads in C++

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 17th, 2008
0

Re: Need help with threads in C++

Hey doug, thanks for the code, worked like a charm. Now I know how to use threads! Yeehaw!!
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: rand(); always ='s 41?
Next Thread in C++ Forum Timeline: Help with adding 2 arrays of different sizes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC