Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #103844
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Memory not updating in thread as expected |
| Date | 2017-02-13 23:15 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <o7tb2c$40o$1@dont-email.me> (permalink) |
| References | <s023ac5gl1bpefvi63upl7tb6iunf0a8mo@4ax.com> <d52fe51a-1f8d-47fe-9283-5f1c13f5a05d@googlegroups.com> <ggg3acdd9s4h3p47f4488mns89opglousv@4ax.com> |
On 13/02/17 15:36, Test wrote:
> m.labanowicz@gmail.com wrote:
>
>> I assume that there is another case with optimizer (it depends on declaration):
>>
>>> // str2 has the address of str1 in the thread, ie. (long)str1==(long)str2
>>> str2[0]=0; // signal thread that it should exit the loop
>>> printf("%d\n",(int)str2[0]); // "0" is shown
>>> // {int i11;for (i11=0;i11<8;i11++) printf("[%.2X]",str2[i11]);printf("\n");}
>>> while (str2[0]==0)
>>> { // waiting for the thread to signal it's about to finish
>>> printf("%d\n",(int)str2[0]); // "50" is shown WTF??
>>> sleep(1);
>>> }
>>> printf("Done %d\n",(int)str2[0]); // "50" is shown WTF??
>>
>> Loop 'while (str2[0]==0)' can be replaced by optimizer to 'while (1)'.
>>
>> Solution for this is to declare str2 with 'volatile' keyword:
>> volatile char * str2;
>>
>> The same problem is with loop in 'thread function'.
>>
>> Cheers
>
>
> After a couple quick test runs the volative keyword appears to fix the issue. I
> appears that the compiler (GCC) optimization was the culprit ..
>
No, gcc optimisation was /not/ the culprit - incorrect code was the
culprit. If you start thinking that the optimisation is the problem and
that using "volatile" will "disable optimisation", you are going to get
your code badly wrong. Adding "volatile" can make things appear to
work, and perhaps work during your simple testing, but "volatile" alone
is far from sufficient to make multi-threaded synchronisation work
correctly. You might end up with code that works while you try it, but
fails in other circumstances, and you'll have no idea why.
You are programming on Linux here. Linux supports a wide array of
mutexes, semaphores, and other synchronisation systems that /work/. You
will have to learn to use them, and not try to invent your own with a
bit of "volatile" here and there - you /will/ get this wrong.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Memory not updating in thread as expected Test <test@.nil.invalid.com> - 2017-02-13 12:46 +0200
Re: Memory not updating in thread as expected m.labanowicz@gmail.com - 2017-02-13 05:18 -0800
Re: Memory not updating in thread as expected Test <test@.nil.invalid.com> - 2017-02-13 16:25 +0200
Re: Memory not updating in thread as expected m.labanowicz@gmail.com - 2017-02-13 10:30 -0800
Re: Memory not updating in thread as expected Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-13 14:30 -0500
Re: Memory not updating in thread as expected m.labanowicz@gmail.com - 2017-02-13 05:46 -0800
Re: Memory not updating in thread as expected Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 14:30 +0000
Re: Memory not updating in thread as expected "Chris M. Thomasson" <invalid@invalid.invalid> - 2017-02-13 15:13 -0800
Re: Memory not updating in thread as expected Test <test@.nil.invalid.com> - 2017-02-13 16:36 +0200
Re: Memory not updating in thread as expected David Brown <david.brown@hesbynett.no> - 2017-02-13 23:15 +0100
Re: Memory not updating in thread as expected James Kuyper <jameskuyper@verizon.net> - 2017-02-13 22:04 -0500
Re: Memory not updating in thread as expected David Brown <david.brown@hesbynett.no> - 2017-02-14 09:44 +0100
Re: Memory not updating in thread as expected Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-14 11:07 +0000
Re: Memory not updating in thread as expected David Brown <david.brown@hesbynett.no> - 2017-02-14 12:46 +0100
Re: Memory not updating in thread as expected Robert Wessel <robertwessel2@yahoo.com> - 2017-02-16 05:12 -0600
Re: Memory not updating in thread as expected David Brown <david.brown@hesbynett.no> - 2017-02-16 12:44 +0100
Re: Memory not updating in thread as expected Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2017-02-14 15:53 +0100
Re: Memory not updating in thread as expected Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-14 15:36 +0000
Re: Memory not updating in thread as expected Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2017-02-14 17:58 +0100
Re: Memory not updating in thread as expected Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-14 17:26 +0000
Re: Memory not updating in thread as expected Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2017-02-14 10:43 +0100
Re: Memory not updating in thread as expected Robert Wessel <robertwessel2@yahoo.com> - 2017-02-14 03:52 -0600
Re: Memory not updating in thread as expected Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2017-02-14 11:36 +0100
Re: Memory not updating in thread as expected supercat@casperkitty.com - 2017-02-14 05:31 -0800
Re: Memory not updating in thread as expected Ken Brody <kenbrody@spamcop.net> - 2017-02-13 12:44 -0500
Re: Memory not updating in thread as expected Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-13 10:58 -0700
Re: Memory not updating in thread as expected "Chris M. Thomasson" <invalid@invalid.invalid> - 2017-02-14 15:13 -0800
Re: Memory not updating in thread as expected "Chris M. Thomasson" <invalid@invalid.invalid> - 2017-02-14 16:37 -0800
csiph-web