File I/O and Multiple Threads

H

headware

What happens to OpenTextFile if one thread tries to write to the file
while another thread currently has it open? I tried testing it by
calling OpenTextFile multiple times without ever calling Close, but it
still seems to work fine. Is there any way in VBScript that you can
guarantee that the file stream is closed (something analogous to a
finally {} block in C#)?

Dave
 
A

Anthony Jones

headware said:
What happens to OpenTextFile if one thread tries to write to the file
while another thread currently has it open? I tried testing it by
calling OpenTextFile multiple times without ever calling Close, but it
still seems to work fine. Is there any way in VBScript that you can
guarantee that the file stream is closed (something analogous to a
finally {} block in C#)?


Objects in VBScript use deterministic finalisation. IOW when a variable
containing an object reference is re-assigned or passes out of scope
(because execution has exited the function it was declared in) that object
reference is released synchoronously. When the last reference to a
TextStream object is released it will close its file.
 
H

headware

Objects in VBScript use deterministic finalisation. IOW when a variable
containing an object reference is re-assigned or passes out of scope
(because execution has exited the function it was declared in) that object
reference is released synchoronously. When the last reference to a
TextStream object is released it will close its file.

Thanks for the reply Anthony. So VBScript allows multiple threads to
hold write streams to the same file (which is not something you can do
in .NET), but the WriteLine method must synchronize access to the file
right? Otherwise you would get multiple threads writing to the file at
the same time and the output would be mangled. I'm having difficulty
finding documentation on the thread-safety of the file I/O code in
VBScript.
 
A

Anthony Jones

headware said:
Thanks for the reply Anthony. So VBScript allows multiple threads to
hold write streams to the same file (which is not something you can do
in .NET), but the WriteLine method must synchronize access to the file
right? Otherwise you would get multiple threads writing to the file at
the same time and the output would be mangled. I'm having difficulty
finding documentation on the thread-safety of the file I/O code in
VBScript.

This isn't really a threadsafety issue. You could have two processes
sharing file, even two processes running on different machines sharing the
file.

If they all have write access to the file and attempt to write to the file
it is quite likely to get mangled.
 
H

headware

This isn't really a threadsafety issue. You could have two processes
sharing file, even two processes running on different machines sharing the
file.

If they all have write access to the file and attempt to write to the file
it is quite likely to get mangled.

Well, it's a thread safety issue in the regard that you could have
more than one thread accessing shared data (the file) simultaneously.

The background here is that I have an ASP app that is using a log file
for errors. I'm worried that if more than one request (it's my
understanding that each request in IIS is run under its own thread, at
least for IIS 5) were to write to the log at the same time, it would
cause problems. I was first concerned that if one thread tried to
write to the log file while another had it open that it would error
out since this is what happens in .NET. It's clear now that this does
not actually happen, but it sounds like there still is the possibility
of the logging output being interleaved between more than one thread
making the log unreadable.

I haven't seen any way to perform thread synchronization in VBScript,
so I'm not sure if this is a problem that can be easily fixed or if
I'm even thinking about it in the right way.

Dave
 
A

Anthony Jones

headware said:
Well, it's a thread safety issue in the regard that you could have
more than one thread accessing shared data (the file) simultaneously.

Typically we use the term 'Thread safety' to refer to issues related to code
in a process modifying memory locations simultaneously (since all threads in
a process share the same Virtual memory space) resulting in unexpected
behaviour.
The background here is that I have an ASP app that is using a log file
for errors. I'm worried that if more than one request (it's my
understanding that each request in IIS is run under its own thread, at
least for IIS 5) were to write to the log at the same time, it would
cause problems.

Yes multiple request can be processed simultaneously in different threads.
I was first concerned that if one thread tried to
write to the log file while another had it open that it would error
out since this is what happens in .NET. It's clear now that this does
not actually happen, but it sounds like there still is the possibility
of the logging output being interleaved between more than one thread
making the log unreadable.

I'm not sure how you tested this but a call to OpenTextFile will throw a
permission denied if you attempt to open for write or append a file already
open elsewhere. In you C# version were you discarding and/or closing the
log file? In C# it would be much easier to have a singleton log file object
that serialised calls write a log.
I haven't seen any way to perform thread synchronization in VBScript,
so I'm not sure if this is a problem that can be easily fixed or if
I'm even thinking about it in the right way.

There is no builtin way to synchronize ASP threads.
 
H

headware

Typically we use the term 'Thread safety' to refer to issues related to code
in a process modifying memory locations simultaneously (since all threads in
a process share the same Virtual memory space) resulting in unexpected
behaviour.


Yes multiple request can be processed simultaneously in different threads.


I'm not sure how you tested this but a call to OpenTextFile will throw a
permission denied if you attempt to open for write or append a file already
open elsewhere. In you C# version were you discarding and/or closing the
log file? In C# it would be much easier to have a singleton log file object
that serialised calls write a log.




There is no builtin way to synchronize ASP threads.

All I did to test it was to open the log file in one thread and not
close it. I realize now though, after reading your first post, that
such as test won't work since the log file is closed by the system
when the reference goes out of scope. I suppose I would have to force
the thread to wait while the reference is still in scope and the file
is still open while the other thread comes in and tries to open it in
order to recreate the permissions error you mentioned.

I only mentioned the C# code as a reference for something that worked
in a way I understood. I was able to write C# code that gave me an
error when multiple threads tired to write to the file; but couldn't
do that in VBScript (now I know why). The app I'm working on is a
classic ASP web application, by the way.

The way I would solve this problem is C#, like you suggested, would be
to have a logging class that synchronized thread access to the file.
Is there a way to do something like this in VBScript? How do people
normally deal with logging in classic ASP?

Dave
 
A

Anthony Jones

headware said:
All I did to test it was to open the log file in one thread and not
close it. I realize now though, after reading your first post, that
such as test won't work since the log file is closed by the system
when the reference goes out of scope. I suppose I would have to force
the thread to wait while the reference is still in scope and the file
is still open while the other thread comes in and tries to open it in
order to recreate the permissions error you mentioned.

I only mentioned the C# code as a reference for something that worked
in a way I understood. I was able to write C# code that gave me an
error when multiple threads tired to write to the file; but couldn't
do that in VBScript (now I know why). The app I'm working on is a
classic ASP web application, by the way.

The way I would solve this problem is C#, like you suggested, would be
to have a logging class that synchronized thread access to the file.
Is there a way to do something like this in VBScript? How do people
normally deal with logging in classic ASP?

The best approach from a purely VBScript in ASP point of view is to use a
database as a means of logging. The DB therefore handles the simultaneous
entries in the log.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top