Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Writing to a file in multiple threads

Posted on 2004-10-07
2
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

647 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