?
Solved

Writing to a file in multiple threads

Posted on 2004-10-07
2
Medium Priority
?
372 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 300 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

840 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