2

Today I started working on a project where I need use thread's, I'm using functions from process.h: _beginthread and _endthread.

My question is, I really need use _endthread(); at the end of function?

void LGThread(void *null_ptr) {
 /* ... code ...*/
 _endthread();
}
void main() {
 _beginthread(LGThread, NULL, NULL);
}

Or even with:

void LGThread(void *null_ptr) {
 /* ... code ...*/
}
void main() {
 _beginthread(LGThread, NULL, NULL);
}

I'm fine? What it does specially?

ernilos
  • 53
  • 1
  • 4

2 Answers2

4

You don't need it. And in a C++ program it can be harmfull: It does not return, so destructors will not be called for objects allocated on the stack in the thread function.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
2

You are using Windows functions. In C++, the standard is std::thread from <thread>.

That said, you don't need _endthread just like you don't need exit(0). Just returning is sufficient. [edit] Scott has a point, returning is even better as that does run destructors. I.e. _endthread is actually more like abort().

MSalters
  • 173,980
  • 10
  • 155
  • 350