Click here to Skip to main content
16,006,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have developed a datalogging application that takes data from several sources and keeps them in a data set. This is done in a thread. Other threads access this data but none change it. Its just the current values from each source.

Another thread takes that data and logs it into another dataset. This adds a row to the data set each second with the time and a column for each value. This data is also accessed by other threads but never changed.

I have several forms that access the data in both datasets to view it in real time. They never change the data in either data set.

Do I need to use read/write locks if only one thread is changing the data? I haven't seen a problem "yet" not using locks but I am seeing a delay on the UI every second when it waits for access to the datasets. Thanks.
Posted

1 solution

Apparently, if only one thread accesses some data, no lock is needed.

Reader/writer lock is very effective if the data is often read by more then one thread at a time, but more rarely written; then the lock already taken for read is upgraded to write; it allows several threads to get read access when no thread is writing. When it is not the case, simple lock is the best.

—SA
 
Share this answer
 
Comments
Klockness 24-Jun-11 17:49pm    
I guess I understand how it works but what is the effect of not using it? Will it cause an error if I'm reading a dataset in one thread while writting in another? Or is it just best practice to ensure correct data?
Sergey Alexandrovich Kryukov 24-Jun-11 20:37pm    
The effect of not using the locks where you need it is really bad. It could be data corruption and/or crash. But there is much worse effect: it can work, say, 20 years without a problem as the probability of the clash could be low, and on 21-th year make a spectacular crash. There are also deadlocks. To me, the worse thing is the invisible problem. Imagine that nothing crashes but produces slightly mangled data due to race condition. Reliability of threading program should be figured out theoretically as testing does not proof anything. Fortunately, it is possible to make a crystal clear design where the rules are simple.
It you have concrete question I could probably answer.
--SA
Sergey Alexandrovich Kryukov 24-Jun-11 20:41pm    
What do you mean "best practice"? The problem is about life or death. You can do amazingly much without locks. The lock is only needed when more than one thread access same portion of data; and there is integrity constraint. Imagine two threads read and write X and Y. If you know that X and Y are independent at any given period of time, you can make one lock around X, another lock around Y, that will improve throughput. Now imagine there is a constraint like X+Y=1, then you need to put all modifications of any of them in one block in both threads and guard with the same lock object. Pretty simple.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900