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 7001725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:50:23+00:00 2026-05-27T20:50:23+00:00

In Win32 I want to suspend a thread using Suspend(GetCurrentThread()); but I find I

  • 0

In Win32 I want to suspend a thread using Suspend(GetCurrentThread()); but I find I cannot resume it using ResumeThread(suspend thread handle); But I find nothing happened.

Here it’s my code.

HANDLE C;
DWORD WINAPI A (LPVOID in)
{
    C = GetCurrentThread();
    cout << "1";
    SuspendThread (C);
    cout << "4";
    return 0;
}
DWORD WINAPI B (LPVOID in)
{
    Sleep (200);
    cout << "2";
    ResumeThread (C);
    cout << "3";
    return 0;
}
int main()
{
    CreateThread (NULL, 0, A, NULL, 0, NULL);
    CreateThread (NULL, 0, B, NULL, 0, NULL);
    Sleep (INFINITE);
    return 0;
}

And all I get on screen is 123.

  • 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-05-27T20:50:23+00:00Added an answer on May 27, 2026 at 8:50 pm

    It is possible right now that when B calls ResumeThread, the variable C contains an uninitialized value.

    However, the current reason why your code does not work is that GetCurrentThread only returns a pseudo-thread handle, a value interpreted to mean the current thread handle. To get the real one which can be used from other threads, you can take the one from the return of the first CreateThread call or convert the pseudo-handle with DuplicateHandle.

    Edit: Using method 1:

    HANDLE C;
    DWORD WINAPI A (LPVOID in)
    {
        cout << "1";
        SuspendThread (C);
        cout << "4";
        return 0;
    }
    DWORD WINAPI B (LPVOID in)
    {
        Sleep (200);
        cout << "2";
        ResumeThread ((HANDLE)in);
        cout << "3";
        return 0;
    }
    int main()
    {
        C = CreateThread (NULL, 0, A, NULL, 0, NULL);
        CreateThread (NULL, 0, B, (LPVOID)C, 0, NULL);
        Sleep (INFINITE);
        return 0;
    }
    

    In fact there is another problem with your code which is that handles returned from CreateThread are being ignored when they should be closed. Also there is a lack of error checking but I have assumed you’ve omitted that for brevity.

    You should also note that, depending on the timing of the context switch it is actually possible for the above code to output:

    1243

    Using method 2:

    HANDLE C = NULL;
    DWORD WINAPI A (LPVOID in)
    {
        C = GetCurrentThread();
        DuplicateHandle( GetCurrentProcess(), C, GetCurrentProcess(), &C, 0, FALSE, DUPLICATE_SAME_ACCESS );
        cout << "1";
        SuspendThread (C);
        cout << "4";
        return 0;
    }
    DWORD WINAPI B (LPVOID in)
    {
        Sleep (200);
        cout << "2";
    
        while( C == NULL ) {
          Sleep(100);
        }
        ResumeThread(C);
        cout << "3";
        return 0;
    }
    int main()
    {
        CreateThread (NULL, 0, A, NULL, 0, NULL);
        CreateThread (NULL, 0, B, NULL, 0, NULL);
        Sleep (INFINITE);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using Win32 API. i have a thread in c and want to
I using the Win32 API and C/C++. I have a HFONT and want to
I'm using GNU Emacs on Win32. I want to be able to run jslint
At the moment, our application uses the Trident Win32 component, but we want to
I want to launch an application using win32 ... Please let me know your
I have some C++ Win32 code that I want to call from Ruby. But
I'm using a Win32 Header. I want to make it so that the header
In Win32, you can create a thread in suspended mode, by using the dwCreationFlags
Win32's CreateFile has FILE_FLAG_DELETE_ON_CLOSE , but I'm on Linux. I want to open a
I want to load Win32 API functions using Runtime.loadLibrary and GetProcAddress(...) . Using mixin

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.