Solved

Multithreading

Posted on 2004-08-11
7
307 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now