0

How can I measure a code if it is thread-safe or not?

may be general guidelines or best practices

I know that the code to be threading safe is to work across threads without doing unpredictable behavior, but that's sometimes become very tricky and hard to do!

Muhammad Nour
  • 2,109
  • 2
  • 17
  • 24

2 Answers2

0

I came up with one simple rule, which is probably hard to implement and therefore theoretical in nature. Code is not thread safe if you can inject some Sleep operations to some places in the code and so change the outcome of the code in a significant way. The code is thread safe otherwise (there's no such combination of delays that can change the result of code execution).

Not only your code should be taken into account when considering thread safety, but other parts of the code, the framework, the operating system, the external factors, like disk drives and memory... everything. That is why this "rule of thumb" is mainly theoretical.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • "Thread safe" does not imply only one outcome is possible. For example, the outcome of a thread-safe cryptographic random number generator could certainly be affected by the introduction of sleep operations in the algorithm. – Sam Harwell Jan 04 '14 at 16:12
  • @280Z28 indeed, that is why I wrote "in a significant way". It was a deliberate umbrella term that serves me well. The meaning of "significant" varies from case to case. I think that producing one random number instead of the other is not significant when all you want is to test the code for thread safety. – Dialecticus Jan 04 '14 at 16:34
-1

I think The best answer would be here Multi Threading, I couldn't have notice such an answer before writing this question I think it is better to close is it ! thanks


Edit by 280Z28 (since I can't add a new answer to a closed question)

Thread safety of an algorithm or application is typically measured in terms of the consistency model which it is guaranteed to follow in the presence of multiple threads of execution (or multiple processes for distributed systems). The two most important things to examine are the following.

  1. Are the pre- and post-conditions of individual methods preserved when multiple threads are used? For example, if your method "adds an element to a dynamically-sized list", then one post condition would be that the size of the list increases by 1 as a result of the add method. If your algorithm is thread-safe, then calling the add method 2 times would result in the size increasing by exactly 2, regardless of which threads were used for the add operations. On the other hand, if the algorithm is not thread-safe, then using multiple threads for the 2 calls could result in anything, ranging from correctly adding the 2 items all the way to the possibility of crashing the program entirely.
  2. When changes are made to data used by algorithms in the program, when do those changes become visible to the other threads in the system. This is the consistency model of your code. Consistency models can be very difficult to understand fully so I'll leave the link above as the starting place for your continued learning, along with a note that systems guaranteeing linearizability or sequential consistency are often the easiest to work with, although not necessarily the easiest to create.
Community
  • 1
  • 1
Muhammad Nour
  • 2,109
  • 2
  • 17
  • 24