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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:21:06+00:00 2026-06-09T19:21:06+00:00

I need to implement the same functionality as this function on Win7 x64. I

  • 0

I need to implement the same functionality as this function on Win7 x64.

I initially used SwitchToThread() but this doesn’t work as it causes a deadlock under extreme conditions. The only alternative I can find is Sleep() yet this is likely to be a performance killer as it only work on millisecond resolution and I’m still not sure it does the same thing as LockSupport.parkNanos().

I found Java’s ability to schedule (if that is what happens) threads on a nanosecond interval suspicious so I implemented what I could only assume they do… spin. However I’m not sure this solves the problem, it may be merely delaying the inevitable as it seems the Java function requires the intervention of the JVM to work. No source code for parkNanos is available; it’s implemented in a native Sun library.

class LockSupport
{
public:
    static void ParkNanos(unsigned __int64 aNanos)
    {
        ULONGLONG start;
        ULONGLONG end;

        ::QueryUnbiasedInterruptTime(&start);
        do
        {
            // My issue with this is that nothing is actually 'Parked'.
            ::SwitchToThread();
            ::QueryUnbiasedInterruptTime(&end);
        }
        while ((end - start) < aNanos);
    }
};

The calling code looks like this:

void SomeClass::SomeFunction()
{
    while (someCond)
    {
        LockSupport.parkNanos(1L);
    }
}

FWIW, I am porting LMAX’s Disruptor pattern to C++. The deadlock is happening when one thread is in SingleThreadedClaimStrategy::WaitForFreeSlotAt() and another is in BlockingWaitStrategy::WaitFor (no timeout). The deadlock is more apparent when the RingBuffer size is tiny… 1, 2, 4, 8 etc.

The threads are created by the normal CreateThread means.

Edit: It was quite late when I wrote this so here’s some more information.
The RingBuffer holds __int64s. I have one Producer thread and one Consumer thread. The Consumer thread also spawns off a Timer thread that polls the Consumer every second for the sequence number of the event it last consumed. There comes a point when the Consumer makes no progress and the Producer hasn’t finished either. The Producer merely runs in a loop a few hundred million times publishing a counter. So my output looks something like this:

898
97
131
Timer: no progress
Timer: no progress
...

It’s only really reproducible in Release Mode, with everything optimised for speed.

  • 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-09T19:21:08+00:00Added an answer on June 9, 2026 at 7:21 pm

    Besides the ability to unpark() a thread, LockSupport.parkNanos(...) is nothing more than a sleep. In the OpenJDK Hotspot VM on Windows, it’s implemented (line 4436) using WaitForSingleObject(...), and sleeps for a minimum of 1ms.

    LMAX disruptor doesn’t ever seem to unpark() threads. Consequently, you should get equivalent behavior by calling Sleep(1). You could possibly do better with Sleep(0): you give up the rest of your time slice in the current thread, and become available for rescheduling immediately. This is equivalent to SwitchToThread() with the exception that the latter may simply tell you “nothing ready to run yet, so you can keep the cpu.” On the other hand, Sleep(1) may actually pause for 1 ms if your scheduling granularity is sufficiently low.

    In the remarks to Sleep() it is noted that you can improve the scheduling granularity of your system (possibly down to as little as 1ms per tick) by invoking timeBeginPeriod().

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

Sidebar

Related Questions

I'm new to WPF but I need to implement following functionality: I have a
In my Django app, I need to implement this timer-based functionality: User creates some
I need to implement in my class Invoke() method with the same behavior as
I need to implement portable code, but I do not know how to deal
I need to have a custom view controller with the same functionality as the
I need to implement functionality of nightly process in my applications billing module. See
I'm trying to implement functionality to cache certain pages depending on the host. This
I need to intercept the browser-reload-functionality in Safari (I know this is usually not
This seems possible, but I'm having a little trouble figuring out how to implement
I have 2 JTextFields: JTextField txtJobType, txtPriorityCode; This is the functionality I need: When

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.