Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading and Thread Safety in C

When there is a common set of global data that needs to be shared among several threaded processes, I typically have used a thread token to protect the shared resource:

enter image description here

Edit - 7/22/15 (to incorporate atomics as a viable option, per Jens comments)

My [First] question is, in C, if I write my routines in such a way as to guarantee each thread accesses one, and only one element of an array:

enter image description here

Is there any reason to think that asynchronous and simultaneous access to different indices of the same unprotected array (as shown in diagram) would be a problem?

Second question: Given that an object that can be accessed as an atomic entity, even in the presence of asynchronous interrupts ( C99 - 7.14 Signal handling ) would using atomics be an effective method for thread protection for an otherwise unprotected variable?

enter image description here

Edit (Clarifications to address questions in comments to this point):
- Specifics for this application:
- Target OS: Windows 7/8/10
- Compiler : C99 compliant (cannot use C11, which include the _Atomic() type specifier )
- H/W : Intel i7 family

like image 255
ryyker Avatar asked Jul 21 '15 19:07

ryyker


People also ask

What is thread safety in C?

Thread safety A threadsafe function protects shared resources from concurrent access by locks. Thread safety concerns only the implementation of a function and does not affect its external interface. In C language, local variables are dynamically allocated on the stack.

Is C write thread-safe?

C functions are generally thread-safe unless they access some common data (i.e. global or static variables).

What is meant by thread safety?

Thread safety is the avoidance of data races—situations in which data are set to either correct or incorrect values, depending upon the order in which multiple threads access and modify the data. When no sharing is intended, give each thread a private copy of the data.

What is thread safety example?

5) Example of thread-safe class in Java: Vector, Hashtable, ConcurrentHashMap, String, etc. 6) Atomic operations in Java are thread-safe like reading a 32-bit int from memory because it's an atomic operation it can't interleave with other threads.


2 Answers

This (which looks like a C standard of some sort) http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf sayeth:

NOTE 1 Two threads of execution can update and access separate memory locations without interfering with each other

NOTE 13 Compiler transformations that introduce assignments to a potentially shared memory location that would not be modified by the abstract machine are generally precluded by this standard, since such an assignment might overwrite another assignment by a different thread in cases in which an abstract machine execution would not have encountered a data race. This includes implementations of data member assignment that overwrite adjacent members in separate memory locations. We also generally preclude reordering of atomic loads in cases in which the atomics in question may alias, since this may violate the "visible sequence" rules.

The way I understand it, this would preclude quamrana's concerns and guarantee you that unprotected writes to separate memory locations should never result in undefined behavior if there is no data race.

like image 195
PSkocik Avatar answered Oct 12 '22 23:10

PSkocik


In C it will depend on your platform, that is your combination of compiler, processor architecture and operating system.

Your compiler can choose how to use the internal registers and instructions of the cpu to make the executable seem to perform the intent of the program. And C may know nothing about threads. It is usually the job of the operating system to provide a threading library.

There may be processors which might perform the write to an element of your array by reading a much larger patch of memory than just one element, then overwrite just the right bits that forms one element within internal registers and then writing the whole patch back. A single threaded program would work just fine, but two or more threads which interrupt each other could cause chaos in the array.

On the other hand it may work out just fine.

And as has been said, read-only access is always just fine.

Also, google is your friend. It found this stackoverflow question.

like image 23
quamrana Avatar answered Oct 12 '22 23:10

quamrana