473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
backgroundworker 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 1192
=?Utf-8?B?bG91aXM=?= <lo***@discussions.microsoft.comwrote in
news:D1**********************************@microsof t.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 backgroundworker 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***@discussions.microsoft.comwrote in
news:D1**********************************@microsof t.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 backgroundworker 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***@discussions.microsoft.comwrote in
news:04**********************************@microsof t.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
backgroundworker 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
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
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...
6
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...
1
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...
18
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...
3
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...
13
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...
35
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.