Solved

Multithreading

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article will show, step by step, how to integrate R code into a R Sweave document
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 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…

758 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

16 Experts available now in Live!

Get 1:1 Help Now