segmentation fault

Thread Solved

Join Date: Dec 2007
Posts: 531
Reputation: Agni is just really nice Agni is just really nice Agni is just really nice Agni is just really nice 
Solved Threads: 92
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro

segmentation fault

 
0
  #1
Dec 18th, 2008
This code here is giving segmentation fault. Any clues why? also any general tips for writing good multi-threaded code are welcome.

  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. }
"A tree laden with fruits always bends low"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,605
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: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: segmentation fault

 
0
  #2
Dec 18th, 2008
>>ExitThread(0);
delete that line in main()
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 531
Reputation: Agni is just really nice Agni is just really nice Agni is just really nice Agni is just really nice 
Solved Threads: 92
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro

Re: segmentation fault

 
0
  #3
Dec 18th, 2008
Originally Posted by Ancient Dragon View Post
>>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.
"A tree laden with fruits always bends low"
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: segmentation fault

 
0
  #4
Dec 18th, 2008
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 7:23 am. Reason: Have written 'than' instead of 'that'
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 531
Reputation: Agni is just really nice Agni is just really nice Agni is just really nice Agni is just really nice 
Solved Threads: 92
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro

Re: segmentation fault

 
0
  #5
Dec 18th, 2008
Originally Posted by unbeatable0 View Post
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.
"A tree laden with fruits always bends low"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,605
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: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: segmentation fault

 
0
  #6
Dec 18th, 2008
ExitThread() belongs inside a thread, not in main().
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.
  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. }
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,601
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 852
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: segmentation fault

 
1
  #7
Dec 18th, 2008
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Reply

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




Views: 722 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC