Safe Lock Key

0 views
Skip to first unread message

Suanne Forte

unread,
Aug 4, 2024, 4:55:14 PM8/4/24
to poinehedza
Nothis is not thread safe. To make it thread safe you can use lock on static objects because they are shared between threads, this may cause deadlocks in the code but it can be handle by maintaining proper order for locking. There is a performance cost associated with lock so use it wisely.

No, this is not thread-safe. A.MethodeA and OtherClass.CallToMethodInOtherClass are locking on different objects, so they're not mutually exclusive. If you need to protect the access to the list, don't pass it to external code, keep it private.


Thread-safe snippets of code do not a thread-safe application make. You can combine different thread-safe operations into non-thread-safe operations. Indeed, much non-thread-safe code can be broken down into smaller pieces all of which are thread-safe on their own.


Before going further, the two bits I described as thread-safe are not promised to be by the spec for List. Conservative coding would assume they are not thread-safe rather than depending upon implementation details, but I'm going to depend on the implementation details because it affects the question of how to use locks in an important way:


Because there is code operating on list that is not acquiring the lock on locker first, that code is not prevented from running concurrently with CallToMethodInOtherClass. Now, while list.Count is thread-safe and list[93] is tread-safe,* the combination of the two where we depend on the first to ensure that the second works is not thread-safe. Because code outside the lock can affect list, it's possible for code to call Remove or Clear in between Count assuring us that list[93] would work, and list[93] being called.


Now, if we know that list is only ever added to, that's fine, even if a resize is happening concurrently we'll end up with the value of list[93] either way. If something is writing to list[93] and it's a type that .NET will write to atomically (and int is one such type), we'll end up with either the old one or the new one, just as if we'd locked correctly we'd get the old or the new depending on which thread go the lock first. Again, this is an implementation detail not a specified promise, I'm stating this just to point out how the thread-safety given still results in non thread-safe code.


Moving this toward real code. We shouldn't assume that list.Count and list[93] is threadsafe because we weren't promised they would be and that could change, but even if we did have that promise, those two promises won't add up to a promise that they'd be thread-safe together.


This class is guaranteed to be thread-safe in everything it does. Without depending on any implementation details, there is no method here that will corrupt state or give incorrect results because of what another thread is doing with the same instance. The following code still doesn't work though:


Of course, this makes the locks within ThreadSafeList redundant and just a waste of effort, space, and time. This is the main reason that most classes don't provide thread-safety on their instance members - since you can't meaningfully protect groups of calls on members from within the class, it's a waste of time trying to unless the thread-safety promises are very well specified and useful on their own.


The lock in CallToMethodInOtherClass should be removed unless OtherClass has its own reason for locking internally. It can't make a meaningful promise that it won't be combined in a non-threadsafe way and adding more locks to a program just increases the complexity of analysing it to be sure there are no deadlocks.


Then as long as CallToMethodInOtherClass doesn't store myList somewhere it can be seen by other threads later on, it doesn't matter that CallToMethodInOtherClass isn't thread-safe because the only code that can access myList brings its own guarantee not to call it concurrently with other operations on myList.


When something is described as "thread-safe", know just what it's promising by that, as there are different sorts of promise that fall under "thread-safe" and on its own it just means "I won't put this object into a nonsensical state", which while an important building block, is not a lot on its own.


Lock on groups of operations, with the same lock for each group that'll affect the same data, and guard the access to objects so that there can't possibly be another thread not playing ball with this.


*This is a very limited definition of thread-safe. Calling list[93] on a List where T is a type that will be written and read atomically and we don't know whether it actually has at least 94 items is equally safe whether or not there are other threads operating on it. Of course, the fact that it can throw ArgumentOutOfRangeException in either case is not what most people would consider "safe", but the guarantee we have with multiple threads remains the same as with one. It's that we obtain a stronger guarantee by checking Count in a single thread but not in a multi-thread situation that leads me to describe that as not thread-safe; while that combo still won't corrupt state it can lead to an exception we'd assured ourselves couldn't happen.


Unauthorized Key Duplication is the greatest hidden threat to your security. Medeco M3, Keymark, Schlage Primus, and other high security locks allow you to control who can copy your keys so you can better protect your property. Read more >>


We sell, install, and service a huge variety of safes designed to protect your property against fire, burglary or both. Our technicians are also certified to work on U.S. Government Classified Containers. Read more >>


Our key duplication service makes the keys you need, as well as manages who requests those duplications. If you want to control who can get copies of your keys made, talk with us to about how we can help control unauthorized key duplication. Read more >>


Need to know who has used a door? Control when a door can be opened, or by what users? Electronic entry systems provide more control, accountability and transparency than simple keyed systems. We feature systems by Schlage, Medeco, Salto and others. Read more >>


Have you had employee or tenant turnover, loss of keys, unauthorized duplication of your key, or crime in your area? Rekeying your locks can be an effective, economical tool to guard against unauthorized entry. Read more >>


View and record events at your facility full-time in high resolution color. Video can even be viewed remotely via the internet. Video Surveillance Systems are an essential security tool for Loss Prevention and can provide critical information for security and law-enforcement teams. Read more >>


Founded in 1914, Grah Safe & Lock is the San Diego Locksmith your neighbors trust for home and business security, safe locksmith service, lock rekeying, high-security keys, and access control system installation.


Improve your bathroom safety and independence with the Carex Safety Lock Bariatric Raised Toilet Seat. The toilet assist riser features a wider opening and smooth contoured design for added comfort. It adds 4.25" of height to the toilet seat to make sitting and standing more comfortable, is easy to clean, and fits most standard toilets. The toile elevator features our Safe Lock system to lock the seat in place for security and stability. Its heavy-duty design supports up to 500 lbs.


DISCLAIMER: We do not accept returns on any toilet products including commodes and raised toilet seats (opened or unopened). If you are unsure if this will work for you, please contact us so we can provide additional information and answer any questions.


Trust in Carex for all of your bathroom safety needs. Our wide selection, guaranteed product warranty, and 35+ year experience make us the trusted brand for anyone looking to improve the safety of their bathroom. Our mission and reason for being are to enable our customers to live healthier, more comfortable, and independent lives.


Wide opening for bariatric users comfort. Raised toilet seats are perfect for anyone that struggles to sit or stand. They're perfect for improving overall bath safety while adding comfort and independence.


Built for safety and functionality in mind, this toilet riser is engineered with heavy-duty, lightweight and easy to clean molded plastic that supports up to 500 pounds. Provides you with safety and support when using the bathroom.


Our easy-to-use electronic safe locks combine proven functionality with a wide range of options that balance convenience and control. Easy to install or retrofit, The Sargent and Greenleaf Model 6120 electronic safe lock simplifies security upgrades for your safe. Model 6120 is EMP-resistant, and is perfect for commercial safes or home gun safes.


Contact our trained customer service and technical support representatives with questions, concerns, or to get help troubleshooting a potential problem with your Sargent and Greenleaf product, software, or technology integration.


The one-battery keypad is ideal for use with Models 6120, Titan PivotBolt, and Spartan PivotBolt. The battery is kept in an easy-to-access battery compartment at the base of the keypad to allow for a quick and easy battery change.


The two-battery keypad is a premium metal input device offering several high-grade finishes. The keypad snaps onto a non-metallic base featuring an aesthetic and functional reveal. The standard two-battery keypad is unlighted and is often used with Models 6120, 6124, and Titan PivotBolt locks.


Safes and vaults fitted with locks from Safelock Systems Ltd are used in the retail, banking

and leisure industries, as well as by homeowners, and in the public sector.

Safe locks are graded according to EN1300, and are tested fully during lock programming,

a service provided as standard along with unparalleled technical support for trade customers.


We offer commercial locksmithing and security options as well as residential security. Bulger provides the best service almost everything security, door, and lock related: door reinforcement, door protection, high-security locks, keyless entry, access control, camera systems, or just a basic lock you need rekeyed to a new key.

3a8082e126
Reply all
Reply to author
Forward
0 new messages