简体   繁体   中英

Thread is not getting Destroyed

I am working on a Multithreaded system here's my code class demo is defined in .h file

when the loop from the main function is executed second time the COMMENT1 below takes the previous value

doesn't closing handle closes the thread?

int threadentry(void* data)
{
   demo* inst=(demo*) data;
   cout << "Value of inst  "<<hex << &inst<< endl;
   string request;
   cin>>request;
   if(request==play)
   {
      inst->play;  
      cout << "Value of inst  "<<hex << &inst<< endl;
      // COMMENT1 here when the thread is executed second time from the main it is taking previous value
   }
}

int main()
{
   while(1)
   {
      demo* inst=new demo();
      cout << "Value of inst  "<<hex << &inst<< endl;  //value is coming different from above
      HANDLE threads;
      DWORD threadId1;
      if ((threads = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadentry,
         (void *)inst, 0, &threadId1)) == NULL)
        return -1;
      //here is some Processing of data and after processing I close the handle
      CloseHandle(threads);
      delete inst;
      system("pause");
   }
}

No -- closing a handle to a thread does not destroy the thread itself. The thread should exit (either by calling ExitThread or by just returning from the thread function) when it's finished doing its job.

In emergencies, you can use TerminateThread to kill a thread, but that should be reserved for true emergencies -- it can leave the process in an unstable state, so it should generally be avoided, and if you have to use it, you probably want to shut down the process as soon afterwards as possible.

Also note that in a program that uses the standard library, it's not really safe to use CreateThread directly -- you should call _beginthread or _beginthreadex instead. These do some setup to allow thread-safe use of standard library functions that use static storage (eg, strtok and mktime , but there are quite a few more).

Drop all those "(type)foo" casts, they are forcing the compiler to accept things that in reality don't fit. You will have to fix a few errors there by replacing things with the proper type. For the context pointer passed to the thread, the conversion from demo* to void* is implicit. The correct cast to reverse this is static_cast<demo*>(data) . If you want, you can use the a static cast for the implicit conversion, too. There are missing return values in functions, too, the only case that is allowed is in main(). The reason I mention th s is that formally, anything can happen in your program, because these things cause undefined behaviour.

Then, you are outputting the "value of inst" but actually outputting the address of local variables called "inst", which is something different. This probably just adds to your confusion.

Now, coming to your problem, CloseHandle() does not stop the thread. It only releases your handle. What you want is WaitForSingleObject() or one of its brethren instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM