Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multithreading

Posted on 2004-08-11
7
Medium Priority
?
315 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
[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
7 Comments
 
LVL 11

Accepted Solution

by:
Jase-Coder earned 100 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 100 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 100 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 100 total points
ID: 11798766
Another solution is synchronizing the access to the log file.

       
0
 
LVL 3

Assisted Solution

by:bigjim2000
bigjim2000 earned 100 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Make the most of your online learning experience.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

618 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