Thread and Thread Protection in C

When there is a common set of global data that needs to be shared between several stream processes, I usually used a stream token to protect the shared resource:

enter image description here

Edit - 7/22/15 (enable atomics as a viable option, according to Jens)

My [first] question is in C, if I write my routines in such a way as to ensure that each thread accesses it, and only one element of the array:

enter image description here

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

Second question: Given that an object that can be accessed, even an asynchronous interrupt ( C99 - 7.14 Signal Processing ), will an atomic object use atomics , an effective method of protecting threads for another unprotected variable?

enter image description here

Edit (Clarifications for answering questions in the comments to this point):
- Features for this application:
- Target OS: Windows 7/8/10
- Compiler: compatible with C99 (cannot use C11, which includes a specifier like _ Atomic () )
- H / W: Intel i7 family

+7
c multithreading windows c99 atomic
source share
3 answers

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

NOTE 1: Two threads can update and access a separate location memory without interfering with each other.

NOTE 13 A compiler transform that introduces assignments of potentially shared memory that will not be modified by an abstract machine is generally excluded from this standard, since such an assignment may overwrite another assignment with another thread in cases where the execution of the abstract machine would not encounter data consumption . This includes the implementation of an assignment data element that overwrites neighboring elements in separate memory locations. We also usually rule out reordering of atomic loads in cases where atomistics may be a pseudonym, as this may violate the rules of the "visible sequence".

As I understand it, this will prevent quamrana related issues and ensures that unprotected writing to individual memory cells should never lead to undefined behavior if there is no data race.

+2
source share

In C, this will depend on your platform, i.e. your combination of compiler, processor architecture, and operating system.

Your compiler can choose how to use the internal registers and processor instructions so that the executable seems to fulfill the intent of the program. And C knows nothing about threads. Typically, the operating system should provide a thread library.

There may be processors that can write to an element in your array, reading a much larger patch of memory than just one element, and then overwrite only the correct bits that form one element inside the internal registers, and then write the entire patch back. A single-threaded program will work fine, but two or more threads that interrupt each other can cause chaos in the array.

On the other hand, this may work very well.

And as already mentioned, read-only access is always wonderful.

In addition, Google is your friend. He found this stackoverflow question .

+2
source share

If each thread refers to another element of the array, and only the element is "assigned", this should not be a problem. Both scenarios above are essentially equivalent, since each element of the array has its own address.

+1
source share

All Articles