Different standard streams per POSIX thread

Posted by Roman Nikitchenko on Stack Overflow See other posts from Stack Overflow or by Roman Nikitchenko
Published on 2010-05-19T18:08:44Z Indexed on 2010/05/19 18:10 UTC
Read the original article Hit count: 274

Filed under:
|
|
|
|

Is there any possibility to achieve different redirections for standard output like printf(3) for different POSIX thread? What about standard input?

I have lot of code based on standard input/output and I only can separate this code into different POSIX thread, not process. Linux operation system, C standard library. I know I can refactor code to replace printf() to fprintf() and further in this style. But in this case I need to provide some kind of context which old code doesn't have.

So doesn't anybody have better idea (look into code below)?

#include <pthread.h>
#include <stdio.h>

void* different_thread(void*)
{
    // Something to redirect standard output which doesn't affect main thread.
    // ...

    // printf() shall go to different stream.
    printf("subthread test\n");

    return NULL;
}

int main()
{
    pthread_t id;
    pthread_create(&id, NULL, different_thread, NULL);

    // In main thread things should be printed normally...
    printf("main thread test\n");

    pthread_join(id, NULL);
    return 0;
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about posix