I have multi thread application in which I’m creating a thread like this:
int main(int argc,char *argv[])
{
pthread_t thread_id[argc-1];
int i;
struct parameter thread_data[argc-1];
int status;
for(i=0;i<argc-1;i++)
{
thread_data[i].ip_filename = argv[i+1];
strcpy (thread_data[i].op_filename,argv[i+1]);
strcat (thread_data[i].op_filename,".h264");
}
for(i=0;i<argc-1;i++)
{
pthread_create (&thread_id[i], NULL , &thread_function, &thread_data[i]);
}
}
Now in the thread function, I want to redirect stderr & stdout in one separate file as per thread. Something like a thread log file.
How can I do so?
Edit:
If thread specific prints can be displayed on different terminal..? I mean if there are 2 threads then it opens 2 terminals & prints each threads data on different terminals.
If you really must do this…
First you need to create 2
pthread_key_ts, one forstdoutand one forstderr. These can be created usingpthread_key_create, and they must be accessable from all threads. Let’s call themstdout_keyandstderr_key.When a thread is being created:
and then in your header file:
then just use:
I don’t recommend this approach though.