Solved

Writing to a file in multiple threads

Posted on 2004-10-07
2
343 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

821 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