Questions tagged [thread-safety]

A piece of code is thread-safe if it only manipulates data structures in a way that allows consistent execution of this code by multiple threads. A code may be thread safe, conditionally safe (mutual exclusion required) or unsafe (can only be safely used by one thread).

Thread Safety

A piece of code is thread-safe if it only manipulates data structures in a way that allows consistent execution of this code by multiple threads. A code may be thread-safe, conditionally-safe (mutual exclusion required), or unsafe (can only be safely used by one thread).

Main approaches to thread safety include re-entrancy, thread-local storage, mutual exclusion (locking), atomic operations, and immutable objects.

Resources

9311 questions
5
votes
1 answer

PHP cURL - thread safe?

I wrote a PHP script which retrieved data via libcurl and processed it. It worked fine but for performance reasons I changed it to use dozens of workers (threads). The performance improved by more than 50 times, however now php.exe is crashing every…
Matt
  • 774
  • 1
  • 10
  • 28
5
votes
4 answers

How make custom Thread Safe Generic List return the whole list in C#?

I am a threading noob and I am trying to write a custom thread safe generic list class in C# (.NET 3.5 SP1). I've read Why are thread safe collections so hard?. After reviewing the requirements of the class I think I only need to safely add to the…
Jeff
  • 13,079
  • 23
  • 71
  • 102
5
votes
1 answer

Threaded CGO using Go 1.2

Edit: This question is moot. I misread the date on the commit, it is included in existing versions of the Go tools. Thanks, James! It seems that the upcoming release of Go (1.3) will allow non-Go threads to call CGO callbacks. I'm wondering what…
laslowh
  • 8,482
  • 5
  • 34
  • 45
5
votes
4 answers

Java Vector Thread safety

Is there any danger, if im using one Vector(java.util.Vector) on my server program when im accessing it from multiple threads only for reading? (myvector .size() .get() ...) For writing im using synchronized methods. Thank you.
Smarty77
  • 1,208
  • 3
  • 15
  • 30
5
votes
3 answers

Is there a thread-safe way to print in Perl?

I currently have a script that kicks off threads to perform various actions on several directories. A snippet of my script is: #main sub BuildInit { my $actionStr = ""; my $compStr = ""; my @component_dirs; my @compToBeBuilt; …
MrDuk
  • 16,578
  • 18
  • 74
  • 133
5
votes
2 answers

is it thread safe to register for a c# event?

specifically, is the "+=" operation atomic? does it make a difference if i'm using the 'event' keyword, or just a plain old delegate? with most types, its a read, then the "+" operator, and then a write. so, it's not atomic. i'd like to know if…
notallama
  • 1,069
  • 1
  • 8
  • 11
5
votes
1 answer

Asynchronous HTTP Handler and using HttpContext in a background thread?

I was reading Walkthrough: Creating an Asynchronous HTTP Handler and noticed they pass the HttpContext from the handler thread and use it in a WaitCallback which runs on a background thread. It makes calls like _context.Response.Write(). Am I…
Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155
5
votes
2 answers

How to make ActiveRecord ThreadSafe

How can I make the following controller threadsafe in rails 4 with postgresql: def controller_action if Model.exists(column_name:"some_value") else @model=Model.new(column_name:"some_value") @model.save end end I am running puma, so…
5
votes
2 answers

Thread safety between subsequent calls of an Executors.newSingleThreadExecutor

I have a question regarding using a single threaded executor. Since it reuses the same thread, does that means that If I modify an object state in one submit call, can I assume that another modification of that object state in subsequent calls of…
adragomir
  • 457
  • 4
  • 16
  • 33
5
votes
2 answers

static int is getting more count than AtomicInteger in single thread, why so?

Output of the following code is 9123:9158 import java.util.concurrent.atomic.AtomicInteger; public class StackOverflow { private static AtomicInteger atomicInteger = new AtomicInteger(0); private static int staticInt = 0; public…
WarFox
  • 4,933
  • 3
  • 28
  • 32
5
votes
2 answers

Javascript thread-handling and race-conditions

Lets asume I have a code like the following: var shared = 100; function workWithIt(){ shared += 100; } setTimeout(workWithIt, 500); setTimeout(workWithIt, 500); Ideally, this piece of code should add 200 to the variable shared, which is 300…
maja
  • 17,250
  • 17
  • 82
  • 125
5
votes
1 answer

Any way to make sure that calling an external method is thread safe?

For the following code: method() { object.externalMethod(); } If externalMethod() is not thread safe. Say, it starts multiple threads, which do some unsafe stuffs. Can we still make sure method() is thread safe, with no knowledge of…
5
votes
3 answers

Should I declare java.util.concurrent.ConcurrentLinkedQueue references volatile?

I am using a java.util.concurrent.ConcurrentLinkedQueue object to pass data between threads. Should I declare my references volatile?
JLindsey
  • 700
  • 2
  • 7
  • 14
5
votes
5 answers

Multithreading ThreadSeven extends Thread

I have simple question from OCJP Given: 1. public class TestSeven extends Thread { 2. private static int x; 3. public synchronized void doThings() { 4. int current = x; 5. current++; 6. x = current; 7. } 8. …
Srikanth
  • 4,349
  • 3
  • 13
  • 8
5
votes
2 answers

Random.Next() always returns 0

I am using a single Random instance to rapidly get random numbers in a Parallel query, but I have noticed that, eventually, Random.Next always returns zero. Is there a reason for that?
BrainStorm.exe
  • 1,565
  • 3
  • 23
  • 40