What does threadsafe mean?

what is thread-safe in java
thread safe c#
thread safe and synchronization in java\
thread-safe collection in java
is array thread safe in java
thread safe in java javatpoint
thread-safe c++
java make method thread-safe

Recently I tried to Access a textbox from a thread (other than the UI thread) and an exception was thrown. It said something about the "code not being thread safe" and so I ended up writing a delegate (sample from MSDN helped) and calling it instead.

But even so I didn't quite understand why all the extra code was necessary.

Update: Will I run into any serious problems if I check

Controls.CheckForIllegalCrossThread..blah =true

Eric Lippert has a nice blog post entitled What is this thing you call "thread safe"? about the definition of thread safety as found of Wikipedia.

3 important things extracted from the links :

"A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads."

"In particular, it must satisfy the need for multiple threads to access the same shared data, …"

"…and the need for a shared piece of data to be accessed by only one thread at any given time."

Definitely worth a read!

What does threadsafe mean?, Eric Lippert has a nice blog post entitled What is this thing you call "thread safe"? about the definition of thread safety as found of Wikipedia. In computer programming, thread-safe describes a program portion or routine that can be called from multiple programming threads without unwanted interaction between the threads. A thread is an execution path of a program. A single threaded program will only have one thread and so this problem doesn't arise.

In the simplest of terms threadsafe means that it is safe to be accessed from multiple threads. When you are using multiple threads in a program and they are each attempting to access a common data structure or location in memory several bad things can happen. So, you add some extra code to prevent those bad things. For example, if two people were writing the same document at the same time, the second person to save will overwrite the work of the first person. To make it thread safe then, you have to force person 2 to wait for person 1 to complete their task before allowing person 2 to edit the document.

What does the term 'thread safe' mean in Java?, The concept of thread safe is simply that the program state (fields/objects/​variables) behaves correctly when multiple simultaneous threads are using a resource. Thread safety in Java means that the methods of a class are either atomic or quiescent. So what does atomic and what does quiescent mean? And why are there no other types of thread-safe methods in

What Does Thread Safety Mean in Java?, Thread safety in Java means that the methods of a class are either atomic or quiescent. So what does atomic and what does quiescent mean? Some of the classes in the framework are marked as thread-safe in the documentation. In particular the docs say the following: "Any public static (*Shared* in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe." What exactly does this mean? Does this mean that if I call a shared

Simply , thread safe means that a method or class instance can be used by multiple threads at the same time without any problems occurring.

Consider the following method:

private int myInt = 0;
public int AddOne()
{
    int tmp = myInt;
    tmp = tmp + 1;
    myInt = tmp;
    return tmp;
}

Now thread A and thread B both would like to execute AddOne(). but A starts first and reads the value of myInt (0) into tmp. Now for some reason the scheduler decides to halt thread A and defer execution to thread B. Thread B now also reads the value of myInt (still 0) into it's own variable tmp. Thread B finishes the entire method, so in the end myInt = 1. And 1 is returned. Now it's Thread A's turn again. Thread A continues. And adds 1 to tmp (tmp was 0 for thread A). And then saves this value in myInt. myInt is again 1.

So in this case the method AddOne was called two times, but because the method was not implemented in a thread safe way the value of myInt is not 2, as expected, but 1 because the second thread read the variable myInt before the first thread finished updating it.

Creating thread safe methods is very hard in non trivial cases. And there are quite a few techniques. In Java you can mark a method as synchronized, this means that only one thread can execute that method at a given time. The other threads wait in line. This makes a method thread safe, but if there is a lot of work to be done in a method, then this wastes a lot of space. Another technique is to 'mark only a small part of a method as synchronized' by creating a lock or semaphore, and locking this small part (usually called the critical section). There are even some methods that are implemented as lock-less thread safe, which means that they are built in such a way that multiple threads can race through them at the same time without ever causing problems, this can be the case when a method only executes one atomic call. Atomic calls are calls that can't be interrupted and can only be done by one thread at a time.

Java Concurrency - Thread Safety?, Having optimistically defined “correctness” as something that can be recognized, we can now define thread safety in a somewhat less circular way: a class is  Thread safety is a concept applicable in the context of multi-threaded programs. Multiple thread can access to the same address space at the same time. So, they can write to the exact same memory location at the same time. It is a defining property of threads.

In real world example for the layman is

Let's suppose you have a bank account with the internet and mobile banking and your account have only $10. You performed transfer balance to another account using mobile banking, and the meantime, you did online shopping using the same bank account. If this bank account is not threadsafe, then the bank allows you to perform two transactions at the same time and then the bank will become bankrupt.

Threadsafe means that an object's state doesn't change if simultaneously multiple threads try to access the object.

What is thread-safe?, Thread-safe is a term used to describe a routine that can be called from multiple threads without unwanted interaction between the threads. Thread-safety is a computer programming concept applicable to multi-threaded programs. A piece of code is thread-safe if it is reentrant or protected from multiple simultaneous execution by some form of mutual exclusion. Thread-safety is a key challenge in multi-threaded programming.

What is Thread-Safety and How to Achieve it, In multithreaded environments, we need to write implementations in a thread-​safe way. This means that different threads can access the same  Thread Safety means that binary can work in a multithreaded webserver context, such as Apache 2 on Windows. Thread Safety works by creating a local storage copy in each thread, so that the data won't collide with another thread. So what do I choose?

Reading 20: Thread Safety, What Threadsafe Means. A data type or static method is threadsafe if it behaves correctly when used from multiple  The question was about what it means for StringBuilder to not be thread-safe. But the issue mentioned in your answer has nothing to do with StringBuilder not being thread-safe. Even if you used a thread-safe version of StringBuilder (i.e. StringBuffer , which is why I mentioned StringBuffer ) you would still get the issue mentioned in your answer.

Thread Safety in Java, Thread safety in java is the process to make our program safe to use in multithreaded environment, there are different ways through which we can make our  Threads refer to the highest level of code executed by a processor, so with many threads, your CPU can handle several tasks at the same time. All CPUs have active threads, and every process performed on your computer has at least a single thread.

Comments
  • Typically, "thread safe" means whatever the person using the term thinks it means, at least to that person. As such, it is not a very useful language construct - you need to be much, much more specific when talking about the behaviour of threaded code.
  • Duplicate?: stackoverflow.com/questions/261683/…
  • @dave Sorry I tried searching, but gave up...thanks anyway..
  • a code that doesn't arises Race-Condition
  • Please avoid link only answers as it may become bad anytime in future.
  • updated link: docs.microsoft.com/en-nz/archive/blogs/ericlippert/…
  • This is called synchronization. Right?
  • Yes. Forcing the various threads to wait for access to a shared resource can be accomplished with synchronization.
  • From Gregory's accepted answer, he is saying ""A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads." while you are saying "To make it thread safe then, you have to force person 1 to wait "; isn't he saying simultaneous is acceptable while you are saying it's not? Can you please explain?
  • Its the same thing. I am just suggesting a simple mechanism as an example of what makes code threadsafe. regardless of the mechanism used though multiple threads running the same code should not interfere with each other.
  • So does this only apply to code utilizing global and static variables then? Using your example of people editing documents, I suppose it doesn't make sense to prevent person 2 from running the document-writing code on another document.
  • if method AddOne was called two times