• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Multi threaded app outputing to same text file?

Hello there!

I have a multi-threaded application and I would like each thread to be able to output information to the same text file.  I'm unsure how to do this in a thread safe manner.

I have considered creating a routine that would check to see if the file is locked.
If it is locked,it would loop until it is not, then write data, unlock, and close the file.

I would thread this routine each time it is called.

Is this the most efficient way to do this?   Would it be thread safe?   I'd appriciate any help from some people more familiar with multi threaded apps than I am!  


0
adamwj
Asked:
adamwj
  • 2
  • 2
2 Solutions
 
Jason EvansSenior Software DeveloperCommented:
Hi there.

There are a couple of ways to do this, one option is to use the SyncLock...End Synclock keywords.

For example,

Public Sub WriteToFile()
    Dim fs As FileStream = File.OpenWrite("C:\TestFile.txt")

    SyncLock fs
' Do file stuff.

    End SyncLock

  End Sub

The above is just a skeleton example, the important part is the SyncLock section. What that does is block multiple threads from executing the code inside that block, meaning only one thread will run that code at at a time. Try this technique to see if it will help.

Cheers.
Jas.
 
0
 
adamwjAuthor Commented:
Thanks Jas.  I did just check up on the synclock statement and it does appear that it will help.

Just to make sure I'm understanding it correctly,  the fs filestream would need to be declared at the class or module level, right?  From what I read the object that you use in the SyncLock statement must be accessable by all threads.

What happens if two or more threads call the routine at the same time?  Would the routine s unable to lock it just exit without writing any data?

I don't mean to be difficult I just want to make sure I understand exactly how this works before I implement it.  

Thanks! :-)
0
 
Jason EvansSenior Software DeveloperCommented:
Hi there.

Ye, good questions there.

I think, but not sure, you could get away with having the file code in a shared sub as part of a class and then use that class as the SyncLock object:

Public Class Test

  Public Shared Sub WriteFile()
    SyncLock Me


    End SyncLock
  End Sub
End Class

The SynLock Me line actually locks the class instance - I haven't checked this 100%, but it's an option.

The other would be to create the fs variable at the class or module level so that it's accessible for all threads.

Here are a few good articles about this you could have a look at:

http://www.codeproject.com/vb/net/threadingvbnet.asp
http://www.developerfusion.co.uk/show/5184/2/
http://www.java2s.com/Tutorial/VB/0420__Thread/SyncLock.htm

Hope these help!

Cheers.
Jas
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"What happens if two or more threads call the routine at the same time?  Would the routine s unable to lock it just exit without writing any data?"

One of the routines will get it first and execute to completion.  The other will wait, suspending its execution until the first is complete and the lock is released.

For the docs on SyncLock:

"Mechanism. When a thread reaches the SyncLock statement, it evaluates the lockobject expression and suspends execution until it acquires an exclusive lock on the object returned by the expression. When another thread reaches the SyncLock statement, it does not acquire a lock until the first thread executes the End SyncLock statement."
0
 
adamwjAuthor Commented:
Thank you both for the help/documentation this is exactly what I needed!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now