Note that there are some explanatory texts on larger screens.

plurals
  1. POconcurrent variable access in c
    primarykey
    data
    text
    <p>I have a fairly specific question about concurrent programming in C. I have done a fair bit of research on this but have seen several conflicting answers, so I'm hoping for some clarification. I have a program that's something like the following (sorry for the longish code block):</p> <pre><code>typedef struct { pthread_mutex_t mutex; /* some shared data */ int eventCounter; } SharedData; SharedData globalSharedData; typedef struct { /* details unimportant */ } NewData; void newData(NewData data) { int localCopyOfCounter; if (/* information contained in new data triggers an event */) { pthread_mutex_lock(&amp;globalSharedData.mutex); localCopyOfCounter = ++globalSharedData.eventCounter; pthread_mutex_unlock(&amp;globalSharedData.mutex); } else { return; } /* Perform long running computation. */ if (localCopyOfCounter != globalSharedData.eventCounter) { /* A new event has happened, old information is stale and the current computation can be aborted. */ return; } /* Perform another long running computation whose results depend on the previous one. */ if (localCopyOfCounter != globalSharedData.eventCounter) { /* Another check for new event that causes information to be stale. */ return; } /* Final stage of computation whose results depend on two previous stages. */ } </code></pre> <p>There is a pool of threads servicing the connection for incoming data, so multiple instances of newData can be running at the same time. In a multi-processor environment there are two problems I'm aware of in getting the counter handling part of this code correct: preventing the compiler from caching the shared counter copy in a register so other threads can't see it, and forcing the CPU to write the store of the counter value to memory in a timely fashion so other threads can see it. I would prefer not to use a synchronization call around the counter checks because a partial read of the counter value is acceptable (it will produce a value different than the local copy, which should be adequate to conclude that an event has occurred). Would it be sufficient to declare the eventCounter field in SharedData to be volatile, or do I need to do something else here? Also is there a better way to handle this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload