hello All,
I am trying to write a multi threaded program in C++ , but it suddenly exits and i can not understand what happens , it actually do not get any error message or exceptions error , just get out of the program. need your help, thanks .

#include<windows.h>
void Create(DWORD);
static DWORD WINAPI ThreadFunc(LPVOID);
int main(){
............
}
void Create(DWORD ID){
HANDLE hThread;
  hThread=creatThread( Null, 0, ThreadFunc, Null, 0, &ID);
}
DWORD WINAPI  ThreadFunc(LPVOID param){
     TBS *ptbs = new TBS();
     ptbs->TbsStart();
     delete ptbs;
     return 0;
}

in "ThreadFunc" i make an object of TBS class , when the program reaches to this line exits , and sometimes in "ptbs->TbsStart()" exits , it seems it can not allocate memory to "TBS" but I'm not sure because i get no error message!
I'm really confused , please help me.

1. You can't COMPILE this snippet: no such API function as creatThread.
2. Your thread do nothing. It starts then dye in silence. What TBS.TbbStart() doing?
What's a true problem?
Obviously, insufficient info...

thanks for your attention ,
you know TBS is class that supports a Telephone Bank system code , it's too long , but before i make the thread it was working properly , actually i made a simulate of my code here ,and thats why i just put some dots in main, i wanna know if there is any problem with my CreatThread function or not ? when i put breakpoints in line

TBS *ptbs = new TBS(); [\code] and i go through the code with F11 it goes to header files for allocating the memory and suddenly drops out.[code = c++]
TBS *ptbs = new TBS();
[\code]
and i go through the code with F11 it goes to header files for allocating the memory and suddenly drops out.

Besides , my codes can be compiled and run , in run time when gets to TBS ... = new ... , it finishes.

i wanna know if there is any problem with my CreatThread function or not ?

I think you made I typo. It's Creat[b]e[/b]Thread instead of CreatThread , so: no. You code doesn't compile unless you have made some sort of custom CreatThread() function.

And without knowing what the TbsStart() functions does, it's kind of hard to tell why your program is shutting down.

May be, TBS constructor is not so long? If the program crashed in new TBS() then let's look at TBS::TBS()...

I am trying to write a multi threaded program in C++ , but it suddenly exits and i can not understand what happens , it actually do not get any error message or exceptions error , just get out of the program.

Could it be so simple that your main() simply exits after the call to Create() has returned?

> hThread=creatThread( Null, 0, ThreadFunc, Null, 0, &ID);
Yeah, there's a problem.
As soon as Create() returns, you've got a danging pointer to a local variable which no longer exists.

If you want to pass parameters to threads, the safest way is to allocate some memory, fill in the details you want to communicate, then pass that pointer (which will be persistent and unique) to the thread. The thread then frees that memory when it's done with it.

> and i go through the code with F11 it goes to header files for allocating the
> memory and suddenly drops out.
Any prior abuse of allocated memory could do this as well. If you've already trashed the pool (maybe even some time ago), then failure at any time is always an option.
Cause and effect (when it comes to dynamic memory in particular) are seldom in the same place. You need to start looking for a cause, rather than staring at the effect.

thanks all , i suppose the reason is what "mitrmkar" said , when it goes to thread , the CreateThread functions returns the handle , main continues its process , and reaches to return 0 , so tell me how can i make main to wait on threads , i do not know the instructions.
by the way CreatThread was an dictation error , i have used CreateThread function of windows.h header file.
realy thank you .

thank you mitrmkar , i will try to see if i can solve the problem or i have to ask more questions here :)

I need your help again ,
the whole algorithm is like this , i have 4 thread , they get started after each other , like below

//pseudo  code 
int main(){
   thread_1->start();
   thread_2->start();
   thread_3->start();
   thread_4->start();
}

, then the state is like this : my threads never die , 'cause i have an infinite loop in my threads , unless i finishes running state , but the main doesn't wait for my threads , i studied on wait functions , i.e WaitForMultipleObjects() , but i couldn't understand what should i do to make "main()" wait on all 4 threads forever unless they get corrupted or killed somehow.would you please help me again . thank you very much. :)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

mitrmkar and all other friend , thank you very much , i could manage the code and solve the problem by your help,
really thank you . then this thread is solved:) it's good for a new poster like me;)

This question has already been answered. Start a new discussion instead.