Re: About the volatile and MESI Protocal

Alex Otenko via Concurrency-interest <[email protected]> Thu, 17 Feb 2022 11:45:05 +0000
Newsgroups gmane.comp.java.jsr.166-concurrency
Message-ID <CANkgWKik3SRLbcjOa-_pX4kpGr_VQrLbGnZkykBvD+8O_=DHhg@mail.gmail.com>
--===============4282262450932439897==
Content-Type: multipart/alternative; boundary="000000000000c7607b05d8354c40"

--000000000000c7607b05d8354c40
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

It's not clear where you reckon MESI is at play here. Rather, MESI acts in
such a way that you don't notice functional difference with direct access
to memory.

Engineers forget that volatile is not only about memory barriers, it is
also about compiler barriers. In this particular example the changes done
by the second thread are not necessarily visible to the first thread, sleep
or no sleep. This has nothing to do with MESI cache coherence prorocol.

Alex

On Thu, 17 Feb 2022, 11:31 =E9=A9=AC=E9=87=8C=E5=A5=A5 via Concurrency-inte=
rest, <
[email protected]> wrote:

> hello concurrency-interest:
>
>        My quest about the volatile,According to the MESI Protocol "bChang=
ed" in second thread modified in STOREBUFFER,then the first thread can not =
get the newest
>
>        "bChanged",why add the sleep method ,this thread can get the newes=
t bChanged?
>
>
>         public class NoVolatile {
> 	//no volatile
> 	private static boolean bChanged;
>
> 	public static void main(String[] args) throws InterruptedException {
> 		new Thread(() -> {
> 			for (; ; ) {
> 				if (bChanged =3D=3D !bChanged) {
> 					System.out.println("!=3D");
> 					break;
> 				}
> 			=09
> 				//why add the sleep method ,this thread can get the newest bChanged?
> //				try {
> //					Thread.sleep(1);
> //				} catch (InterruptedException e) {
> //					e.printStackTrace();
> //				}
> 			}
> 			System.exit(0);
> 		}).start();
>
> 		Thread.sleep(1000);
> 	=09
> 		new Thread(() -> {
> 			for (; ; ) {
> 				bChanged =3D !bChanged;
> 			}
> 		}).start();
> 	}
> }
>
> _______________________________________________
> Concurrency-interest mailing list
> [email protected]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>

--000000000000c7607b05d8354c40
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto">It&#39;s not clear where you reckon MESI is at play here.=
 Rather, MESI acts in such a way that you don&#39;t notice functional diffe=
rence with direct access to memory.<div dir=3D"auto"><br></div><div dir=3D"=
auto">Engineers forget that volatile is not only about memory barriers, it =
is also about compiler barriers. In this particular example the changes don=
e by the second thread are not necessarily visible to the first thread, sle=
ep or no sleep. This has nothing to do with MESI cache coherence prorocol.<=
/div><div dir=3D"auto"><br></div><div dir=3D"auto">Alex</div></div><br><div=
 class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Thu, 17 Feb=
 2022, 11:31 =E9=A9=AC=E9=87=8C=E5=A5=A5 via Concurrency-interest, &lt;<a h=
ref=3D"mailto:[email protected]">[email protected]=
swego.edu</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><pre>hel=
lo=C2=A0concurrency-interest:</pre><pre>       My quest about the volatile,=
According to the MESI Protocol<span style=3D"font-family:Verdana"> &quot;</=
span>bChanged&quot; in second thread modified in STOREBUFFER,then the first=
 thread can not get the newest</pre><pre>       &quot;bChanged&quot;,why ad=
d the sleep method ,this thread can get the newest bChanged?</pre><pre><br>=
</pre><pre>        public class NoVolatile {
	//no volatile
	private static boolean bChanged;

	public static void main(String[] args) throws InterruptedException {
		new Thread(() -&gt; {
			for (; ; ) {
				if (bChanged =3D=3D !bChanged) {
					System.out.println(&quot;!=3D&quot;);
					break;
				}
			=09
				//why add the sleep method ,this thread can get the newest bChanged?
//				try {
//					Thread.sleep(1);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
			}
			System.exit(0);
		}).start();

		Thread.sleep(1000);
	=09
		new Thread(() -&gt; {
			for (; ; ) {
				bChanged =3D !bChanged;
			}
		}).start();
	}
}<br></pre>_______________________________________________<br>
Concurrency-interest mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank" rel=
=3D"noreferrer">[email protected]</a><br>
<a href=3D"http://cs.oswego.edu/mailman/listinfo/concurrency-interest" rel=
=3D"noreferrer noreferrer" target=3D"_blank">http://cs.oswego.edu/mailman/l=
istinfo/concurrency-interest</a><br>
</blockquote></div>

--000000000000c7607b05d8354c40--

--===============4282262450932439897==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Concurrency-interest mailing list
[email protected]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest

--===============4282262450932439897==--