473,806 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is synclock needed in DLL?

Say I have a DLL with a method that does a file write. I call this DLL from
an application and pass the file path and name to the FileWrite() method
inside the DLL. The application is multi threaded so that two or more
threads may be trying to write to the file at once. If one thread is
writing then another comes along and trys to write, it will get a file in
use error.

Should SyncLock (or ReaderWriterLoc k) be used inside the application,
surrounding the FileWrite() call or inside the DLL's FileWrite() method?

Thanks,
Bretrt
Nov 21 '05 #1
7 1369
You can use lock in the DLL and then claim that the DLLs FileWrite method is
thread safe :-)

HTH
rawCoder

"Brett" <no@spam.net> wrote in message
news:uU******** ******@tk2msftn gp13.phx.gbl...
Say I have a DLL with a method that does a file write. I call this DLL from an application and pass the file path and name to the FileWrite() method
inside the DLL. The application is multi threaded so that two or more
threads may be trying to write to the file at once. If one thread is
writing then another comes along and trys to write, it will get a file in
use error.

Should SyncLock (or ReaderWriterLoc k) be used inside the application,
surrounding the FileWrite() call or inside the DLL's FileWrite() method?

Thanks,
Bretrt

Nov 21 '05 #2
So the DLL's lock will affect threading in the calling application? If so,
that's good because now I don't have to retrofit the app with Locks to every
one of the DLL FileWrite() methods.

What do you mean by "claim"? Are there scenarios where it won't be thread
safe?

Thanks,
Brett
"rawCoder" <ra******@hotma il.com> wrote in message
news:ut******** *****@TK2MSFTNG P10.phx.gbl...
You can use lock in the DLL and then claim that the DLLs FileWrite method
is
thread safe :-)

HTH
rawCoder

"Brett" <no@spam.net> wrote in message
news:uU******** ******@tk2msftn gp13.phx.gbl...
Say I have a DLL with a method that does a file write. I call this DLL

from
an application and pass the file path and name to the FileWrite() method
inside the DLL. The application is multi threaded so that two or more
threads may be trying to write to the file at once. If one thread is
writing then another comes along and trys to write, it will get a file in
use error.

Should SyncLock (or ReaderWriterLoc k) be used inside the application,
surrounding the FileWrite() call or inside the DLL's FileWrite() method?

Thanks,
Bretrt


Nov 21 '05 #3
I meant that currently ur method is not thread safe for the caller.
Ever seen this 'CLAIM' on MSDN :-)

----
for e.g. at
http://msdn.microsoft.com/library/de...classtopic.asp
Thread Safety
Public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Instance members are not guaranteed to be
thread-safe.
----

rawCoder

"Brett" <no@spam.net> wrote in message
news:u%******** ********@TK2MSF TNGP12.phx.gbl. ..
So the DLL's lock will affect threading in the calling application? If so, that's good because now I don't have to retrofit the app with Locks to every one of the DLL FileWrite() methods.

What do you mean by "claim"? Are there scenarios where it won't be thread
safe?

Thanks,
Brett
"rawCoder" <ra******@hotma il.com> wrote in message
news:ut******** *****@TK2MSFTNG P10.phx.gbl...
You can use lock in the DLL and then claim that the DLLs FileWrite method is
thread safe :-)

HTH
rawCoder

"Brett" <no@spam.net> wrote in message
news:uU******** ******@tk2msftn gp13.phx.gbl...
Say I have a DLL with a method that does a file write. I call this DLL

from
an application and pass the file path and name to the FileWrite() method inside the DLL. The application is multi threaded so that two or more
threads may be trying to write to the file at once. If one thread is
writing then another comes along and trys to write, it will get a file in use error.

Should SyncLock (or ReaderWriterLoc k) be used inside the application,
surrounding the FileWrite() call or inside the DLL's FileWrite() method?
Thanks,
Bretrt



Nov 21 '05 #4
Hi,

Should SyncLock (or ReaderWriterLoc k) be used inside the application,
surrounding the FileWrite() call or inside the DLL's FileWrite() method?
<<

Yes (or Monitor) -- as long as you use the same Object to SyncLock for all
threads. And, you have to make sure that you don't dead-lock. That is,
you have to make sure that no tread can call a lock and not release it -- or
that one thread's execution does not depend on that of another thread, where
both are sharing this common write method.

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
www.mabry.com/vbpgser4 to order.
Nov 21 '05 #5

No, it's one set of code, just the synchronized version wraps the
non-synchronized one.

For example, the .NET framework provides two classes ArrayList and
SyncArrayList. The difference is that ArrayList is non thread safe
byt SyncArrayList is.

ArrayList is the class that has all of the functionality for managing
a dynamic array.

SyncArrayList simply wraps an ArrayList and has all the same
properties and methods and wraps all of the calls in a synclock.

So there is more code to maintain, but it's not duplicate code.

As far as performance, it's all relative and whether you need to worry
about it depends on the operation and expected number of calls.

HTH,

Sam
On Thu, 7 Apr 2005 16:08:44 -0400, "Brett" <no@spam.com> wrote:
I think the performance hit would be very smalll in most every case. The
first method you described requires keeping track of two sets of
code...correct ?

Thanks,
Brett

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #6
I think I see now. Calling SyncArrayList is an indirect call to ArrayList.
So in my DLL, I have some type of synclock wrapper sub that calls the non
thread safe subs and puts them between the syncLocks, depending on what the
user wants...right?

Thanks,
Brett
"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:5p******** *************** *********@4ax.c om...

No, it's one set of code, just the synchronized version wraps the
non-synchronized one.

For example, the .NET framework provides two classes ArrayList and
SyncArrayList. The difference is that ArrayList is non thread safe
byt SyncArrayList is.

ArrayList is the class that has all of the functionality for managing
a dynamic array.

SyncArrayList simply wraps an ArrayList and has all the same
properties and methods and wraps all of the calls in a synclock.

So there is more code to maintain, but it's not duplicate code.

As far as performance, it's all relative and whether you need to worry
about it depends on the operation and expected number of calls.

HTH,

Sam
On Thu, 7 Apr 2005 16:08:44 -0400, "Brett" <no@spam.com> wrote:
I think the performance hit would be very smalll in most every case. The
first method you described requires keeping track of two sets of
code...correc t?

Thanks,
Brett

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.

Nov 21 '05 #7

Yup, that's the pattern.

Sam
On Thu, 7 Apr 2005 17:46:38 -0400, "Brett" <no@spam.com> wrote:
I think I see now. Calling SyncArrayList is an indirect call to ArrayList.
So in my DLL, I have some type of synclock wrapper sub that calls the non
thread safe subs and puts them between the syncLocks, depending on what the
user wants...right?

Thanks,
Brett
"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:5p******* *************** **********@4ax. com...

No, it's one set of code, just the synchronized version wraps the
non-synchronized one.

For example, the .NET framework provides two classes ArrayList and
SyncArrayList. The difference is that ArrayList is non thread safe
byt SyncArrayList is.

ArrayList is the class that has all of the functionality for managing
a dynamic array.

SyncArrayList simply wraps an ArrayList and has all the same
properties and methods and wraps all of the calls in a synclock.

So there is more code to maintain, but it's not duplicate code.

As far as performance, it's all relative and whether you need to worry
about it depends on the operation and expected number of calls.

HTH,

Sam


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2551
by: Henri | last post by:
1) Do Shared methods always need to be synchronised to be thread-safe? 2) Why must SyncLock be used with a reference type? I can't get the relation between threads and a type. Thanks for your help. Henri
12
3057
by: Keith Langer | last post by:
I have some questions about whether synclock is necessary in a few different scenarios: 1) I have a Queue class which is shared between two threads. Thread 1 pushes objects onto the queue and Thread 2 pops objects off the queue one at a time (with no enumerators - just first in, first out). Does the Queue class need to be synclocked when objects are pushed on or popped off? 2) I have a boolean variable which can be read or written...
4
3791
by: fred | last post by:
If I have multiple threads running a Sub as below then a number of threads can be held up at the SyncLock. When it becomes free which thread goes first. Is it just by chance which thread goes first or do the threads queue in a FIFO or perhaps a LIFO bases. Thanks Fred Sub SomeWork()
10
3164
by: Bob Day | last post by:
Using vs 2003, vb.net sql msde.. Consider the following code snippets. See **** for questions. All are shared and accessed by multiple threads simultaneiously. ' Instantiate per for this shared class Private Shared gcCDsd As New Class_Component_Designer
3
2133
by: Bob Day | last post by:
Ok, I have done a lot of reading(of the newsgroup answers, help files and MSDN articles) of synclock. I understand what you are saying in the newsgroup, and it is very helpful. It does, however, directly contradict what the VS 2003 help file says, and it seems that various posters to my original questions on Synclock disagree with each other. It seems, that the best practice is to use sysnlock to protect code, not variables. That...
7
1627
by: SD | last post by:
I have a public object that I only want one thread to access at a time. However the access to this object is not limited to one procedure. Will SyncLock work in this case? If not what options do I have? Thanks SD Code in Module public A as Collection
1
565
by: fred | last post by:
I have a VB application that is using MS Access as its database. To avoid connection delays the application creates one connection to the database at start-up and maintains that single connection throughout until the application closes. I now want to use multithreading to improve the feel of the application but I need some help understanding Synclock. The aim here is to prevent any two threads from trying to use the same connection at the...
2
10058
by: HONOREDANCESTOR | last post by:
I have a buffer that needs to be locked sometimes, because 2 processes update it. So I made the buffer into a class and whenever there is code that affects it, I sandwich the code between Synclock MyBufferClass ........... End Synclock My question is, what is Synlock protecting? Is it just protecting
10
4109
by: Frank Osterberg | last post by:
Hi, I want to simultaneously SyncLock multiple objects, something like: SyncLock objA, objB ' do stuff with objA and objB
2
2943
by: eBob.com | last post by:
I was changing some code in a multi-threaded application today and noticed that it was not locking where it really needed to be locking. The Sub was already working with an array so I just stuck a SyncLock ArrayName.SyncRoot at the beginning of the Sub and an End SyncLock at the end. But this caused the application to produce no output (an Excel spreadsheet)! After some screwing around, sorry ... I mean experimenting, I noticed that the...
0
10364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.