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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:29:40+00:00 2026-05-25T00:29:40+00:00

In a multi-threaded program I’m writing a custom print function which accepts a variable

  • 0

In a multi-threaded program I’m writing a custom print function which accepts a variable argument list.

void t_printf(char * str, ...)
{
    if(file_ptr != NULL)
    {
            va_list ap;
            va_start(ap, str);

            vfprintf(file_ptr, str, ap);

            va_end(ap);

            fflush(file_ptr);
    }
}

Inside this function I want to add the current thread id (using pthread_self()) to the message getting printed. How can I do it? Is there a way to add it to the existing va_list?

  • 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-25T00:29:41+00:00Added an answer on May 25, 2026 at 12:29 am

    With a variadic macro:

    With a variadic macro you can call the function with an argument prepended or appended:

    #define t_printf(format, args...) \
        _t_printf(format, thread_id, __VA_ARGS__);
    

    This prepends the thread_id before other arguments. (Note that on the _t_printf() function you have to modify the format string too.)

    If you do this:

    t_printf("some format string", a, b, c);
    

    This will expand do this:

    _t_printf("some format string", thread_id, a, b, c);
    

    If t_printf() is called without other argument that format, you will have a trailing comma. GCC has a ## extension that takes care of adding the comma as needed:

    #define t_printf(format, args...) \
        _t_printf(format, thread_id ##__VA_ARGS__);
    

    Complete solution with the macro:

    #define t_printf(format, args...) \
        _t_printf(format, thread_id, __VA_ARGS__);
    
    void _t_printf(char * str, ...)
    {
        if(file_ptr != NULL)
        {
                char format[1024];
    
                /* safely prefix the format string with [thread_id: %x] */
                snprintf(format, sizeof(format), "%s%s", "[thread_id: %x] ", str);
    
                va_list ap;
                va_start(ap, str);
    
                vfprintf(file_ptr, format, ap);
    
                va_end(ap);
    
                fflush(file_ptr);
        }
    }
    

    Without modifying the arguments

    An other solution is to do two printf()s:

    vsnprintf(buffer, bufsize, str, ap);
    vfprintf(file_ptr, "[thread_id: %x] %s", thread_id, buffer);
    

    Complete solution:

    void _t_printf(char * str, ...)
    {
        if(file_ptr != NULL)
        {
                char buffer[1024];
    
                va_list ap;
                va_start(ap, str);
    
                vsnprintf(buffer, sizeof(buffer), str, ap);
                vfprintf(file_ptr, "[thread_id: %x] %s", thread_id, buffer);
    
                va_end(ap);
    
                fflush(file_ptr);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am on MacOSX. I am writing a multi threaded program. One thread does
I am writing a multi-threaded program using OpenMP in C++. At one point my
I wrote a multi-threaded program which does some CPU heavy computation with a lot
I'am writing a multi-threaded program in C. Before creating the threads, a global python
I want to debug a multi-threaded program by controlling which threads execute when. I
I am writing a multi-threaded program in C where one core periodically grabs an
I have a simple multi-threaded program, with the following structure. public void someFunction() {
I am trying to debug (using gdb) a multi-threaded program, which uses POSIX threads.
I'm writing a wxwidget multi-threaded application. But the code crashes randomly of which I
I have implemented a multi-threaded program which involves spawning a thread for each user,and

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.