473,786 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Classes with threads

Can someone please discuss "Synclock" or other methods to use for making a
class 'thread safe". I need to have about 4 processes running (probably on
backgroundworke r threads) and all will need to call the same class for
reading data (not SQL Server). If each worker thread creates an "instance"
of the class, won't that be good enough?
TIA
Jul 15 '07 #1
4 1205
=?Utf-8?B?bG91aXM=?= <lo***@discussi ons.microsoft.c omwrote in
news:D1******** *************** ***********@mic rosoft.com:
Can someone please discuss "Synclock" or other methods to use for
making a class 'thread safe". I need to have about 4 processes
running (probably on backgroundworke r threads) and all will need to
call the same class for reading data (not SQL Server). If each worker
thread creates an "instance" of the class, won't that be good enough?
Yes, if each thread has their own instance of data then you do not need to
synchronize access to the object. Synclock is used when you need to
synchronize cross thread access. In essence Synclock single threads access
to a particular object to prevent multiple threads from accessing and
changing and object at a particular time.

Do these processes have any functions that can be called from another
thread? If they do - then you'll need to synclock :-)
Jul 15 '07 #2
the class has several public vars that are set/read. It has also has
initialize and terminate events (currently in VB6). It has 1 main public
function that could be called by any of the 4 processes at any time. An
article I found on MSDN leads me to believe I should use Synclock to prevent
2 or more process from using (changing vars) in the class at the same time.

"Spam Catcher" wrote:
=?Utf-8?B?bG91aXM=?= <lo***@discussi ons.microsoft.c omwrote in
news:D1******** *************** ***********@mic rosoft.com:
Can someone please discuss "Synclock" or other methods to use for
making a class 'thread safe". I need to have about 4 processes
running (probably on backgroundworke r threads) and all will need to
call the same class for reading data (not SQL Server). If each worker
thread creates an "instance" of the class, won't that be good enough?

Yes, if each thread has their own instance of data then you do not need to
synchronize access to the object. Synclock is used when you need to
synchronize cross thread access. In essence Synclock single threads access
to a particular object to prevent multiple threads from accessing and
changing and object at a particular time.

Do these processes have any functions that can be called from another
thread? If they do - then you'll need to synclock :-)
Jul 15 '07 #3
=?Utf-8?B?bG91aXM=?= <lo***@discussi ons.microsoft.c omwrote in
news:04******** *************** ***********@mic rosoft.com:
the class has several public vars that are set/read. It has also has
initialize and terminate events (currently in VB6). It has 1 main
public function that could be called by any of the 4 processes at any
time. An article I found on MSDN leads me to believe I should use
Synclock to prevent 2 or more process from using (changing vars) in
the class at the same time.
Yes. However, synclocking is not that cut and dry ... you'll need to get
more familiar with threading and then apply synclocking in places where it
makes sense.
Jul 15 '07 #4
louis wrote:
Can someone please discuss "Synclock" or other methods to use for making a
class 'thread safe". I need to have about 4 processes running (probably on
backgroundworke r threads) and all will need to call the same class for
reading data (not SQL Server).
Synclock is a keyword that provides simple access to the Monitor class.
Monitor allows you to ensure that only one Thread is running a given
piev of code at any given time.
If each worker thread creates an "instance" of the class,
won't that be good enough?
Strictly speaking, no.
You ought to have synchronisation logic on "sensitive" methods so that
only one Thread can be executing it at any time. Without this, you can
get updates interlacing with one another, causing a hell of mess.

However, since you are "wrapping up" a /database/ it may not be so much
of an issue; you could probably manage the synchronisation just as
easily at the database level.
HTH,
Phill W.
Jul 17 '07 #5

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

Similar topics

9
1427
by: Klaus Neuner | last post by:
Hello, I wrote a program that does essentially the following: for rule in rules: for line in line_list: line = my_apply(rule, line) line_list contains the lines of some input text.
11
4203
by: Przemysław Różycki | last post by:
Hello, I have written some code, which creates many threads for each connection ('main connection'). The purpose of this code is to balance the load between several connections ('pipes'). The number of spawned threads depends on how many pipes I create (= 2*n+2, where n is the number of pipes). For good results I'll presumably share main connection's load between 10 pipes - therefore 22 threads will be spawned. Now if about 50
6
1861
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which contains methods for serialising and de-serialising objects to the database storage. I put this as a shared class as multiple web clients will be using the class to store and retreive data, the problem I'm haivng now is that I think multiple threads...
1
1797
by: ravinder | last post by:
I wanted to develop a multithreaded program using OO concepts on windows platform. Problem: I have to simulate two layers(similar to TCP/IP stack layers), and the layer functionality is of finite state machine type i.e after receiving any event(either after receiving message or any timer expiry), state change happens internal to layer. And there will be message transfer between two layers. My idea is to put layer specific data in the class and...
18
3325
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done with the StaticClass.Method1 before it can use it? What if I removed static methods and made all the threads instantiate its own copy of the class? Would that remove the waiting contention?
3
2973
by: Bob Day | last post by:
VS 2003, vb.net, sql msde... The help is pretty empatic that you cannot pass parameters to a thread. The sample below is from help, showing you to set up variables in the TasksClass, and assign their value from sub DoWork where you start the thread. This approach works, of course. Why can't you simply add a Sub New to the TaskClass below, and then in sub DoWork modify the threading line with the sub new arguments needed like this: ...
13
1320
by: cj | last post by:
Stephany Young provided me with the following code to count threads created by my program. Public Class MyThreadCount Private Shared m_lock As New Object Private Shared m_threadcount As Int32 = 0 Public Shared Sub Increment() SyncLock (m_lock)
35
9365
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from 500000 to 3200000 of a file whose size is say 20MB... how do i request a download which starts directly at 500000th byte... thank u cheers
3
2985
by: Steven Blair | last post by:
I have been watching an MSDN video on the PFX Task class and have a question. Here is my scenario: TcpListener waits on incoming connections. Once a new connection is established, a new Task will be created. So, for my example, 8 connections have been established almost all at the same time (8 Tasks object). If my server is a dual core (for arguments sakes, 4 Tasks per CPU) does
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
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
7510
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
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.