Do you mean all threads should log to the same file? As Bayint mentioned, fprintf is thread safe, i.e. there are no static local variables that are shared across threads, that could cause problems with mixed data.
If you're asking about preventing one thread from writing to the log file until the other is done, then no, fprintf doesn't lock a file like that. You can happily have several threads each writing to the same file, and their output will be interleaved. The threads execute in an unspecified order, and you don't know if the output buffer of one will be flushed before switching to the next thread, which may write it's output and flush the buffer to file.
You can protect this with a mutex however, so that each thread is guaranteed to write it's full log message to the file without interleaving. Here's a test program that will show the difference between using a mutex and not using one. It uses stdout, but the effects are the same as writing to a file.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#define NUM_THREADS 5
static int do_mutex;
static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
static void *print_hello(void *thread_id)
{
int i;
long tid = (long) thread_id;
if (do_mutex) {
pthread_mutex_lock(&mut);
}
for (i = 0; i < 3; i++) {
usleep(rand() % 100000); // kill a little time
printf("Thread %ld, loop %d\n", tid, i);
}
if (do_mutex) {
pthread_mutex_unlock(&mut);
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int rc;
long t;
pthread_t threads[NUM_THREADS];
if (argc > 2) {
fprintf(stderr, "Usage: threads [mutex]\n");
fprintf(stderr, "\twhere 'mutex' causes the thread to lock "
"until all 3 loops are done\n");
exit(1);
}
do_mutex = (argc == 2) && !strcmp(argv[1], "mutex");
srand(time(NULL));
for (t = 0; t < NUM_THREADS; t++) {
rc = pthread_create(&threads[t], NULL, print_hello, (void *) t);
if (rc) {
perror("pthread_create");
exit(1);
}
}
pthread_exit(NULL);
return 0;
}