943,920 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 926
  • C++ RSS
Dec 18th, 2008
0

segmentation fault

Expand Post »
This code here is giving segmentation fault. Any clues why? also any general tips for writing good multi-threaded code are welcome.

c++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <process.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. unsigned __stdcall Display(void* p)
  8. {
  9. Sleep(500);
  10. cout << "Display" << endl;
  11. cout << *((int*)p) << endl;
  12. return 0;
  13. }
  14. int main()
  15. {
  16. unsigned taddr = NULL;
  17. unsigned createFlag = 0;
  18.  
  19. for(int i=0;i<5;i++)
  20. {
  21. if (_beginthreadex(NULL,0,Display,&i,createFlag,&taddr))
  22. {
  23. cout << "success" << endl;
  24. }
  25. else
  26. cout << "failed" << endl;
  27. }
  28.  
  29. cout << "main's execution is over" << endl;
  30. ExitThread(0);
  31. }
Similar Threads
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Dec 18th, 2008
0

Re: segmentation fault

>>ExitThread(0);
delete that line in main()
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
Dec 18th, 2008
0

Re: segmentation fault

>>ExitThread(0);
delete that line in main()
But if i do that the threads never execute as 'main' terminates and all the threads die. Any reason why you think it might help?

I found a possible solution to the problem, since the threads would sleep in 'Display' and by the time they wake up, main exits hence 'i' doesn't exist anymore and when i try to print the value it crashes. Actually i executed it once without the 'sleep' and it went through fine. Though i think it might still crash some other time when main exits before any of the threads is left incomplete.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Dec 18th, 2008
0

Re: segmentation fault

It doesn't matter that 'i' doesn't exist when main() ends and the thread is using it, simply because when main() ends the whole program shuts down, including the threads.
Last edited by unbeatable0; Dec 18th, 2008 at 8:23 am. Reason: Have written 'than' instead of 'that'
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Dec 18th, 2008
0

Re: segmentation fault

It doesn't matter than 'i' doesn't exist when main() ends and the thread is using it, simply because when main() ends the whole program shuts down, including the threads.

The threads wont shut down because I'm using 'ExitThread', if I'm not wrong its the equivalent of pthread_exit for Unix.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Dec 18th, 2008
0

Re: segmentation fault

ExitThread() belongs inside a thread, not in main().
Quote ...
ExitThread is the preferred method of exiting a thread in C code.
There are other methods available to keep main() alive until the threads terminate on their own.
C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <process.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6. unsigned __stdcall Display(void* p)
  7. {
  8. Sleep(500);
  9. cout << "Display" << endl;
  10. cout << *((int*)p) << endl;
  11. return 0;
  12. }
  13. int main()
  14. {
  15. unsigned taddr = NULL;
  16. unsigned createFlag = 0;
  17. HANDLE handles[5] = {0};
  18. for(int i=0;i<5;i++)
  19. {
  20. if (handles[i] = (HANDLE)_beginthreadex(NULL,0,Display,&i,createFlag,&taddr))
  21. {
  22. cout << "success" << endl;
  23. }
  24. else
  25. cout << "failed" << endl;
  26. }
  27. WaitForMultipleObjects(5, handles, TRUE, INFINITE);
  28.  
  29. cout << "main's execution is over" << endl;
  30. }
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
Dec 18th, 2008
1

Re: segmentation fault

> Any clues why?
Because you passed a POINTER to i.
The main() thread exits, the i variable goes out of scope and another thread is looking down the barrel of a segfault.

> also any general tips for writing good multi-threaded code are welcome.
Well the first step is to make sure that whatever parameter you pass has a lifetime which exceeds the thread creator.

Using dynamic memory, and then making the thread the owner of that memory is one way to achieve this.
This also solves the other problem of helping to ensure that only one thread has access to the memory passed into it.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

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: game
Next Thread in C++ Forum Timeline: Best practices question





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


Follow us on Twitter


© 2011 DaniWeb® LLC