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


Groups > comp.lang.c > #103830

Re: Memory not updating in thread as expected

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.c
Subject Re: Memory not updating in thread as expected
Date 2017-02-13 14:30 -0500
Organization A noiseless patient Spider
Message-ID <o7t1da$s9u$1@jstuckle.eternal-september.org> (permalink)
References <s023ac5gl1bpefvi63upl7tb6iunf0a8mo@4ax.com> <03324b12-a3d4-447a-86eb-f23065e59795@googlegroups.com> <eof3actvintefjsnqn1o6k5g18r5b4p5qr@4ax.com> <330c7242-aa78-485b-b64f-4fdfc6e2c294@googlegroups.com>

Show all headers | View raw


On 2/13/2017 1:30 PM, m.labanowicz@gmail.com wrote:
>> I have just one thread and one main program.
> 
> You have to think that you have two threads:
>   a) main_thread
>   b) user_thread (created by main_thread)
> 
> Following scenario as example:
> 
> A) main_thread INIT:
>    str1 = malloc(50);
>    if (str1 == NULL) { exit(EXIT_FAILURE); }
>    str2 = str1;
> 
> B) main_thread creates and starts user_thread
> 
> [main_thread]
>   1: str2[0]=0;
>   2: printf("%d\n",(int)str2[0]);
>   3: while (str2[0]==0)
>   4: {
>   5:   /* Imagine that this thread at this point will sleep for 5 seconds */
>   6:   /* We can simulate it with adding: */ sleep(5);
>   7:   printf("%d\n",(int)str2[0]); 
>   8:   sleep(1); 
>   9: } 
>  10: printf("Done %d\n",(int)str2[0]); 
> 
> [user_thread]
>   1: /* Imagine that this thread at this point will sleep for 2 seconds */
>   2: /* We can simulate it with adding: */ sleep(2);
>   3: str1[0]=50;
>   4: while (str1[0]==50)
>   5: {
>   6:   sleep(1);
>   7: }
>   8: /* Imagine that this thread at this point will sleep for 5 seconds */
>   9: /* We can simulate it with adding: */ sleep(5);
>  10: str1[0]=2;
> 
> Please analyze and tell what will be printed? :)
> 
> Note: You can imagine that such 'sleep(N) (where N is random value)' (added by OS thread scheduler) may be present at any place in the program.
> 

You are assuming the only time main() or the thread will give up control
is when they call sleep().  That is not true; the system can take
control from virtually any application at any time - even in the middle
of a statement such as "str2 = str1" or "str2[0] = 0".

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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