Welcome to DU! The truly grassroots left-of-center political community where regular people, not algorithms, drive the discussions and set the standards. Join the community: Create a free account Support DU (and get rid of ads!): Become a Star Member Latest Breaking News General Discussion The DU Lounge All Forums Issue Forums Culture Forums Alliance Forums Region Forums Support Forums Help & Search

DaveJ

(5,023 posts)
Sat Jun 15, 2013, 03:08 PM Jun 2013

Do I need to worry about thread safe methods?

My boss at my new job is mentoring me on making my methods 'thread safe' and such, which I never had to worry about before. I don't understand what he's trying to say. I thought that 'thread safe' pertained to applications where you intentionally create threads. We are not creating any threads. He seems to think that a website has the potential of overwriting variables when multiple users log on. He likes to make methods static and then he started talking about the heap and stack where variables are stored. If we never create a thread, I don't see how that would happen...???

9 replies = new reply since forum marked as read
Highlight: NoneDon't highlight anything 5 newestHighlight 5 most recent replies

Recursion

(56,582 posts)
1. Any function that keeps an internal state needs thread safety
Sat Jun 15, 2013, 03:11 PM
Jun 2013

And that's a surprising number of functions. Check the documentation; they'll usually say if they are re-entrant or not. If not, you have to write your own semaphore.

What platform are you on? Linux processes are actually "threads".

DaveJ

(5,023 posts)
4. I'm using C#/.Net
Mon Jun 17, 2013, 09:13 AM
Jun 2013

I still don't understand why there is any concern that a value will overwrite another value, unless the code is sharing a variable among threads. I would think that this would be well documented or handled by .Net if there were a constant danger of variables overwriting one another.

Recursion

(56,582 posts)
7. This is the importance of the static keyword
Mon Jun 17, 2013, 10:57 AM
Jun 2013

That's the main advantage of Java and C# from a developer's perspective: the VM forces declaration of static members and methods, so that you can see exactly where you need to worry about concurrency.

If you're sure that there can be no two concurrent calls of any static methods (and anything that accesses a static variable is a static method), then you're fine. I'd actively prove it to myself, though.

In terms of why you care, the normal case for bugs is that a method stores some kind of recordkeeping data in a static variable. User A and user B try to write and read that at the same time, and the result is inconsistent. Inconsistency is a bug, and will eventually make something explode.

ManiacJoe

(10,136 posts)
9. This is exactly why you need to think about it.
Tue Jun 18, 2013, 05:27 PM
Jun 2013
I still don't understand why there is any concern that a value will overwrite another value, unless the code is sharing a variable among threads.

This is guaranteed to happen since your code is running inside the multi-threaded web server.

Sometimes sharing variables between threads is sometimes a good thing, sometimes a bad thing. Only you as the programmer knows which case your code falls into. If you do not want the variables shared, you need to code in a certain way to prevent it. It is actually quite easy do to; you just need to be aware of how you are coding things.

jeff47

(26,549 posts)
2. Just because you don't create a thread doesn't mean they aren't created.
Sat Jun 15, 2013, 06:43 PM
Jun 2013

Your application is running inside a web server. That web server will create multiple threads while serving requests.

As a result, your code could actually be running in multiple threads even though you are not creating any threads.

DaveJ

(5,023 posts)
5. I'm aware that threads are created since there are multiple users
Mon Jun 17, 2013, 09:15 AM
Jun 2013

I just don't see anything to indicate that values in the code will overwrite values in other threads that .Net created servicing these requests.

napoleon_in_rags

(3,991 posts)
3. Pretty much any web platform will come with a good degree of thread safety.
Sun Jun 16, 2013, 05:42 PM
Jun 2013

You want to make sure your connection to the database is thread safe, then what's the worry? Of course you understand if your code is like

f = open_file("something.txt", "rb&quot
sleep(random_amount)
f.write(something)
f.close()

than you run the risk that a second user will click the same page while its still sleeping, and won't be able to access the file. But generally, if you keep all the data in functions or in the database, its safe.

napoleon_in_rags

(3,991 posts)
8. Maybe its your boss's love of static methods.
Mon Jun 17, 2013, 01:48 PM
Jun 2013

This is java we're talking about right? If I recall, static methods are called on the class, not the object. So then static attributes will appear for all pages simultaneously. Maybe that's created trouble for him in the past.

I thought the java model was to instantiate a servlet, (object) and then call its service() method in its own thread. Then all you have to worry about is external files, network connections, etc, just like an web app, you have to make sure no conflicts happen there. But that's fricking simple, its called the "synchronized" keyword:

http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

Latest Discussions»Retired Forums»Website, DB, & Software Developers»Do I need to worry about ...