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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:40:59+00:00 2026-05-27T02:40:59+00:00

I had an earlier question about integrating Mathematica with functions written in C++. This

  • 0

I had an earlier question about integrating Mathematica with functions written in C++.

This is a follow-up question:

If the computation takes too long I’d like to be able to abort it using Evaluation > Abort Evaluation. Which of the technologies suggested in the answers make it possible to have an interruptible C-based extension function? How can “interruptibility” be implemented on the C side?

I need to make my function interruptible in a way which will corrupt neither it, nor the Mathematica kernel (i.e. it should be possible to call the function again from Mathematica after it has been interrupted)

  • 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-27T02:41:00+00:00Added an answer on May 27, 2026 at 2:41 am

    For MathLink – based functions, you will have to do two things (On Windows): use MLAbort to check for aborts, and call MLCallYieldFunction, to yield the processor temporarily. Both are described in the MathLink tutorial by Todd Gayley from way back, available here.

    Using the bits from my previous answer, here is an example code to compute the prime numbers (in an inefficient manner, but this is what we need here for an illustration):

    code = 
    "
    #include <stdlib.h>
    
    extern void primes(int n);
    
    static void yield(){
        MLCallYieldFunction(
            MLYieldFunction(stdlink), 
            stdlink,
           (MLYieldParameters)0 );
    }
    
    static void abort(){
        MLPutFunction(stdlink,\" Abort \",0);
    }
    
    void primes(int n){
        int i = 0, j=0,prime = 1, *d = (int *)malloc(n*sizeof(int)),ctr = 0;    
        if(!d) {
           abort();
           return;
        }
        for(i=2;!MLAbort && i<=n;i++){
            j=2;
            prime = 1;      
            while (!MLAbort && j*j <=i){
                if(i % j == 0){
                    prime = 0;
                    break;
                }
                j++;
            }
            if(prime) d[ctr++] = i;
            yield();
        }
        if(MLAbort){
            abort();
            goto R1;
        }
    
        MLPutFunction(stdlink,\"List\",ctr);
        for(i=0; !MLAbort && i < ctr; i++ ){
            MLPutInteger(stdlink,d[i]);
            yield();        
        }
        if(MLAbort) abort();
    
     R1: free(d);
     }
     ";
    

    and the template:

    template = 
    "
    void primes P((int ));
    
    :Begin:
    :Function:       primes
    :Pattern:        primes[n_Integer]
    :Arguments:      { n }
    :ArgumentTypes:  { Integer }
    :ReturnType:     Manual
    :End:
    ";
    

    Here is the code to create the program (taken from the previous answer, slightly modified):

    Needs["CCompilerDriver`"];
    fullCCode = makeMLinkCodeF[code];
    projectDir = "C:\\Temp\\MLProject1";
    If[! FileExistsQ[projectDir], CreateDirectory[projectDir]]
    pname = "primes";
    files = MapThread[
       Export[FileNameJoin[{projectDir, pname <> #2}], #1, 
         "String"] &, {{fullCCode, template}, {".c", ".tm"}}];
    

    Now, here we create it:

    In[461]:= exe=CreateExecutable[files,pname];
    Install[exe]
    
    Out[462]= LinkObject["C:\Users\Archie\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\
    Windows-x86-64\primes.exe",161,10]
    

    and use it:

    In[464]:= primes[20]
    Out[464]= {2,3,5,7,11,13,17,19}
    
    In[465]:= primes[10000000]
    Out[465]= $Aborted
    

    In the latter case, I used Alt+”.” to abort the computation. Note that this won’t work correctly if you do not include a call to yield.

    The general ideology is that you have to check for MLAbort and call MLCallYieldFunction for every expensive computation, such as large loops etc. Perhaps, doing that for inner loops like I did above is an overkill though. One thing you could try doing is to factor the boilerplate code away by using the C preprocessor (macros).

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

Sidebar

Related Questions

I had asked a question about this earlier, but it didn't get answered right
This is a follow up to my earlier question . Tomcat 5.0.28 had a
I had this question earlier and it was concluded it was a bug in
Ok, I asked about this very error earlier this week and had some very
i had asked earlier about the same kind of question but that was in
This is a follow-up to another question I asked earlier today. I am creating
This is a better understanding of a question I had earlier. I have the
Earlier this week I ask a question about filtering out duplicate values in sequence
So, I had a question earlier about mod_rewrite which you find here mod_rewrite changing
I had asked a question earlier( How to keep this counter from reseting at

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.