What is the purpose of this pattern using a volatile pointer to "this"?

Caetano Sauer

I have recently come across a curious use of the volatile keyword in C++ multithreaded code. To abstract the programming pattern, let's assume there is a control object which is accessed by one producer and several consumer threads:

class control_t {
    pthread_mutex_t control_lock;
    pthread_cond_t wake_cond;
    bool there_is_work_todo;

    control_t volatile* vthis() { return this; }
}

The consumer thread does the following (c is a non-volatile pointer to the control object):

...
pthread_mutex_lock(c->control_lock)
while (!c->vthis()->there_is_work_todo) {
    pthread_cond_wait(&c->wake_cond, &c->control_lock);
}
...

The idea here is that the consumer threads will wait until there is some work to be done, which the producer signalizes via the wake_cond variable.

What I don't understand here is why the control object is accessed through a volatile pointer to "this", which is returned by the method vthis(). Why is that?

Matthieu M.

The use of volatile in multi-threaded code is generally suspect. volatile was designed to avoid optimizing out reads and writes to memory, which is useful when such reads and writes occur on special addresses that map to hardware registers. See, for example, how volatile is useless to prevent data-races, and how it can be (ab)used as a phantom type...

Since the author used proper synchronization (mutexes and condition variables), the use of of volatile is extremely suspect. Such uses generally stem from a misunderstanding, most notably spread by languages such as Java who reused the same keyword with different semantics.

In C and C++, multi-threaded code should rely on memory barriers, such as those introduced by the mutexes and atomic operations, which guarantee that the values are correctly synchronized across the different CPU cores and caches.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the purpose of this pattern using a volatile pointer to "this"?

From Dev

What is the purpose of the .prototype object when not using the constructor invocation pattern?

From Dev

What is the purpose of an empty loop in this volatile var example?

From Dev

gcc, what is the purpose of asm volatile ("" : : "r" (x))

From Dev

What is the purpose of declaring a function as a pointer?

From Dev

What is this pattern's name / purpose?

From Dev

What is this pattern's name / purpose?

From Dev

Builder Pattern: What is the purpose of the Director?

From Java

What is the purpose of using HKDF?

From Dev

What is Purpose of @using in BeginForm

From Dev

What is the purpose of using ~ in JavaScript?

From Dev

What is Purpose of @using in BeginForm

From Dev

What is the purpose of Function Pointer syntax in C?

From Dev

What is the purpose of Function Pointer syntax in C?

From Dev

What is the purpose of function pointer returned by signal() function?

From Dev

Purpose of Using a double pointer in Linked Lists

From Dev

Purpose/advantages of volatile

From Dev

Purpose/advantages of volatile

From Dev

What is the purpose of using session in WCF

From Dev

BASH: What is the purpose of using `/` in a list?

From Dev

What is the purpose of using nginx with gunicorn?

From Dev

what is the purpose of using JSON in android?

From Dev

BASH: What is the purpose of using `/` in a list?

From Dev

Volatile Pointer to Non Volatile Data

From Dev

What's the purpose of stack pointer alignment in the prologue of main()

From Dev

What's the purpose of align C++ pointer position

From Dev

What is the purpose of a triple pointer for 2d array?

From Java

What's the purpose of applying the Bulkhead pattern on a non-blocking application?

From Dev

What is the purpose of the 'y' sticky pattern modifier in JavaScript RegExps?

Related Related

  1. 1

    What is the purpose of this pattern using a volatile pointer to "this"?

  2. 2

    What is the purpose of the .prototype object when not using the constructor invocation pattern?

  3. 3

    What is the purpose of an empty loop in this volatile var example?

  4. 4

    gcc, what is the purpose of asm volatile ("" : : "r" (x))

  5. 5

    What is the purpose of declaring a function as a pointer?

  6. 6

    What is this pattern's name / purpose?

  7. 7

    What is this pattern's name / purpose?

  8. 8

    Builder Pattern: What is the purpose of the Director?

  9. 9

    What is the purpose of using HKDF?

  10. 10

    What is Purpose of @using in BeginForm

  11. 11

    What is the purpose of using ~ in JavaScript?

  12. 12

    What is Purpose of @using in BeginForm

  13. 13

    What is the purpose of Function Pointer syntax in C?

  14. 14

    What is the purpose of Function Pointer syntax in C?

  15. 15

    What is the purpose of function pointer returned by signal() function?

  16. 16

    Purpose of Using a double pointer in Linked Lists

  17. 17

    Purpose/advantages of volatile

  18. 18

    Purpose/advantages of volatile

  19. 19

    What is the purpose of using session in WCF

  20. 20

    BASH: What is the purpose of using `/` in a list?

  21. 21

    What is the purpose of using nginx with gunicorn?

  22. 22

    what is the purpose of using JSON in android?

  23. 23

    BASH: What is the purpose of using `/` in a list?

  24. 24

    Volatile Pointer to Non Volatile Data

  25. 25

    What's the purpose of stack pointer alignment in the prologue of main()

  26. 26

    What's the purpose of align C++ pointer position

  27. 27

    What is the purpose of a triple pointer for 2d array?

  28. 28

    What's the purpose of applying the Bulkhead pattern on a non-blocking application?

  29. 29

    What is the purpose of the 'y' sticky pattern modifier in JavaScript RegExps?

HotTag

Archive