| | |
segmentation fault
Thread Solved |
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)
#include <windows.h> #include <process.h> #include <iostream> using namespace std; unsigned __stdcall Display(void* p) { Sleep(500); cout << "Display" << endl; cout << *((int*)p) << endl; return 0; } int main() { unsigned taddr = NULL; unsigned createFlag = 0; for(int i=0;i<5;i++) { if (_beginthreadex(NULL,0,Display,&i,createFlag,&taddr)) { cout << "success" << endl; } else cout << "failed" << endl; } cout << "main's execution is over" << endl; ExitThread(0); }
"A tree laden with fruits always bends low"
>>ExitThread(0);
delete that line in main()
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
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.
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"
•
•
•
•
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"
ExitThread() belongs inside a thread, not in main().
There are other methods available to keep main() alive until the threads terminate on their own.
•
•
•
•
ExitThread is the preferred method of exiting a thread in C code.
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include <process.h> #include <iostream> using namespace std; unsigned __stdcall Display(void* p) { Sleep(500); cout << "Display" << endl; cout << *((int*)p) << endl; return 0; } int main() { unsigned taddr = NULL; unsigned createFlag = 0; HANDLE handles[5] = {0}; for(int i=0;i<5;i++) { if (handles[i] = (HANDLE)_beginthreadex(NULL,0,Display,&i,createFlag,&taddr)) { cout << "success" << endl; } else cout << "failed" << endl; } WaitForMultipleObjects(5, handles, TRUE, INFINITE); cout << "main's execution is over" << endl; }
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
> 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.
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
--
If your code lacks code tags, you will be IGNORED
![]() |
Similar Threads
- segmentation fault (C)
- Access Violation (Segmentation Fault) + atol (C++)
- unix/C++ segmentation fault (C++)
- what is the best way to track segmentation fault errors (C++)
Other Threads in the C++ Forum
- Previous Thread: game
- Next Thread: Best practices question
Views: 722 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int lazy link linker list loop looping loops math matrix member memory newbie number object objects opengl operator output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference server sort spoonfeeding string strings struct student studio template templates text time tree undefined url variable vc++ vector video visual win32 window windows winsock wxwidgets






