Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #23089 > unrolled thread

Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually

Started byMyth__Buster <raghavanil4m@gmail.com>
First post2012-07-05 10:44 -0700
Last post2012-07-07 12:53 -0700
Articles 20 on this page of 51 — 14 participants

Back to article view | Back to comp.lang.c


Contents

  Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Myth__Buster <raghavanil4m@gmail.com> - 2012-07-05 10:44 -0700
    Re: Clarification on the applicability of compile-time optimization to a  struct variable which encompasses a mixture of fields declared as volatile  and non-volatile individually pete <pfiland@mindspring.com> - 2012-07-05 14:59 -0400
      Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Myth__Buster <raghavanil4m@gmail.com> - 2012-07-07 12:53 -0700
    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-05 23:22 +0100
      Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 13:08 -0400
        Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ian Collins <ian-news@hotmail.com> - 2012-07-07 09:20 +1200
          Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 14:01 -0400
            Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-06 18:11 -0400
              Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 14:33 -0400
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ian Collins <ian-news@hotmail.com> - 2012-07-07 11:29 +1200
                  Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 16:27 -0400
                    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ian Collins <ian-news@hotmail.com> - 2012-07-07 12:37 +1200
                      Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 17:29 -0400
                        Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ian Collins <ian-news@hotmail.com> - 2012-07-07 15:50 +1200
                          Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 19:55 -0400
                        Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-07 17:04 +0300
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-07 00:10 -0700
                  Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Gareth Owen <gwowen@gmail.com> - 2012-07-07 10:00 +0100
                    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-07 10:55 -0700
                    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-07 15:13 -0700
                  Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 04:40 -0400
                  Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-07 17:20 +0300
                    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-07 10:33 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-07 08:16 -0400
                  Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 05:12 -0400
                    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-07 09:24 -0400
                      Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 06:07 -0400
                        Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Keith Thompson <kst-u@mib.org> - 2012-07-07 13:26 -0700
                          Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 16:04 -0400
                            Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually James Kuyper <jameskuyper@verizon.net> - 2012-07-09 11:14 -0400
            Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-06 16:58 -0700
              Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 17:20 -0400
                Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-06 19:39 -0700
                  Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 18:54 -0400
                    Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-06 20:12 -0700
                      Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 19:25 -0400
                        Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-06 20:33 -0700
                          Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-06 19:47 -0400
              Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Jens Gustedt <jens.gustedt@loria.fr> - 2012-07-07 15:47 +0200
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Uncle Steve <stevet810@gmail.com> - 2012-07-07 06:58 -0400
                  Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Philip Lantz <prl@canterey.us> - 2012-07-14 21:46 -0700
                Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-07 16:19 -0700
                  Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Jens Gustedt <jens.gustedt@loria.fr> - 2012-07-08 09:40 +0200
                    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually James Kuyper <jameskuyper@verizon.net> - 2012-07-08 10:51 -0400
                  Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Philip Lantz <prl@canterey.us> - 2012-07-13 23:25 -0700
                    Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-14 14:37 -0700
                      Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-14 14:46 -0700
                        Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Philip Lantz <prl@canterey.us> - 2012-07-14 21:39 -0700
      Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-06 17:24 -0700
        Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-07 02:33 +0100
      Re: Clarification on the applicability of compile-time optimization to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually Myth__Buster <raghavanil4m@gmail.com> - 2012-07-07 12:53 -0700

Page 2 of 3 — ← Prev page 1 [2] 3  Next page →


#23149

FromUncle Steve <stevet810@gmail.com>
Date2012-07-07 04:40 -0400
Message-ID<aed725fd25dd131-a14e3@gmail.com>
In reply to#23140
On Sat, Jul 07, 2012 at 12:10:02AM -0700, Tim Rentsch wrote:
> Uncle Steve <stevet810@gmail.com> writes:
> 
> > What is it about the volatile attribute that makes it
> > insufficient in your mind to the problem presented by
> > threaded code?
> 
> Let's take a specific example:
> 
>     volatile int a = 1, b = 2;
> 
>     ... start up threads X and Y ...
> 
>     ... in parallel ... {
> 
>         ... in thread X ... {
>             a = 3;
>             b = 4;    
>         }
> 
>         ... in thread Y ... {
>             printf( "a = %d\n", a );
>             printf( "b = %d\n", b );
>         }
>     
>     }
> 
> Question: what values for a and b might thread Y print?
> 
> Answer:  Besides the obvious possibilities of {1,2}, {3,2},
> and {3,4}, the Standard allows a conforming implementation
> to define volatile access in such a way that thread Y might
> print 1 for a but 4 for b.  In other words, using volatile
> is not enough to guarantee write ordering.  Moreover that
> lack of guarantee holds true even if write-barrier opcodes
> are used before and after each volatile-qualified access.
> 
> So it's kind of unfortunate but true - the meaning of volatile
> is inherently non-portable.  You may be able to write code
> using volatile to do inter-thread communication that works
> reliably on a number of implementations, but that depends
> on decisions made by those implementations, and not just on
> the Standard.

I never said I was relying on the volatile attribute exclusively to
provide coherence between threads.  The bug up my butt concerning the
volatile keyword is more related to presenting consistent results in
d ata structures that might transition several states quickly, and I
don't want the presentation of those states to be optimized out
simply because the compiler decides it can.

I showed the potential for this in the simple timing code I posted
earlier.  Note that removing the volatile qualifier from variables
that have them changes the output.  So, my interest in the volatile
attribute is not related to thread synchronization so much as it is in
general data coherence in a complex threaded application.

Note that I have not yet found out whether structure attributes
propogate to substructures that are structure elements.
 
> > I don't have a copy of the standard, [...]
> 
> Easily solved.  For C99:
> 
>    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
> 
> For C11:
> 
>    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

Thanks, I'll have a look.

I've been reviewing the code I posted previously, and I can no longer
make it malfunction the way it was breaking.  I forget what I changed,
so I'm going to have to check into it before I can better illustrate
the phenomenon.



Regards,

Uncle Steve

-- 
The moon has never been closer.

[toc] | [prev] | [next] | [standalone]


#23156

FromPhil Carmody <thefatphil_demunged@yahoo.co.uk>
Date2012-07-07 17:20 +0300
Message-ID<87obnrzm8b.fsf@bazspaz.fatphil.org>
In reply to#23140
Tim Rentsch <txr@alumni.caltech.edu> writes:
> Uncle Steve <stevet810@gmail.com> writes:
> 
> > What is it about the volatile attribute that makes it
> > insufficient in your mind to the problem presented by
> > threaded code?
> 
> Let's take a specific example:
> 
>     volatile int a = 1, b = 2;
> 
>     ... start up threads X and Y ...
> 
>     ... in parallel ... {
> 
>         ... in thread X ... {
>             a = 3;
>             b = 4;    
>         }
> 
>         ... in thread Y ... {
>             printf( "a = %d\n", a );
>             printf( "b = %d\n", b );
>         }
>     
>     }
> 
> Question: what values for a and b might thread Y print?
> 
> Answer:  Besides the obvious possibilities of {1,2}, {3,2},
> and {3,4}, the Standard allows a conforming implementation
> to define volatile access in such a way that thread Y might
> print 1 for a but 4 for b.  In other words, using volatile
> is not enough to guarantee write ordering. 

The two reads and two writes can be sequenced 6 ways.

ra rb wa wb  - {1,2}
ra wa rb wb  - {1,2}
ra wa wb rb  - {1,4}
wa ra rb wb  - {3,2}
wa ra wb rb  - {3,4}
wa wb ra rb  - {3,4}

{1.4} is not a violation of write ordering - wa is still
before wb.

Changing the order of the two printfs in thread Y would yield

rb ra wa wb  - {2,1}
rb wa ra wb  - {2,3}
rb wa wb ra  - {2,3}
wa rb ra wb  - {2,3}
wa rb wb ra  - {2,3}
wa wb rb ra  - {4,3}

Here the reversed output {4,1} is impossible, as it violates
the write ordering in thread X. (And in fact this read-order
being the reverse of write order is a common technique for 
reducing the number of possibilities if you don't wish to 
turn both the block read and the block writes into atomic 
transactions.)

Phil
-- 
> I'd argue that there is much evidence for the existence of a God.
Pics or it didn't happen.
-- Tom (/. uid 822)

[toc] | [prev] | [next] | [standalone]


#23171

FromTim Rentsch <txr@alumni.caltech.edu>
Date2012-07-07 10:33 -0700
Message-ID<kfny5mv1nol.fsf@x-alumni2.alumni.caltech.edu>
In reply to#23156
Phil Carmody <thefatphil_demunged@yahoo.co.uk> writes:

> Tim Rentsch <txr@alumni.caltech.edu> writes:
>> Uncle Steve <stevet810@gmail.com> writes:
>> 
>> > What is it about the volatile attribute that makes it
>> > insufficient in your mind to the problem presented by
>> > threaded code?
>> 
>> Let's take a specific example:
>> 
>>     volatile int a = 1, b = 2;
>> 
>>     ... start up threads X and Y ...
>> 
>>     ... in parallel ... {
>> 
>>         ... in thread X ... {
>>             a = 3;
>>             b = 4;    
>>         }
>> 
>>         ... in thread Y ... {
>>             printf( "a = %d\n", a );
>>             printf( "b = %d\n", b );
>>         }
>>     
>>     }
>> 
>> Question: what values for a and b might thread Y print?
>> 
>> Answer:  Besides the obvious possibilities of {1,2}, {3,2},
>> and {3,4}, the Standard allows a conforming implementation
>> to define volatile access in such a way that thread Y might
>> print 1 for a but 4 for b.  In other words, using volatile
>> is not enough to guarantee write ordering. 
>
> The two reads and two writes can be sequenced 6 ways.
>
> ra rb wa wb  - {1,2}
> ra wa rb wb  - {1,2}
> ra wa wb rb  - {1,4}
> wa ra rb wb  - {3,2}
> wa ra wb rb  - {3,4}
> wa wb ra rb  - {3,4}
>
> {1.4} is not a violation of write ordering - wa is still
> before wb.
>
> Changing the order of the two printfs in thread Y would yield
>
> rb ra wa wb  - {2,1}
> rb wa ra wb  - {2,3}
> rb wa wb ra  - {2,3}
> wa rb ra wb  - {2,3}
> wa rb wb ra  - {2,3}
> wa wb rb ra  - {4,3}
>
> Here the reversed output {4,1} is impossible, as it violates
> the write ordering in thread X. (And in fact this read-order
> being the reverse of write order is a common technique for 
> reducing the number of possibilities if you don't wish to 
> turn both the block read and the block writes into atomic 
> transactions.)

Good analysis.  Clearly a weakness in my example;
thank you for pointing this out.

However, even if the printf()s in thread Y are done in
the other order, the reversed output {4,1} may occur in
a conforming implementation.  This result holds because
of the great latitude granted by 6.7.3 p7, which allows
implementations to define what it means to access a
volatile object.  That freedom means that how the
C program behaves could be different than what would
happen if, for example, an equivalent assembly language
program were run directly on the native hardware.

[toc] | [prev] | [next] | [standalone]


#23148

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-07-07 08:16 -0400
Message-ID<jt99bs$l1l$1@dont-email.me>
In reply to#23114
On 7/6/2012 2:33 PM, Uncle Steve wrote:
> On Fri, Jul 06, 2012 at 06:11:28PM -0400, Eric Sosman wrote:
>> [...]
>>      Although the latest "C11" Standard adds some multi-threading
>> support, [...]
>>
>>      [...]I suggest you visit comp.programming.threads,
>> a newsgroup devoted to multi-threading issues.  There you will learn
>> that `volatile' is neither necessary *nor* sufficient in connection
>> with variables shared between threads.  [...]
>
> Is there are C standard threading framework?

     See above.

> Threaded code is a de-facto reality in today's multi-core environment.
> Save for the embedded sector, most systems will run threads in
> parallel, and that is that.  C has to recognize this watershed moment
> in the refinement of the language.

     See above.

> What is it about the volatile attribute that makes it
> insufficient in your mind to the problem presented by threaded code?

     "Then Kolokolo Bird said, with a mournful cry, 'Go to the banks of
the great grey-green, greasy Limpopo River -- er, that is, the G,G-G,G
comp.programming.threads newsgroup -- all set about with fever-trees,
and find out.'"

     By "go," I mean "go away," as in "Don't continue this thread in
comp.lang.c unless you specifically wish to discuss the threading
features of C11."  Follow-ups set.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#23150

FromUncle Steve <stevet810@gmail.com>
Date2012-07-07 05:12 -0400
Message-ID<55a75ee50390481-3bc95@gmail.com>
In reply to#23148
On Sat, Jul 07, 2012 at 08:16:53AM -0400, Eric Sosman wrote:
> On 7/6/2012 2:33 PM, Uncle Steve wrote:
> >On Fri, Jul 06, 2012 at 06:11:28PM -0400, Eric Sosman wrote:
> >>[...]
> >>     Although the latest "C11" Standard adds some multi-threading
> >>support, [...]
> >>
> >>     [...]I suggest you visit comp.programming.threads,
> >>a newsgroup devoted to multi-threading issues.  There you will learn
> >>that `volatile' is neither necessary *nor* sufficient in connection
> >>with variables shared between threads.  [...]
> >
> >Is there are C standard threading framework?
> 
>     See above.
> 
> >Threaded code is a de-facto reality in today's multi-core environment.
> >Save for the embedded sector, most systems will run threads in
> >parallel, and that is that.  C has to recognize this watershed moment
> >in the refinement of the language.
> 
>     See above.
> 
> >What is it about the volatile attribute that makes it
> >insufficient in your mind to the problem presented by threaded code?
> 
>     "Then Kolokolo Bird said, with a mournful cry, 'Go to the banks of
> the great grey-green, greasy Limpopo River -- er, that is, the G,G-G,G
> comp.programming.threads newsgroup -- all set about with fever-trees,
> and find out.'"
> 
>     By "go," I mean "go away," as in "Don't continue this thread in
> comp.lang.c unless you specifically wish to discuss the threading
> features of C11."  Follow-ups set.

Your message excells as an example of myopic religious orthodoxy.  I'm
not actually suggesting that there should be changes to the standard,
so I don't quite understand why you seem so threatened.


Regards,

Uncle Steve

-- 
The moon has never been closer.

[toc] | [prev] | [next] | [standalone]


#23151

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-07-07 09:24 -0400
Message-ID<jt9daf$8v5$1@dont-email.me>
In reply to#23150
On 7/7/2012 5:12 AM, Uncle Steve wrote:
> On Sat, Jul 07, 2012 at 08:16:53AM -0400, Eric Sosman wrote:
>> On 7/6/2012 2:33 PM, Uncle Steve wrote:
>>> On Fri, Jul 06, 2012 at 06:11:28PM -0400, Eric Sosman wrote:
>>>> [...]
>>>>      Although the latest "C11" Standard adds some multi-threading
>>>> support, [...]
>>>>
>>>>      [...]I suggest you visit comp.programming.threads,
>>>> a newsgroup devoted to multi-threading issues.  There you will learn
>>>> that `volatile' is neither necessary *nor* sufficient in connection
>>>> with variables shared between threads.  [...]
>>>
>>> Is there are C standard threading framework?
>>
>>      See above.
>>
>>> Threaded code is a de-facto reality in today's multi-core environment.
>>> Save for the embedded sector, most systems will run threads in
>>> parallel, and that is that.  C has to recognize this watershed moment
>>> in the refinement of the language.
>>
>>      See above.
>>
>>> What is it about the volatile attribute that makes it
>>> insufficient in your mind to the problem presented by threaded code?
>>
>>      "Then Kolokolo Bird said, with a mournful cry, 'Go to the banks of
>> the great grey-green, greasy Limpopo River -- er, that is, the G,G-G,G
>> comp.programming.threads newsgroup -- all set about with fever-trees,
>> and find out.'"
>>
>>      By "go," I mean "go away," as in "Don't continue this thread in
>> comp.lang.c unless you specifically wish to discuss the threading
>> features of C11."  Follow-ups set.
>
> Your message excells as an example of myopic religious orthodoxy.  I'm
> not actually suggesting that there should be changes to the standard,
> so I don't quite understand why you seem so threatened.

     When you ask (somewhat incoherently) "Is there are C standard
threading framework," which part of

	THE LATEST "C11" STANDARD ADDS SOME MULTI-THREADING SUPPORT

are you having trouble understanding?

     C11's threading support is on-topic here in comp.lang.c -- but
you've indicated that you're not interested in C11 threads and in fact
are using Pthreads instead.  Pthreads is off-topic here, so please help
reduce the noise by removing the discussion to comp.programming.threads
which "excells" [sic] on that topic.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#23154

FromUncle Steve <stevet810@gmail.com>
Date2012-07-07 06:07 -0400
Message-ID<373fba1c2f082e0-4af63@gmail.com>
In reply to#23151
On Sat, Jul 07, 2012 at 09:24:25AM -0400, Eric Sosman wrote:
> On 7/7/2012 5:12 AM, Uncle Steve wrote:
> >On Sat, Jul 07, 2012 at 08:16:53AM -0400, Eric Sosman wrote:
> >>On 7/6/2012 2:33 PM, Uncle Steve wrote:
> >>>On Fri, Jul 06, 2012 at 06:11:28PM -0400, Eric Sosman wrote:
> >>>>[...]
> >>>>     Although the latest "C11" Standard adds some multi-threading
> >>>>support, [...]
> >>>>
> >>>>     [...]I suggest you visit comp.programming.threads,
> >>>>a newsgroup devoted to multi-threading issues.  There you will learn
> >>>>that `volatile' is neither necessary *nor* sufficient in connection
> >>>>with variables shared between threads.  [...]
> >>>
> >>>Is there are C standard threading framework?
> >>
> >>     See above.
> >>
> >>>Threaded code is a de-facto reality in today's multi-core environment.
> >>>Save for the embedded sector, most systems will run threads in
> >>>parallel, and that is that.  C has to recognize this watershed moment
> >>>in the refinement of the language.
> >>
> >>     See above.
> >>
> >>>What is it about the volatile attribute that makes it
> >>>insufficient in your mind to the problem presented by threaded code?
> >>
> >>     "Then Kolokolo Bird said, with a mournful cry, 'Go to the banks of
> >>the great grey-green, greasy Limpopo River -- er, that is, the G,G-G,G
> >>comp.programming.threads newsgroup -- all set about with fever-trees,
> >>and find out.'"
> >>
> >>     By "go," I mean "go away," as in "Don't continue this thread in
> >>comp.lang.c unless you specifically wish to discuss the threading
> >>features of C11."  Follow-ups set.
> >
> >Your message excells as an example of myopic religious orthodoxy.  I'm
> >not actually suggesting that there should be changes to the standard,
> >so I don't quite understand why you seem so threatened.
> 
>     When you ask (somewhat incoherently) "Is there are C standard
> threading framework," which part of
> 
> 	THE LATEST "C11" STANDARD ADDS SOME MULTI-THREADING SUPPORT
> 
> are you having trouble understanding?
> 
>     C11's threading support is on-topic here in comp.lang.c -- but
> you've indicated that you're not interested in C11 threads and in fact
> are using Pthreads instead.  Pthreads is off-topic here, so please help
> reduce the noise by removing the discussion to comp.programming.threads
> which "excells" [sic] on that topic.

It was previously mentioned that C11 threads were copied from
pthreads, although presumably there would be some difference in
semantics.  Please don't cut off my hands if I inadvertently say
something against the standard, eh?  I approve of standards, and I'm
not just saying that to get on your good side.


Regards,

Uncle Steve

-- 
The moon has never been closer.

[toc] | [prev] | [next] | [standalone]


#23188

FromKeith Thompson <kst-u@mib.org>
Date2012-07-07 13:26 -0700
Message-ID<lna9zb1fog.fsf@nuthaus.mib.org>
In reply to#23154
Uncle Steve <stevet810@gmail.com> writes:
[...]
> It was previously mentioned that C11 threads were copied from
> pthreads, although presumably there would be some difference in
> semantics.  Please don't cut off my hands if I inadvertently say
> something against the standard, eh?  I approve of standards, and I'm
> not just saying that to get on your good side.

Nobody is cutting off your hands.  It's being suggested that,
unless you specifically want to discuss C11's threading support,
this discussion would be more appropriate in a different newsgroup.
Please don't overreact.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#23194

FromUncle Steve <stevet810@gmail.com>
Date2012-07-07 16:04 -0400
Message-ID<e70f28c36b2e6d1-9dc75@gmail.com>
In reply to#23188
On Sat, Jul 07, 2012 at 01:26:07PM -0700, Keith Thompson wrote:
> Uncle Steve <stevet810@gmail.com> writes:
> [...]
> > It was previously mentioned that C11 threads were copied from
> > pthreads, although presumably there would be some difference in
> > semantics.  Please don't cut off my hands if I inadvertently say
> > something against the standard, eh?  I approve of standards, and I'm
> > not just saying that to get on your good side.
> 
> Nobody is cutting off your hands.  It's being suggested that,
> unless you specifically want to discuss C11's threading support,
> this discussion would be more appropriate in a different newsgroup.
> Please don't overreact.

Well, I hope not since voice recognition isn't as efficient as typing.
I realize that c.l.c. is for discussions about the C language, without
necessary reference to any particular implementation.  Nevertheless,
with threads supported by C11 I would imagine some discussion might be
allowed.


Regards,

Uncle Steve

-- 
The moon has never been closer.

[toc] | [prev] | [next] | [standalone]


#23214

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-07-09 11:14 -0400
Message-ID<4FFAF554.1040509@verizon.net>
In reply to#23194
On 07/07/2012 04:04 PM, Uncle Steve wrote:
...
> I realize that c.l.c. is for discussions about the C language, without
> necessary reference to any particular implementation.  Nevertheless,
> with threads supported by C11 I would imagine some discussion might be
> allowed.

No one can disallow anything in an unmoderated newsgroup like this.

However, if you insist on discussing pthreads rather than C11 threads,
c.l.c is not as a good a place to have a well-informed discussion as
comp.programming.threads. If you deliberately want to get less than the
best possible answers to your question, continue posting here; but if
that is your goal, then surely a newsgroup completely unrelated to any
kind of computer programming, such as alt.fun.with.steve, would serve
that goal even better than this newsgroup?

[toc] | [prev] | [next] | [standalone]


#23121

FromJoshua Maurice <joshuamaurice@gmail.com>
Date2012-07-06 16:58 -0700
Message-ID<3ad80f94-cbba-4bf4-98ff-000177e7383e@q29g2000vby.googlegroups.com>
In reply to#23112
On Jul 6, 11:01 am, Uncle Steve <stevet...@gmail.com> wrote:
> On Sat, Jul 07, 2012 at 09:20:57AM +1200, Ian Collins wrote:
> > On 07/ 7/12 05:08 AM, Uncle Steve wrote:
> > >This is a concern in my mind right now as I'm writing some posix
> > >threads code, and so obviously there are a large number of variables
> > >which ought to be declared volatile.
>
> > What leads you to that conclusion?  That line of though usually implies
> > you are misunderstanding the relationship between volatile and threads.
>
> > What are you trying to achieve with volatile?
>
> Well, I have some data structures that will be accessed from arbitrary
> threads, so I want to make sure there's no funny business happening
> behind the scenes where the compiler decides that certain accesses are
> optional.
>
> [snip]
>
> The variable declared volatile are required otherwise some variable
> accesses are optimized out, giving inaccurate results during the data
> acquisition phase.  The problems here are magnified when these access
> patterns occur with competing threads.  IIRC, mutexes are not memory
> access barriers and solve different problems.

To be topical, here's the answer for C11: volatile is useless as a
portable threading construct. Do not use it. It's (maybe) useful in a
portable way only for 3 obscure situations: MMIO, setjmp and longjmp
stuff, and signal handling. If you properly use C11 mutexes, there is
absolutely no need for volatile. To paraphrase you else-thread, there
will be no "funny business" if you use mutexes correctly. It tells the
compiler, the hardware, and anything else relevant, to obey the
semantics of the mutex. volatile is similarly useless when you
correctly use the new "weak" C11 atomics. volatile adds nothing
(portably).

Now, to finally answer your question, C11 threading semantics,
including the lack of useful volatile semantics, is basically copied
wholesale from pthreads. Don't use volatile as a portable threading
construct with pthreads either. pthread mutexes do everything you
need. volatile adds absolutely nothing (portably).

(Yes, you can accomplish useful code with volatile for specific
platform and specific compilers with specific settings. That's an
"advanced" topic that I won't cover now.)

Now, what do you mean by "mutexes are not memory access barriers"?
Mutexes do two things: they provide blocking semantics for mutual
exclusion, and they guarantee visibility ala the usual "happens-
before" semantics. Mutexes are implemented with hardware memory
fences / barriers. This is all true for mutexes in pthreads, win32
threads, Boost threads, ACE threads, C11 threads, C++11 threads, and
every case I can recall seeing the word "mutex".

[toc] | [prev] | [next] | [standalone]


#23129 — Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually

FromUncle Steve <stevet810@gmail.com>
Date2012-07-06 17:20 -0400
SubjectRe: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually
Message-ID<046b22079b2080c-b9239@gmail.com>
In reply to#23121
On Fri, Jul 06, 2012 at 04:58:09PM -0700, Joshua Maurice wrote:
> On Jul 6, 11:01 am, Uncle Steve <stevet...@gmail.com> wrote:
> > On Sat, Jul 07, 2012 at 09:20:57AM +1200, Ian Collins wrote:
> > > On 07/ 7/12 05:08 AM, Uncle Steve wrote:
> > > >This is a concern in my mind right now as I'm writing some posix
> > > >threads code, and so obviously there are a large number of variables
> > > >which ought to be declared volatile.
> >
> > > What leads you to that conclusion?  That line of though usually implies
> > > you are misunderstanding the relationship between volatile and threads.
> >
> > > What are you trying to achieve with volatile?
> >
> > Well, I have some data structures that will be accessed from arbitrary
> > threads, so I want to make sure there's no funny business happening
> > behind the scenes where the compiler decides that certain accesses are
> > optional.
> >
> > [snip]
> >
> > The variable declared volatile are required otherwise some variable
> > accesses are optimized out, giving inaccurate results during the data
> > acquisition phase.  The problems here are magnified when these access
> > patterns occur with competing threads.  IIRC, mutexes are not memory
> > access barriers and solve different problems.
> 
> To be topical, here's the answer for C11: volatile is useless as a
> portable threading construct. Do not use it. It's (maybe) useful in a
> portable way only for 3 obscure situations: MMIO, setjmp and longjmp
> stuff, and signal handling. If you properly use C11 mutexes, there is
> absolutely no need for volatile. To paraphrase you else-thread, there
> will be no "funny business" if you use mutexes correctly. It tells the
> compiler, the hardware, and anything else relevant, to obey the
> semantics of the mutex. volatile is similarly useless when you
> correctly use the new "weak" C11 atomics. volatile adds nothing
> (portably).
> 
> Now, to finally answer your question, C11 threading semantics,
> including the lack of useful volatile semantics, is basically copied
> wholesale from pthreads. Don't use volatile as a portable threading
> construct with pthreads either. pthread mutexes do everything you
> need. volatile adds absolutely nothing (portably).
> 
> (Yes, you can accomplish useful code with volatile for specific
> platform and specific compilers with specific settings. That's an
> "advanced" topic that I won't cover now.)
> 
> Now, what do you mean by "mutexes are not memory access barriers"?
> Mutexes do two things: they provide blocking semantics for mutual
> exclusion, and they guarantee visibility ala the usual "happens-
> before" semantics. Mutexes are implemented with hardware memory
> fences / barriers. This is all true for mutexes in pthreads, win32
> threads, Boost threads, ACE threads, C11 threads, C++11 threads, and
> every case I can recall seeing the word "mutex".

Well, mutexes are barriers for the memory accesses associated with the
mutex itself, but not in general.  Reading the above I can tell that
you think I'm using POSIX mutexes, however that is not the case.  I am
using a custom locking scheme that allows for concurrent readers, but
is exclusive for writers.  As a consequence, the behavior of
variables marked volatile in my code is critical, so I don't want to
mistakenly use it in a way that results in surprises.

Now you can say that what I am doing is non-portable, but you would
be wrong there since my algorithms are in fact portable as long as I
am following the rules of the C standard.  Perhaps the proper term is
limited portability as I rely on a tiny amount of platform specific
asm.  (One instruction.)


Regards,

Uncle Steve

-- 
The moon has never been closer.

[toc] | [prev] | [next] | [standalone]


#23132 — Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually

FromJoshua Maurice <joshuamaurice@gmail.com>
Date2012-07-06 19:39 -0700
SubjectRe: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually
Message-ID<b85c6e84-07dc-4b1c-b94e-d36ea0af03fb@n33g2000vbi.googlegroups.com>
In reply to#23129
On Jul 6, 2:20 pm, Uncle Steve <stevet...@gmail.com> wrote:
> On Fri, Jul 06, 2012 at 04:58:09PM -0700, Joshua Maurice wrote:
> > To be topical, here's the answer for C11: volatile is useless as a
> > portable threading construct. Do not use it. It's (maybe) useful in a
> > portable way only for 3 obscure situations: MMIO, setjmp and longjmp
> > stuff, and signal handling. If you properly use C11 mutexes, there is
> > absolutely no need for volatile. To paraphrase you else-thread, there
> > will be no "funny business" if you use mutexes correctly. It tells the
> > compiler, the hardware, and anything else relevant, to obey the
> > semantics of the mutex. volatile is similarly useless when you
> > correctly use the new "weak" C11 atomics. volatile adds nothing
> > (portably).
>
> > Now, to finally answer your question, C11 threading semantics,
> > including the lack of useful volatile semantics, is basically copied
> > wholesale from pthreads. Don't use volatile as a portable threading
> > construct with pthreads either. pthread mutexes do everything you
> > need. volatile adds absolutely nothing (portably).
>
> > (Yes, you can accomplish useful code with volatile for specific
> > platform and specific compilers with specific settings. That's an
> > "advanced" topic that I won't cover now.)
>
> > Now, what do you mean by "mutexes are not memory access barriers"?
> > Mutexes do two things: they provide blocking semantics for mutual
> > exclusion, and they guarantee visibility ala the usual "happens-
> > before" semantics. Mutexes are implemented with hardware memory
> > fences / barriers. This is all true for mutexes in pthreads, win32
> > threads, Boost threads, ACE threads, C11 threads, C++11 threads, and
> > every case I can recall seeing the word "mutex".
>
> Well, mutexes are barriers for the memory accesses associated with the
> mutex itself, but not in general.  Reading the above I can tell that
> you think I'm using POSIX mutexes, however that is not the case.

I strongly suggest that you don't use the word "mutex" for your code
because you will confuse most readers. Most readers will associate
"mutex" with both mutual exclusion and "happens-before" global memory
visibility. That's how the term has been commonly used (ex: posix
pthreads), and how it will continue to commonly be used (ex: C11).

> I am
> using a custom locking scheme that allows for concurrent readers, but
> is exclusive for writers.  As a consequence, the behavior of
> variables marked volatile in my code is critical, so I don't want to
> mistakenly use it in a way that results in surprises.
>
> Now you can say that what I am doing is non-portable, but you would
> be wrong there since my algorithms are in fact portable as long as I
> am following the rules of the C standard.  Perhaps the proper term is
> limited portability as I rely on a tiny amount of platform specific
> asm.  (One instruction.)

I will say it again: the C89 and C99 standards do not mean what you
think. What the compiler writers and thread library writers understand
volatile to mean is not what you understand volatile to mean, and at
the end of the day what the compiler writers and thread library
writers think is paramount. You may be able to get this to work for
specific platforms with specific compilers. I suggest you ask in a
location specific to such implementations for such platform specific
hackery. However, do not look at the C standard for guidance on this.

In the C11 parlance, what you're trying to write requires the "weak"
atomics, such as relaxed, acquire, release, and consume memory
operations. Attempting to write stuff like this without C11 support
with the C11 primitives is a wholly platform specific affair, and
neither C89 nor C90 can help you there. volatile specifically cannot
help you (barring platform specific documentation to the contrary).

From else-thread:
On Jul 6, 2:29 pm, Uncle Steve <stevet...@gmail.com> wrote:
> On Sat, Jul 07, 2012 at 12:37:59PM +1200, Ian Collins wrote:
> > Then use your platform's native threading primitives.
>
> Doing so would incur a performance penalty in comparison to my
> synchronization primitives in some circumstances.

That's your problem. You have broken code. Your options are:
1- broken code (e.g. using volatile and asm memory barriers absent
platform specific guarantees)
2- code that relies on platform specific guarantees (e.g. with using
volatile and asm memory barriers)
3- "slow" code (e.g. with posix mutexes)
4- C11 code

[toc] | [prev] | [next] | [standalone]


#23133 — Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually

FromUncle Steve <stevet810@gmail.com>
Date2012-07-06 18:54 -0400
SubjectRe: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually
Message-ID<c365530590f39fd-5d0be@gmail.com>
In reply to#23132
On Fri, Jul 06, 2012 at 07:39:56PM -0700, Joshua Maurice wrote:
> On Jul 6, 2:20 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > On Fri, Jul 06, 2012 at 04:58:09PM -0700, Joshua Maurice wrote:
> > > To be topical, here's the answer for C11: volatile is useless as a
> > > portable threading construct. Do not use it. It's (maybe) useful in a
> > > portable way only for 3 obscure situations: MMIO, setjmp and longjmp
> > > stuff, and signal handling. If you properly use C11 mutexes, there is
> > > absolutely no need for volatile. To paraphrase you else-thread, there
> > > will be no "funny business" if you use mutexes correctly. It tells the
> > > compiler, the hardware, and anything else relevant, to obey the
> > > semantics of the mutex. volatile is similarly useless when you
> > > correctly use the new "weak" C11 atomics. volatile adds nothing
> > > (portably).
> >
> > > Now, to finally answer your question, C11 threading semantics,
> > > including the lack of useful volatile semantics, is basically copied
> > > wholesale from pthreads. Don't use volatile as a portable threading
> > > construct with pthreads either. pthread mutexes do everything you
> > > need. volatile adds absolutely nothing (portably).
> >
> > > (Yes, you can accomplish useful code with volatile for specific
> > > platform and specific compilers with specific settings. That's an
> > > "advanced" topic that I won't cover now.)
> >
> > > Now, what do you mean by "mutexes are not memory access barriers"?
> > > Mutexes do two things: they provide blocking semantics for mutual
> > > exclusion, and they guarantee visibility ala the usual "happens-
> > > before" semantics. Mutexes are implemented with hardware memory
> > > fences / barriers. This is all true for mutexes in pthreads, win32
> > > threads, Boost threads, ACE threads, C11 threads, C++11 threads, and
> > > every case I can recall seeing the word "mutex".
> >
> > Well, mutexes are barriers for the memory accesses associated with the
> > mutex itself, but not in general.  Reading the above I can tell that
> > you think I'm using POSIX mutexes, however that is not the case.
> 
> I strongly suggest that you don't use the word "mutex" for your code
> because you will confuse most readers. Most readers will associate
> "mutex" with both mutual exclusion and "happens-before" global memory
> visibility. That's how the term has been commonly used (ex: posix
> pthreads), and how it will continue to commonly be used (ex: C11).
> 
> > I am
> > using a custom locking scheme that allows for concurrent readers, but
> > is exclusive for writers.  As a consequence, the behavior of
> > variables marked volatile in my code is critical, so I don't want to
> > mistakenly use it in a way that results in surprises.
> >
> > Now you can say that what I am doing is non-portable, but you would
> > be wrong there since my algorithms are in fact portable as long as I
> > am following the rules of the C standard.  Perhaps the proper term is
> > limited portability as I rely on a tiny amount of platform specific
> > asm.  (One instruction.)
> 
> I will say it again: the C89 and C99 standards do not mean what you
> think. What the compiler writers and thread library writers understand
> volatile to mean is not what you understand volatile to mean, and at
> the end of the day what the compiler writers and thread library
> writers think is paramount. You may be able to get this to work for
> specific platforms with specific compilers. I suggest you ask in a
> location specific to such implementations for such platform specific
> hackery. However, do not look at the C standard for guidance on this.

Um, what is it that compiler writers and thread library writers
understand volatile to mean?
 
> In the C11 parlance, what you're trying to write requires the "weak"
> atomics, such as relaxed, acquire, release, and consume memory
> operations. Attempting to write stuff like this without C11 support
> with the C11 primitives is a wholly platform specific affair, and
> neither C89 nor C90 can help you there. volatile specifically cannot
> help you (barring platform specific documentation to the contrary).

You seem to be making too many assumptions about my code.
 
> From else-thread:
> On Jul 6, 2:29 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > On Sat, Jul 07, 2012 at 12:37:59PM +1200, Ian Collins wrote:
> > > Then use your platform's native threading primitives.
> >
> > Doing so would incur a performance penalty in comparison to my
> > synchronization primitives in some circumstances.
> 
> That's your problem. You have broken code. Your options are:
> 1- broken code (e.g. using volatile and asm memory barriers absent
> platform specific guarantees)
> 2- code that relies on platform specific guarantees (e.g. with using
> volatile and asm memory barriers)
> 3- "slow" code (e.g. with posix mutexes)
> 4- C11 code

How do you know, a priori, that my code is broken?  Do I have to
change my passwords again?


Regards,

Uncle Steve

-- 
The moon has never been closer.

[toc] | [prev] | [next] | [standalone]


#23134 — Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually

FromJoshua Maurice <joshuamaurice@gmail.com>
Date2012-07-06 20:12 -0700
SubjectRe: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually
Message-ID<2a01b1e1-8607-47b3-a40b-44466901c511@j9g2000vbk.googlegroups.com>
In reply to#23133
On Jul 6, 3:54 pm, Uncle Steve <stevet...@gmail.com> wrote:
> On Fri, Jul 06, 2012 at 07:39:56PM -0700, Joshua Maurice wrote:
> > I will say it again: the C89 and C99 standards do not mean what you
> > think. What the compiler writers and thread library writers understand
> > volatile to mean is not what you understand volatile to mean, and at
> > the end of the day what the compiler writers and thread library
> > writers think is paramount. You may be able to get this to work for
> > specific platforms with specific compilers. I suggest you ask in a
> > location specific to such implementations for such platform specific
> > hackery. However, do not look at the C standard for guidance on this.
>
> Um, what is it that compiler writers and thread library writers
> understand volatile to mean?

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2016.html

> > In the C11 parlance, what you're trying to write requires the "weak"
> > atomics, such as relaxed, acquire, release, and consume memory
> > operations. Attempting to write stuff like this without C11 support
> > with the C11 primitives is a wholly platform specific affair, and
> > neither C89 nor C90 can help you there. volatile specifically cannot
> > help you (barring platform specific documentation to the contrary).
>
> You seem to be making too many assumptions about my code.

I think it's pretty clear from what you've written else-thread.

> > From else-thread:
> > On Jul 6, 2:29 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > > On Sat, Jul 07, 2012 at 12:37:59PM +1200, Ian Collins wrote:
> > > > Then use your platform's native threading primitives.
>
> > > Doing so would incur a performance penalty in comparison to my
> > > synchronization primitives in some circumstances.
>
> > That's your problem. You have broken code. Your options are:
> > 1- broken code (e.g. using volatile and asm memory barriers absent
> > platform specific guarantees)
> > 2- code that relies on platform specific guarantees (e.g. with using
> > volatile and asm memory barriers)
> > 3- "slow" code (e.g. with posix mutexes)
> > 4- C11 code
>
> How do you know, a priori, that my code is broken?  Do I have to
> change my passwords again?

How do I know it's broken? By your own admission, you're using
volatile to try and affect inter-thread visibility (absent constructs
that actually give inter-thread visibility). By your previous
statements about using the C standard to derive useful threading
semantics for volatile, I doubt you've looked at the compiler
documentation and hardware documentation for your implementation. Thus
you have broken code. (I don't think you are trying to nit on "it
could be right by coincidence".)

[toc] | [prev] | [next] | [standalone]


#23135 — Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually

FromUncle Steve <stevet810@gmail.com>
Date2012-07-06 19:25 -0400
SubjectRe: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually
Message-ID<fd30057892b3a7b-9f30b@gmail.com>
In reply to#23134
On Fri, Jul 06, 2012 at 08:12:57PM -0700, Joshua Maurice wrote:
> On Jul 6, 3:54 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > On Fri, Jul 06, 2012 at 07:39:56PM -0700, Joshua Maurice wrote:
> > > I will say it again: the C89 and C99 standards do not mean what you
> > > think. What the compiler writers and thread library writers understand
> > > volatile to mean is not what you understand volatile to mean, and at
> > > the end of the day what the compiler writers and thread library
> > > writers think is paramount. You may be able to get this to work for
> > > specific platforms with specific compilers. I suggest you ask in a
> > > location specific to such implementations for such platform specific
> > > hackery. However, do not look at the C standard for guidance on this.
> >
> > Um, what is it that compiler writers and thread library writers
> > understand volatile to mean?
> 
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2016.html
> 
> > > In the C11 parlance, what you're trying to write requires the "weak"
> > > atomics, such as relaxed, acquire, release, and consume memory
> > > operations. Attempting to write stuff like this without C11 support
> > > with the C11 primitives is a wholly platform specific affair, and
> > > neither C89 nor C90 can help you there. volatile specifically cannot
> > > help you (barring platform specific documentation to the contrary).
> >
> > You seem to be making too many assumptions about my code.
> 
> I think it's pretty clear from what you've written else-thread.
> 
> > > From else-thread:
> > > On Jul 6, 2:29 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > > > On Sat, Jul 07, 2012 at 12:37:59PM +1200, Ian Collins wrote:
> > > > > Then use your platform's native threading primitives.
> >
> > > > Doing so would incur a performance penalty in comparison to my
> > > > synchronization primitives in some circumstances.
> >
> > > That's your problem. You have broken code. Your options are:
> > > 1- broken code (e.g. using volatile and asm memory barriers absent
> > > platform specific guarantees)
> > > 2- code that relies on platform specific guarantees (e.g. with using
> > > volatile and asm memory barriers)
> > > 3- "slow" code (e.g. with posix mutexes)
> > > 4- C11 code
> >
> > How do you know, a priori, that my code is broken?  Do I have to
> > change my passwords again?
> 
> How do I know it's broken? By your own admission, you're using
> volatile to try and affect inter-thread visibility (absent constructs
> that actually give inter-thread visibility). By your previous
> statements about using the C standard to derive useful threading
> semantics for volatile, I doubt you've looked at the compiler
> documentation and hardware documentation for your implementation. Thus
> you have broken code. (I don't think you are trying to nit on "it
> could be right by coincidence".)

Well you know what?  You're going to have to wait until version 1.0
before you get to decide how wrong it is.


Regards,

Uncle Steve

-- 
The moon has never been closer.

[toc] | [prev] | [next] | [standalone]


#23136 — Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually

FromJoshua Maurice <joshuamaurice@gmail.com>
Date2012-07-06 20:33 -0700
SubjectRe: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually
Message-ID<362f5134-1a84-457c-8fec-b55e988b6d12@f30g2000vbz.googlegroups.com>
In reply to#23135
On Jul 6, 4:25 pm, Uncle Steve <stevet...@gmail.com> wrote:
> On Fri, Jul 06, 2012 at 08:12:57PM -0700, Joshua Maurice wrote:
> > On Jul 6, 3:54 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > > On Fri, Jul 06, 2012 at 07:39:56PM -0700, Joshua Maurice wrote:
> > > > I will say it again: the C89 and C99 standards do not mean what you
> > > > think. What the compiler writers and thread library writers understand
> > > > volatile to mean is not what you understand volatile to mean, and at
> > > > the end of the day what the compiler writers and thread library
> > > > writers think is paramount. You may be able to get this to work for
> > > > specific platforms with specific compilers. I suggest you ask in a
> > > > location specific to such implementations for such platform specific
> > > > hackery. However, do not look at the C standard for guidance on this.
>
> > > Um, what is it that compiler writers and thread library writers
> > > understand volatile to mean?
>
> >http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2016.html
>
> > > > In the C11 parlance, what you're trying to write requires the "weak"
> > > > atomics, such as relaxed, acquire, release, and consume memory
> > > > operations. Attempting to write stuff like this without C11 support
> > > > with the C11 primitives is a wholly platform specific affair, and
> > > > neither C89 nor C90 can help you there. volatile specifically cannot
> > > > help you (barring platform specific documentation to the contrary).
>
> > > You seem to be making too many assumptions about my code.
>
> > I think it's pretty clear from what you've written else-thread.
>
> > > > From else-thread:
> > > > On Jul 6, 2:29 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > > > > On Sat, Jul 07, 2012 at 12:37:59PM +1200, Ian Collins wrote:
> > > > > > Then use your platform's native threading primitives.
>
> > > > > Doing so would incur a performance penalty in comparison to my
> > > > > synchronization primitives in some circumstances.
>
> > > > That's your problem. You have broken code. Your options are:
> > > > 1- broken code (e.g. using volatile and asm memory barriers absent
> > > > platform specific guarantees)
> > > > 2- code that relies on platform specific guarantees (e.g. with using
> > > > volatile and asm memory barriers)
> > > > 3- "slow" code (e.g. with posix mutexes)
> > > > 4- C11 code
>
> > > How do you know, a priori, that my code is broken?  Do I have to
> > > change my passwords again?
>
> > How do I know it's broken? By your own admission, you're using
> > volatile to try and affect inter-thread visibility (absent constructs
> > that actually give inter-thread visibility). By your previous
> > statements about using the C standard to derive useful threading
> > semantics for volatile, I doubt you've looked at the compiler
> > documentation and hardware documentation for your implementation. Thus
> > you have broken code. (I don't think you are trying to nit on "it
> > could be right by coincidence".)
>
> Well you know what?  You're going to have to wait until version 1.0
> before you get to decide how wrong it is.

Let me share with you a story. I currently work for a company selling
Enterprise software. The software is heavily intertwined with
databases. We have a license for a large piece of code that does heavy
duty sorting. We have a contract with the guys who wrote this sorting
library for support and maintenance. One of our customers upgraded
some of their hardware, ran some tests - or possibly worse ran actual
usage - and noticed that one row out of a few billion was "missing".
Our engine dropped it, somewhere. It should be in the output, but it
wasn't. Of course, this is very bad for our company. This could cost
millions of dollars in sales. So, we get the guys who wrote the
sorting library, and immediately fly them out to us. We get them to
work on the code. I was only tangentially related to this process. It
took a week or so of work to find and fix the issue. What was the
problem? The guys who wrote the sorting library used "volatile", and
the customer just upgraded to some itanium processor or something. A
quick fix/hack later, and our customer was back in business, our
millions of dollars was secure, as was our company image and
reputation.

Use volatile at your own risk. I don't plan on using your software if
I can help it.

[toc] | [prev] | [next] | [standalone]


#23137 — Re: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually

FromUncle Steve <stevet810@gmail.com>
Date2012-07-06 19:47 -0400
SubjectRe: Clarification on the applicability of compile-time optimizationo to a struct variable which encompasses a mixture of fields declared as volatile and non-volatile individually
Message-ID<d87c7549c660812-506c2@gmail.com>
In reply to#23136
On Fri, Jul 06, 2012 at 08:33:45PM -0700, Joshua Maurice wrote:
> On Jul 6, 4:25 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > On Fri, Jul 06, 2012 at 08:12:57PM -0700, Joshua Maurice wrote:
> > > On Jul 6, 3:54 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > > > On Fri, Jul 06, 2012 at 07:39:56PM -0700, Joshua Maurice wrote:
> > > > > I will say it again: the C89 and C99 standards do not mean what you
> > > > > think. What the compiler writers and thread library writers understand
> > > > > volatile to mean is not what you understand volatile to mean, and at
> > > > > the end of the day what the compiler writers and thread library
> > > > > writers think is paramount. You may be able to get this to work for
> > > > > specific platforms with specific compilers. I suggest you ask in a
> > > > > location specific to such implementations for such platform specific
> > > > > hackery. However, do not look at the C standard for guidance on this.
> >
> > > > Um, what is it that compiler writers and thread library writers
> > > > understand volatile to mean?
> >
> > >http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2016.html
> >
> > > > > In the C11 parlance, what you're trying to write requires the "weak"
> > > > > atomics, such as relaxed, acquire, release, and consume memory
> > > > > operations. Attempting to write stuff like this without C11 support
> > > > > with the C11 primitives is a wholly platform specific affair, and
> > > > > neither C89 nor C90 can help you there. volatile specifically cannot
> > > > > help you (barring platform specific documentation to the contrary).
> >
> > > > You seem to be making too many assumptions about my code.
> >
> > > I think it's pretty clear from what you've written else-thread.
> >
> > > > > From else-thread:
> > > > > On Jul 6, 2:29 pm, Uncle Steve <stevet...@gmail.com> wrote:
> > > > > > On Sat, Jul 07, 2012 at 12:37:59PM +1200, Ian Collins wrote:
> > > > > > > Then use your platform's native threading primitives.
> >
> > > > > > Doing so would incur a performance penalty in comparison to my
> > > > > > synchronization primitives in some circumstances.
> >
> > > > > That's your problem. You have broken code. Your options are:
> > > > > 1- broken code (e.g. using volatile and asm memory barriers absent
> > > > > platform specific guarantees)
> > > > > 2- code that relies on platform specific guarantees (e.g. with using
> > > > > volatile and asm memory barriers)
> > > > > 3- "slow" code (e.g. with posix mutexes)
> > > > > 4- C11 code
> >
> > > > How do you know, a priori, that my code is broken?  Do I have to
> > > > change my passwords again?
> >
> > > How do I know it's broken? By your own admission, you're using
> > > volatile to try and affect inter-thread visibility (absent constructs
> > > that actually give inter-thread visibility). By your previous
> > > statements about using the C standard to derive useful threading
> > > semantics for volatile, I doubt you've looked at the compiler
> > > documentation and hardware documentation for your implementation. Thus
> > > you have broken code. (I don't think you are trying to nit on "it
> > > could be right by coincidence".)
> >
> > Well you know what?  You're going to have to wait until version 1.0
> > before you get to decide how wrong it is.
> 
> Let me share with you a story. I currently work for a company selling
> Enterprise software. The software is heavily intertwined with
> databases. We have a license for a large piece of code that does heavy
> duty sorting. We have a contract with the guys who wrote this sorting
> library for support and maintenance. One of our customers upgraded
> some of their hardware, ran some tests - or possibly worse ran actual
> usage - and noticed that one row out of a few billion was "missing".
> Our engine dropped it, somewhere. It should be in the output, but it
> wasn't. Of course, this is very bad for our company. This could cost
> millions of dollars in sales. So, we get the guys who wrote the
> sorting library, and immediately fly them out to us. We get them to
> work on the code. I was only tangentially related to this process. It
> took a week or so of work to find and fix the issue. What was the
> problem? The guys who wrote the sorting library used "volatile", and
> the customer just upgraded to some itanium processor or something. A
> quick fix/hack later, and our customer was back in business, our
> millions of dollars was secure, as was our company image and
> reputation.
> 
> Use volatile at your own risk. I don't plan on using your software if
> I can help it.

Well I wouldn't want you to use any software that you didn't want to
run.


Regards,

Uncle Steve

-- 
The moon has never been closer.

[toc] | [prev] | [next] | [standalone]


#23152

FromJens Gustedt <jens.gustedt@loria.fr>
Date2012-07-07 15:47 +0200
Message-ID<4FF83DFA.3050305@loria.fr>
In reply to#23121
Am 07.07.2012 01:58, schrieb Joshua Maurice:

> To be topical, here's the answer for C11: volatile is useless as a
> portable threading construct. Do not use it. It's (maybe) useful in a
> portable way only for 3 obscure situations: MMIO, setjmp and longjmp
> stuff, and signal handling.

No, this is misleading. volatile alone is not sufficient to warrant data
consistency between threads, but is necessary. volatile and _Atomic are
complementary specifications that *both* are needed if you want to make
sure that a thread always uses the latest stored value of a variable. If
you have

static int _Atomic a = ATOMIC_VAR_INIT(23);
if (tester)
  for (;;) printf("current value is %d\n", a);

The compiler is allowed to change the loop into something equivalent to

 {
  int tmp = a; // do an atomic_load operation here
  for (;;) printf("current value is %d\n", tmp);
 }

and thus always print the same value.

static int _Atomic volatile a = ATOMIC_VAR_INIT(23);

would assure that each execution of printf would see an updated value.

> If you properly use C11 mutexes, there is
> absolutely no need for volatile.

C11 mutexes (as are POSIX mutexes) are much too heavy machinery where a
simple atomic would suffice. I think that atomics and the precise data
consistency model are the real novelty in C11 (and C++1). That is to
provide a possibility of communicating safely between different threads
(and with signal handlers) without relying on OS support (or almost).


Jens

[toc] | [prev] | [next] | [standalone]


#23159

FromUncle Steve <stevet810@gmail.com>
Date2012-07-07 06:58 -0400
Message-ID<1206163f87ab652-5f1c1@gmail.com>
In reply to#23152
On Sat, Jul 07, 2012 at 03:47:38PM +0200, Jens Gustedt wrote:
> Am 07.07.2012 01:58, schrieb Joshua Maurice:
> 
> > To be topical, here's the answer for C11: volatile is useless as a
> > portable threading construct. Do not use it. It's (maybe) useful in a
> > portable way only for 3 obscure situations: MMIO, setjmp and longjmp
> > stuff, and signal handling.
> 
> No, this is misleading. volatile alone is not sufficient to warrant data
> consistency between threads, but is necessary. volatile and _Atomic are
> complementary specifications that *both* are needed if you want to make
> sure that a thread always uses the latest stored value of a variable. If
> you have
> 
> static int _Atomic a = ATOMIC_VAR_INIT(23);
> if (tester)
>   for (;;) printf("current value is %d\n", a);
> 
> The compiler is allowed to change the loop into something equivalent to
> 
>  {
>   int tmp = a; // do an atomic_load operation here
>   for (;;) printf("current value is %d\n", tmp);
>  }
> 
> and thus always print the same value.
> 
> static int _Atomic volatile a = ATOMIC_VAR_INIT(23);
> 
> would assure that each execution of printf would see an updated value.
> 
> > If you properly use C11 mutexes, there is
> > absolutely no need for volatile.
> 
> C11 mutexes (as are POSIX mutexes) are much too heavy machinery where a
> simple atomic would suffice. I think that atomics and the precise data
> consistency model are the real novelty in C11 (and C++1). That is to

That was my thinking.  The POSIX mutex api is really kind of awkward,
and on Linux the underlying implementation is more complicated than it
has to be.  Currently I'm developing on x86_64 and x86, and I chose to
use "lock incl %0" and then inspect the result to detect collisions.
It is obviously very fast, and I can control how long a thread will
spin on the lock before sleeping.  I have yet to investigate whether I
need to use a different instruction for the 64-bit arch.

A compiler supported atomic type would allow the removal of the
minimal inline asm that I'm using, but I can keep it to preserve some
backward compatibility with older systems.

> provide a possibility of communicating safely between different threads
> (and with signal handlers) without relying on OS support (or almost).

One potential application for non-mutex locking structures occurs in
some virtualized environments.  Under the Xen hypervisor, it is
possible to share virtual memory segments between guest domains.  The
facility is called 'grant pages', and looks a little like a SYSV
shared memory segment to the application.  With such a memory window,
a ring buffer (or equivalent algorithm) can allow inter-domain
communications without any system calls.  The same goes, of course,
for a ring-buffer instantiated in SYSV shared memory, but that is
limited to communications between processes on the same host.
Compared to TCP/IP IPC, this should greatly improve the available IPC
bandwidth among virtualized hosts on a multi-core platform.

I haven't yet got to the point of integrating support for Xen in any
of my code, but it's on my todo list.


Regards,

Uncle Steve

-- 
The moon has never been closer.

[toc] | [prev] | [next] | [standalone]


Page 2 of 3 — ← Prev page 1 [2] 3  Next page →

Back to top | Article view | comp.lang.c


csiph-web