Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9153565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:17:34+00:00 2026-06-17T12:17:34+00:00

So I have been learning about threads in the Windows API, and I wrote

  • 0

So I have been learning about threads in the Windows API, and I wrote some test code, but I am have some issues. First off I have two functions:

DWORD WINAPI MyThreadFunction( LPVOID lpParam ) 
{
    int i = (int)lpParam;
    cout << i << ".) I work! " << endl;
    return 0;
}

And

// MyThreadFunction2 - loops continuously forever, outputing "working..." every 1ms
DWORD WINAPI MyThreadFunction2( LPVOID lpParam ) 
{
        int i = (int)lpParam;
    while(1) {
        cout << i << ".) Working... " << endl;
        Sleep(1);
    }
    return 0;
}

And I am creating threads using CreateThread, but I will change that later to _beginThread which is what people have told me to do in a different but related post. So this is how I have been creating threads:

    threads.push_back(CreateThread(
                                   NULL,           // default security attributes
                                   0,              // use default stack size
                                   lpStartAddress, // thread function name
                                   lpParameter,    // arugument to thread function
                                   0,              // use default creation flag
                                   NULL));         // ignore thread identifier.

I am putting the thread HANDLEs I create into a vector, but that is irrelevant. Basically I have the above code working, and it creates the threads fine. The problem I am having is calling TerminateThread(HANDLE, 0) when the thread was created using MyThreadFunction2, where it loops forever. When I call TerminateThread, my program just freezes and sits there. It appears the thread has stopped, because I don’t get the “Working…” message, but it doesn’t continue any other code. However, when I call this same function when it is with MyThreadFunction, it works fine.

I don’t know if this is extremely confusing how I am explaining what is going on, I hope it isn’t. I know this isn’t a place to “just dump code, and have people fix it” – I read that on someone else’s post, but just incase you didn’t understand a thing I wrote above, or it is helpful, here is my code:

ThreadManager.cpp

http://pastebin.com/0B1N3TAH

ThreadManager.h

http://pastebin.com/yfwMJTaz

Sorry the code is poorly written, and some things aren’t complete. I am still working on re-learning C++ and learning how to use the Windows API. But does anyone have any ideas of what I might possibly be doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:17:35+00:00Added an answer on June 17, 2026 at 12:17 pm

    Using TerminateThread to kill a thread is generally a really bad idea. Even MSDN page for this function tells you that. A better way to tell the threads to stop working is to pass them an event. With an event the thread that runs an infinite loop would do while (WaitForSingleObject(event, 1) == WAIT_TIMEOUT) instead of while (1).

    Why exactly TerminateThread does what it dies, I don’t know, but you can get an idea of what might be wrong when you read this http://msdn.microsoft.com/en-us/library/windows/desktop/ms686717(v=vs.85).aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been learning about various functional languages for some time now including Haskell,
Have been learning about MVP and have tried writing a test app using it
I have been learning about google maps api from here and did exactly what
I'm a solo developer and always have been, but I've been learning lately about
I have been neglecting learning about caching for quite some time now, and although
I have been learning about locking on threads and I have not found an
I have been learning about the basics of C# but haven't come across a
I have been learning about Delegation and Data Sources for iOS programming and need
Recently I have been learning about WMI and WQL. I found out the list
I have been learning the Definitive Guide of JavaScript.i got a question about the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.