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
Solved

Multithreading

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …

790 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