Solved

Multithreading

Posted on 2004-08-11
7
308 Views
Last Modified: 2010-04-17
Hi all,

I'm new to multithreaded applications and would like to write a program that has threads that write to a single log file. I wondering, if thread A does an fopen and is in the middle of writing to the file, would thread B have an error if it tries to do an fopen to also write to the file? Or would it wait for thread A to finish?

Would appreciate any pointers on multithreading that you could provide.

Many thanks in advance!

Cheers,
Ben
0
Comment
Question by:bcheong
7 Comments
 
LVL 11

Accepted Solution

by:
Jase-Coder earned 25 total points
ID: 11772937
hi if you open the file with a then with b then both threads will write to the file causing unwanted info.
You could lock a file while it is open.

http://www.flipcode.com/tutorials/tut_mthread.shtml
http://www.hpctec.mcc.ac.uk/hpctec/courses/MT/MTcourse.html

here are a couple sites that introduce threads
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 25 total points
ID: 11772984
>if you open the file with a then with b then both threads will write to the file causing unwanted info.
That will occur if you are writting information at the same position, or appending at the same time. But if you write different register, then there will be not problems with the proper file access rights.
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 25 total points
ID: 11774768
The answer is to have a completely separate 'Log' thread that you send messages to. It then writes the messages to the log file. You then get ONE owner of the resource, a much safer environment.

Paul
0
 
LVL 1

Assisted Solution

by:sunny1907
sunny1907 earned 25 total points
ID: 11798766
Another solution is synchronizing the access to the log file.

       
0
 
LVL 3

Assisted Solution

by:bigjim2000
bigjim2000 earned 25 total points
ID: 11808065
The key with this type of synchronization problem is the "lock" keyword.  Here is a sample to demonstrate it's use (C#):


private System.IO.StreamWriter mLogger;
private const string LOG_FILE = @"c:\log.txt";
private bool bLog = true;

private void btnStart_Click(object sender, System.EventArgs e)
{
      this.mLogger = new System.IO.StreamWriter(LOG_FILE, true);
      System.Threading.Thread t;
      t = new System.Threading.Thread(new System.Threading.ThreadStart(LogThread1));
      t.Start();
      t = new System.Threading.Thread(new System.Threading.ThreadStart(LogThread2));
      t.Start();
      System.Threading.Thread.Sleep(10000);
      this.bLog = false;
      System.Threading.Thread.Sleep(1000);
      this.mLogger.Close();
}

private void LogThread1()
{
      while( this.bLog )
      {
            lock( this.mLogger )
            {
                  this.mLogger.WriteLine("I'm in thread 1!");
            }
            System.Threading.Thread.Sleep(500);
      }
}

private void LogThread2()
{
      while( this.bLog )
      {
            lock( this.mLogger )
            {
                  this.mLogger.WriteLine("I'm in thread 2!");
            }
            System.Threading.Thread.Sleep(500);
      }
}



The lock statement "locks" a resource used by multiple threads.  Try the same code without the lock brackets and you will nodoubt get more than a few errors ;-)
If you have any more questions, let me know :-)

Hope this helped!

-Eric
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

810 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