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

  • Home
  • SEARCH
  • 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 6754355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:16:12+00:00 2026-05-26T13:16:12+00:00

I’m looking for a tool which generates subroutine for checking a return code of

  • 0

I’m looking for a tool which generates subroutine for checking a return code of some other subroutine.

For example, pthread_create can returns 0, EAGAIN, EINVAL and EPERM codes. It would be nice to have such checker:

void pthread_create_check(int retcode) {
    switch (retcode) {
    case 0:
        printf("pthread_create success.\n");
        break;
    case EAGAIN:
        printf("pthread_create EAGAIN error: insufficient resources"
               " to create another thread, or a system-imposed"
               " limit on the number of threads was encountered.\n");
        break;
    case EINVAL:
        printf("pthread_create EINVAL error: invalid settings in"
               " attr.\n");
        break;
    case EPERM:
        printf("pthread_create EPERM error: no permission to set the"
               " scheduling policy and parameters specified in"
               " attr.\n");
        break;
    }
}

And use it in such manner:

iret = pthread_create(&thread_desc, 
                      NULL, 
                      thread_function, 
                      (void *) thread_param);
pthread_create_check(iret);

There are explanation of each error code in man page. Creating such checker is nothing but copy-paste error codes and explanation from man page. I think that computer can done this job much better than human since computer never get tire. Also, I’m too lazy to do it for every subroutine call. Is there any automation tool?

  • 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-26T13:16:12+00:00Added an answer on May 26, 2026 at 1:16 pm

    Just make message tables. It will save coding time and space.

    typedef struct pthread_message {
        int code;
        const char* text;
    } pthread_message;
    
    int pthread_check(int retcode, const char* fname, 
        pthread_message* messages)
    {
        if(!retcode) /* Makes the common case fast. */
        {
            fprintf(stderr, "%s success.\n", fname);
            return retcode;
        }
    
        /* Look for a message. */
        for(; messages->code; ++messages)
            if(messages->code == retcode)
            {
                fprintf(stderr, "%s %s\n", fname, message->text);
                return retcode;
            }
    
        /* Fall back on standard library. If you lack strerror_r, 
           then put a generic message here.
        */
        char buf[256];
        fprintf(stderr, "%s %s\n", fname, strerror_r(retcode, buf, 256));
        return retcode;
    );
    
    pthread_message pthread_create_messages[] = {
        { EAGAIN, "EAGAIN error: insufficient resources to create another thread,"
          " or a system-imposed limit on the number of threads was encountered." },
        { EINVAL, "EINVAL error: invalid settings in attr." },
        { EPERM, "EPERM error: no permission to set the scheduling policy and"
          " parameters specified in attr." },
        { 0, 0 } /* End of list. */
    };
    
    iret = pthread_check(pthread_create(arg1, arg2, ...), "pthread_create", 
        pthread_create_messages);
    

    There’s nothing stopping you from sharing message lists between functions, so you can write as little or as much as you want.

    If you’re insane, you can make a macro out of the call:

    #define PTHREAD_CHECK(fname, arglist) \
        (pthread_check(fname arglist, #fname, fname##_messages))
    
    iret = PTHREAD_CHECK(pthread_create, (arg1, arg2, ...));
    

    In this case, to share a message list means you need to create a pointer with the proper name for each additional function pointing to the list of the first function. Still a lot less work.

    For the record, I just wrote one check function with generic messages (except for the success messages, they’re spammy) and used it everywhere in my C++ wrapper around pthread. (Don’t carp at me about Boost, this was ten years ago.)

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.