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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:14:06+00:00 2026-05-20T18:14:06+00:00

I have written a compiler and interpreter for a scripting language. The interpreter is

  • 0

I have written a compiler and interpreter for a scripting language. The interpreter is a DLL (‘The Engine’) which runs in a single thread and can load many 100s or 1000s of compiled byte-code applications and excecute them as a set of internal processes. There is a main loop that excecutes a few instructions from each of the loaded app processes before moving one to the next process.

The byte code instruction in the compiled apps can either be a low level instructions (pop, push, add, sub etc) or a call to an external function library (which is where most of the work is done). These external libararies can call back to the engine to put the internal processes into a sleep state waiting for a particular event upon which the external function (probably after receiving an event) will wake up the internal process again. If all internal processes are in a sleep state (which the are most of the time) then I can put the Engine to sleep as well thus handing off the CPU to other threads.

However there is nothing to prevent someone writing a script which just does a tight loop like this:

while(1)
x=1;
endwhile

Which means my main loop will never enter a sleep state and so the CPU goes up to 100% and locks up the system. I want my engine to run as fast as possibly, whilst still handling windows events so that other applications are still responsive when a tight loop similar to the above is encountered.

So my first question is how to add code to my main loop to ensure windows events are handled without slowing down the main engine which should run at the fastest speed possible..

Also it would be nice to be able to set the maximum CPU usage my engine can use and throttle down the CPU usage by calling the occasional Sleep(1)..

So my second question is how can I throttle down then CPU usage to the required level?

The engine is written in Borland C++ and makes calls to the win32 API.

Thanks in advance

  • 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-20T18:14:07+00:00Added an answer on May 20, 2026 at 6:14 pm

    1. Running a message loop at the same time as running your script

    I want my engine to run as fast as
    possibly, whilst still handling
    windows events so that other
    applications are still responsive when
    a tight loop similar to the above is
    encountered.

    The best way to continue running a message loop while performing another operation is to move that other operation to another thread. In other words, move your script interpreter to a second thread and communicate with it from your main UI thread, which runs the message loop.

    When you say Borland C++, I assume you’re using C++ Builder? In this situation, the main thread is the only one that interacts with the UI, and its message loop is run via Application->Run. If you’re periodically calling Application->ProcessMessages in your library callbacks, that’s reentrant and can cause problems. Don’t do it.

    One comment to your question suggested moving each script instance to a separate thread. This would be ideal. However, beware of issues with the DLLs the scripts call if they keep state – DLLs are loaded per-process, not per-thread, so if they keep state you may encounter threading issues. For the moment purely to address your current question, I’d suggest moving all your script execution to a single other thread.

    You can communicate between threads many ways, such as by posting messages between them using PostMessage or PostThreadMessage. Since you’re using Borland C++, you should have access to the VCL. It has a good thread wrapper class called TThread. Derive from this and put your script loop in Execute. You can use Synchronize (blocks waiting) or Queue (doesn’t block; method may be run at any time, when the target thread processes its message loop) to run methods in the context of another thread.

    As a side note:

    so that other
    applications are still responsive when
    a tight loop similar to the above is
    encountered.

    This is odd. In a modern, preemptively multitasked version of Windows other applications should still be responsive even when your program is very busy. Are you doing anything odd with your thread priorities, or are you using a lot of memory so that other applications are paged out?

    2. Handling an infinite loop in a script

    You write:

    there is nothing to prevent someone
    writing a script which just does a
    tight loop like this:

    while(1) x=1; endwhile

    Which means my main loop will never
    enter a sleep state and so the CPU
    goes up to 100% and locks up the
    system.

    but phrase how to handle this as:

    Also it would be nice to be able to
    set the maximum CPU usage my engine
    can use and throttle down the CPU
    usage by calling the occasional
    Sleep(1)..

    So my second question is how can I
    throttle down then CPU usage to the
    required level?

    I think you’re taking the wrong approach. An infinite loop like while(1) x=1; endwhile is a bug in the script, but it should not take down your host application. Just throttling the CPU won’t make your application able to handle the situation. (And using lots of CPU isn’t necessarily a problem: if it the work is available for the CPU to run, do it! There’s nothing holy about using only a bit of your computer’s CPU. It’s there to use after all.) What (I think) you really want is to be able to continue to have your application able to respond when running this script (solved by a second thread) and then:

    1. Detect when a script is ‘not responding’, or not calling into your callbacks

    2. Be able to take action, such as asking the user if they want to terminate the script

    An example of another program that does this is Firefox. If you go to a page with a misbehaving script, eventually you’ll get a dialog asking if you want to stop the script running.

    Without knowing more about how your script is actually interpreted or run, I can’t give a detailed answer to these two. But I can suggest an approach, which is:

    1. Your interpreter probably runs a loop, getting the next instruction and executing it. Your interactivity is currently provided by a callback running from one of those instructions being executed. I’d suggest making use of that by having your callback simply log the time it was last called. Then in your processing thread, every instruction (or every ten or a hundred) check the current time against the last callback time. If a long time has passed, say fifteen or thirty seconds, it may be an indication that the script is stuck. Notify the main thread but keep processing.

      For “time”, something like GetTickCount is probably sufficient.

    2. Next step: Your main UI thread can react to this by asking the user what to do. If they want to terminate the script, communicate with the script thread to set a flag. In your script processing loop, again every instruction (or hundred) check for this flag, and if it’s set, stop.

      When you move to having one thread per script interpreter, you TThread‘s Terminated flag for this. Idiomatically for something that runs infinitely in a thread, you run in a while (!Terminated && [any other conditions]) loop in your Execute function.

    To actually answer your question about using less CPU, the best approach is probably to change your thread’s priority using SetThreadPriority to a lower priority, such as THREAD_PRIORITY_BELOW_NORMAL. It will still run if nothing else needs to run. This will affect your script’s performance. Another approach is to use Sleep as you suggest, but this really is artificial. Perhaps SwitchToThread is slightly better – it yields to another thread the OS chooses. Personally, I think the CPU is there to use, and if you solve the problem of an interactive UI and handling out-of-control scripts then there should be no problem with using all CPU if your script needs it. If you’re using “too much” CPU, perhaps the interpreter itself could be optimised. You’ll need to run a profiler and find out where the CPU time is being spent.

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

Sidebar

Related Questions

I have written an application in C, which runs as a Windows service. Most
i have written a hexdump utility in assembly language using nasm compiler and ld
I have just been re-working an old bit of compiler-like code written using bison.
I have written a ruby script which opens up dlink admin page in firefox
I have written an AppleScript which when supplied with a Windows network link, will
I have written a DLL that uses MS Word to spell check the content
I have written a compiler for C that outputs byte code. The reason for
I've written an interpreter for my experimental language and know I want to move
I have an application which is written in VC++ using VC 6.0 version. Now
I have written a VC++ dll. The declaration for one of the methods in

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.