Solved

Writing to a file in multiple threads

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

732 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