Solved

Writing to a file in multiple threads

Posted on 2004-10-07
2
324 Views
Last Modified: 2008-02-01
Hi,
I am trying out with thread synchronization.  I wrote this small thread program.  I need to write some information to the file using both threads.  But, it is not working... I know there are other problems in the code, like the x, y assignments..etc... but, I need to get the writing part complted so that I can experiment with the rest of them.

Instead writing to the file, if I write to the console that is also not working.  It only displays the printfs in the main().  Why it is?
Thanks for your help.

#include <stdio.h>
#include <windows.h>

int table[200];

static FILE *myFile;

DWORD WINAPI FirstThread( LPVOID lpvThreadParam ) {
   int i;
      
   for ( i = 0; i < 200; i++ ) {
      table[i] = i;
      fprintf(myFile, " %d\n", table[i]);
   }

   (int)lpvThreadParam = 5;
   return 0;
}

DWORD WINAPI SecondThread( LPVOID lpvThreadParam ) {
   int j;

   for ( j=0; j < 200; j++ ) {
      fprintf(myFile, " %d", table[j]);
      if ((j % 10 ) == 0 )
         fprintf(myFile,"\n");
            
   }
      
   return 0;
}


main()
{
      
   int x = 0, y =0;

   HANDLE hThread;
   WORD dwThreadId1, dwThreadId2, dwResult = 0;
      
   myFile = fopen("abc.txt", "w");
   fprintf(myFile, "Begining\n");
   hThread = CreateThread(NULL, 0, FirstThread, (LPVOID)    
                                        &x,0,&dwThreadId1);
   hThread = CreateThread(NULL, 0, SecondThread, (LPVOID)
                                        &y,0,&dwThreadId2);
      
    printf("%d\n", dwThreadId1 );
    printf("%d\n", dwThreadId2);
      
   return (dwResult);

}
0
Comment
Question by:thanesh
2 Comments
 
LVL 1

Expert Comment

by:rghome
ID: 12257254
You should open the file in append mode ("a") if you are writing to a file, otherwise the threads may try and overwrite the same part of the buffer. Even so, your stdio library must be thread-safe and bug free for this to work.

If you still have problems, try setting line buffering on the file, or turning buffering off completely. Use the setvbuf call. Alternatively, try calling flush() after each printf.

If it still doesn't work. Try using write() directly and see if that works. You will have to open the file using open(), use the append mode flag O_APPEND.
0
 
LVL 6

Accepted Solution

by:
vadim_ti earned 75 total points
ID: 12257277
#include <stdio.h>
#include <windows.h>

int table[200];

static FILE *myFile;
static LPCRITICAL_SECTION  cs;

void add_to_file(char *src)
{
      EnterCriticalSection(cs);
      fprintf(myFile, src);
      LeaveCriticalSection(cs);
}

DWORD WINAPI FirstThread( LPVOID lpvThreadParam ) {
   int i;
char t[20];
     
   for ( i = 0; i < 200; i++ ) {
      table[i] = i;
      sprintf(t, " %d\n", table[i]);
      add_to_file(t);
   }

   (int)lpvThreadParam = 5;
   return 0;
}

DWORD WINAPI SecondThread( LPVOID lpvThreadParam ) {
   int j;
char t[20];
 
   for ( j=0; j < 200; j++ ) {
      sprintf(t, " %d", table[i]);
      add_to_file(t);
      if ((j % 10 ) == 0 ) {
         sprintf(t,"\n");
         add_to_file(t);
     }
         
   }
     
   return 0;
}


main()
{
     
   int x = 0, y =0;

   HANDLE hThread1, hThread2;
   WORD dwThreadId1, dwThreadId2, dwResult = 0;

  InitializeCriticalSection(cs);    
   myFile = fopen("abc.txt", "w");
  add_to_file("Begining\n");
   hThread1 = CreateThread(NULL, 0, FirstThread, (LPVOID)    
                                        &x,0,&dwThreadId1);
   hThread2 = CreateThread(NULL, 0, SecondThread, (LPVOID)
                                        &y,0,&dwThreadId2);
    WaitForSingleObject(hThread1);
    WaitForSingleObject(hThread2);
    printf("%d\n", dwThreadId1 );
    printf("%d\n", dwThreadId2);
     
  DeleteCriticalSection(cs);    
   return (dwResult);

}
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now