Click here to Skip to main content
15,909,939 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: i Pin
toxcct22-Apr-05 4:32
toxcct22-Apr-05 4:32 
Generalcollections doubt Pin
carlos_rocha22-Apr-05 3:56
carlos_rocha22-Apr-05 3:56 
GeneralRe: collections doubt Pin
Dave Kreskowiak22-Apr-05 5:12
mveDave Kreskowiak22-Apr-05 5:12 
GeneralRe: collections doubt Pin
carlos_rocha22-Apr-05 7:42
carlos_rocha22-Apr-05 7:42 
GeneralRe: collections doubt Pin
Dave Kreskowiak22-Apr-05 13:17
mveDave Kreskowiak22-Apr-05 13:17 
GeneralRe: collections doubt Pin
rwestgraham22-Apr-05 14:11
rwestgraham22-Apr-05 14:11 
GeneralRe: collections doubt Pin
carlos_rocha25-Apr-05 22:31
carlos_rocha25-Apr-05 22:31 
GeneralRe: collections doubt Pin
rwestgraham26-Apr-05 13:09
rwestgraham26-Apr-05 13:09 
Mutexes and Semaphores will still work. Keep in mind that mutexes and semaphores do not "lock" anything. They are just a means of inter-thread communications.

A mutex says "I am in the process of modifying some object. No other threads should attempt to access this object until I am done". A semaphore says "I am in the process of accessing some object. No other threads should attempt to modify this object until I am done."

The developer still has to write all the code that ensures each thread obeys the rules when accessing an object.

carlos_rocha wrote:
When i complete a read i have directly the object which i keep in the collection(along with many others).The thing is that at this point i need to lock just the object, not the entire collection.But when i'm enumerating the collection i lock it because i want to access all objects.So, i'm divided in how i should do that, because if i lock the collection but that in the read function have access to the object i'll be able to modify it!!!That's not right.

You have to set a reference to the object somewhere. It does not matter if you are enumerating the collection or setting a reference directly to an object that exists in the collection. Before you access a member of the collection by any method of access - direct or by enumeration (both of which require you to set a reference to the object) the thread should acquire a semaphore to notify all other threads that it is accessing an object and they cannot modify any objects or the collection itrself. Before you attempt to modify either an object or the collection itself, a thread must acquire the mutex and wait for all semphores to become signalled. This means that no other thread is actively accessing any object in the collection.

In this scenario, you are not sychronizing the objects themselves, you are synchronizing read and write operations on both the collection itself and all of it's members. This will work as long as you are not frequently modifying data either at the collection level or at the member object level.

If you are frequently modifying member objects then you may find that synchronizing on read and write operations causes excessive wait times. In this case you have to implmement synchronization at two levels - collection add/deletes as a top level and per-object synchronization at the member level. This is a lot more complicated.

Before I would implement this degree of synchronization I would carefully examine my application design. Multiple threads frequently accessing common data for anything other than read only (inherently threadsafe) operations is not a scenario that is generally efficient. Threads are mostly useful only when there are minimal concurrency issues. If your application requires more than minimal synchronization, multi-threading will tend to make it slower than just using a single thread.

Or you need to review your overall design.

For example, suppose your application needs to read a lot of data, populate objects from the raw data, and put them in a collection (or it needs to continuously read data in real-time). The application then modfifies some of the objects. The logical way to multithread this would be to have a background thread read data asynchronously, and populate objects and save them in a local collection. Periodically the background thread should stop reading data, and notify the main thread that it has a new set of objects ready. The main thread then adds the new objects to a collection that it owns exclusively, tells the background thread it has taken ownership of the data, and then the background thread drops all references to the existing objects and goes and gets new data.

Threading is generally only useful in the following scenarios:

Background processing of data passed off from a main thread to a worker thread, for example slow dataset processing/updates using data already modified by the main thread, or preparing a print job.

Background processing of data read from an external source to prepare it to be used by the main application, for example slow queries, downloading data from a webserver, or real time data acquisition systems.

Thread pooling applications that require a thread to be assigned to a given operation to ensure all operations remain responsive. For example an IVR system may assign threads on a per channel basis so that each caller gets a response in a timely manner.

In the first two cases, thread synchronization occurs infrequently - only when threads need to transfer data from one thread to another. In the third case there is no concurrency, each thread acts independently.

Although there are always exceptions, in general if significant thread concurrency issues exist, it probably indicates someone is trying to use threads based on the totally misguided notion that multiple threads will somehow "speed up" an application.

carlos_rocha wrote:
I wanted a quick way to make sure the concurrency doesn´t raise any problems.

Sorry, there is no quick way to ensure concurrency issues don't arise. SynchLocks (critical sections) are deceptively easy to code but are by far the most difficult form of synchronization to use correctly. The only place you'll find "quick ways" are in the MSDN examples which are practically useless in real applications. The MSDN examples do not even bother to show the absolute minimum implementation required to use a SynchLock. Any code that enters a critical section absolutely must have a Finally block to ensure that no matter what happens in the protected code the thread always exits the critical section and releases the lock. The MSDN glosses over even this basic fundamental, much less describing any of the numerous subtleties of using SynchLocks.

Robert
GeneralRe: collections doubt Pin
carlos_rocha26-Apr-05 22:19
carlos_rocha26-Apr-05 22:19 
GeneralRe: collections doubt Pin
rwestgraham1-May-05 10:19
rwestgraham1-May-05 10:19 
GeneralCannot copy… It is being used by another person Pin
KreativeKai22-Apr-05 3:50
professionalKreativeKai22-Apr-05 3:50 
GeneralRe: Cannot copy… It is being used by another person Pin
Dave Kreskowiak22-Apr-05 5:02
mveDave Kreskowiak22-Apr-05 5:02 
GeneralRe: Cannot copy… It is being used by another person Pin
KreativeKai22-Apr-05 6:22
professionalKreativeKai22-Apr-05 6:22 
GeneralRe: Cannot copy… It is being used by another person Pin
Dave Kreskowiak22-Apr-05 7:05
mveDave Kreskowiak22-Apr-05 7:05 
GeneralRe: Cannot copy… It is being used by another person Pin
Dave Kreskowiak22-Apr-05 7:14
mveDave Kreskowiak22-Apr-05 7:14 
GeneralRe: Cannot copy… It is being used by another person Pin
rwestgraham25-Apr-05 9:01
rwestgraham25-Apr-05 9:01 
Generalweb services Pin
muktajoshi_1022-Apr-05 1:40
muktajoshi_1022-Apr-05 1:40 
GeneralRe: web services Pin
Dave Kreskowiak22-Apr-05 4:55
mveDave Kreskowiak22-Apr-05 4:55 
GeneralError Handling Pin
dpagka21-Apr-05 23:38
dpagka21-Apr-05 23:38 
GeneralRe: Error Handling Pin
Colin Angus Mackay22-Apr-05 0:01
Colin Angus Mackay22-Apr-05 0:01 
GeneralRe: Error Handling Pin
Dave Kreskowiak22-Apr-05 1:05
mveDave Kreskowiak22-Apr-05 1:05 
GeneralRe: Error Handling Pin
toxcct22-Apr-05 1:35
toxcct22-Apr-05 1:35 
GeneralRe: Error Handling Pin
Dave Kreskowiak22-Apr-05 4:51
mveDave Kreskowiak22-Apr-05 4:51 
GeneralRe: Error Handling Pin
toxcct23-Apr-05 0:44
toxcct23-Apr-05 0:44 
GeneralRe: Error Handling Pin
Dave Kreskowiak23-Apr-05 2:35
mveDave Kreskowiak23-Apr-05 2:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.