[Mono-list] Microsoft Mutex example doesn't work

Sergio Blanco Cuaresma sergio-blanco@iespana.es
28 Aug 2003 18:56:28 +0200


--=-Yy/VVaBvtEu7vrX1UlWb
Content-Type: multipart/mixed; boundary="=-Iqb5GTK37mFuvKn1OWWy"


--=-Iqb5GTK37mFuvKn1OWWy
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable


I've tested an example from:

http://msdn.microsoft.com/library/default.asp?url=3D/library/en-us/cpguide/=
html/cpconmutex.asp

And it doesn't work correctly with Mono. It uses 2 mutex no control 4
threads, lets see each one:

t1Start: Waits to finish until all mutex are released
t2Start: Waits until mutex 1 is released
t3Start: Waits until any mutex is released (1 or 2)
t4Start: Waits until mutex 2 is released

The program launch the 4 threads and Release mutex 1 and then mutex 2.
The output of the program shows:

MutexSample.cs ...
 - Main Owns gM1 and gM2
t1Start started,  Mutex.WaitAll(Mutex[])
t2Start started,  gM1.WaitOne( )
t3Start started,  Mutex.WaitAny(Mutex[])
t4Start started,  gM2.WaitOne( )
 - Main releases gM1
t2Start finished, gM1.WaitOne( )
 - Main releases gM2
t4Start finished, gM2.WaitOne( )

And the program doesn't finish. As we can see only the threads t2 and t4
have finished, so the calls WaitAll and WaitAny of thread t1 and t3
don't work properly.

The code is attached. Is it a bug?

--=20

[aka Marble]
 Registered LiNUX user #140941  <>  http://counter.li.org/
 Socio #3274 de HispaLinux  <>  http://www.hispalinux.es
 Miembro de GPL URV  <>  http://www.gplurv.org
 GnuPG key: 0x0ED2CF9D  <>  pgp.escomposlinux.org

--=-Iqb5GTK37mFuvKn1OWWy
Content-Disposition: attachment; filename=ThreadMutexMS.cs
Content-Transfer-Encoding: base64
Content-Type: text/plain; name=ThreadMutexMS.cs; charset=iso-8859-15

dXNpbmcgU3lzdGVtOw0KdXNpbmcgU3lzdGVtLlRocmVhZGluZzsNCg0KcHVibGljIGNsYXNzIE11
dGV4U2FtcGxlew0KDQogICBzdGF0aWMgTXV0ZXggICAgICAgICAgICBnTTE7DQogICBzdGF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gZXZzWzFdID0gRXZlbnQyOw0KICAgICAgICAgICAgLy8gRXZlbnQgZm9y
IHQyLg0KICAgICAgZXZzWzJdID0gRXZlbnQzOw0KICAgICAgICAgICAgLy8gRXZlbnQgZm9yIHQz
Lg0KICAgICAgZXZzWzNdID0gRXZlbnQ0Ow0KICAgICAgICAgICAgLy8gRXZlbnQgZm9yIHQ0Lg0K
DQogICAgICBNdXRleFNhbXBsZSAgICAgICAgIHRtICA9IG5ldyBNdXRleFNhbXBsZSggKTsNCiAg
ICAgIFRocmVhZCAgICAgICAgICAgICAgdDEgID0gbmV3IFRocmVhZChuZXcgVGhyZWFkU3RhcnQo
dG0udDFTdGFydCkpOw0KICAgICAgVGhyZWFkICAgICAgICAgICAgICB0MiAgPSBuZXcgVGhyZWFk
KG5ldyBUaHJlYWRTdGFydCh0bS50MlN0YXJ0KSk7DQogICAgICBUaHJlYWQgICAgICAgICAgICAg
IHQzICA9IG5ldyBUaHJlYWQobmV3IFRocmVhZFN0YXJ0KHRtLnQzU3RhcnQpKTsNCiAgICAgIFRo
cmVhZCAgICAgICAgICAgICAgdDQgID0gbmV3IFRocmVhZChuZXcgVGhyZWFkU3RhcnQodG0udDRT
dGFydCkpOw0KICAgICAgdDEuU3RhcnQoKTsNCiAgICAgICAgICAgICAgICAvLyBDYWxscyBNdXRl
eC5XYWl0QWxsKE11dGV4W10gb2YgZ00xIGFuZCBnTTIpLg0KICAgICAgdDIuU3RhcnQoKTsNCiAg
ICAgICAgICAgICAgICAvLyBDYWxscyBNdXRleC5XYWl0T25lKE11dGV4IGdNMSkuDQogICAgICB0
My5TdGFydCgpOw0KICAgICAgICAgICAgICAgIC8vIENhbGxzIE11dGV4LldhaXRBbnkoTXV0ZXhb
XSBvZiBnTTEgYW5kIGdNMikuDQogICAgICB0NC5TdGFydCgpOw0KICAgICAgICAgICAgICAgIC8v
IENhbGxzIE11dGV4LldhaXRPbmUoTXV0ZXggZ00yKS4NCg0KICAgICAgVGhyZWFkLlNsZWVwK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gTXV0ZXggb2JqZWN0IGlzIHJlbGVh
c2VkLg0KICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoInQzU3RhcnQgZmluaXNoZWQsIE11dGV4Lldh
aXRBbnkoTXV0ZXhbXSkiKTsNCiAgICAgIEV2ZW50My5TZXQoICk7DQogICAgICAgICAgICAgICAv
LyBBdXRvUmVzZXRFdmVudC5TZXQoICkgZmxhZ2dpbmcgbWV0aG9kIGlzIGRvbmUuDQogICB9DQoN
CiAgIHB1YmxpYyB2b2lkIHQ0U3RhcnQoKXsNCiAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJ0NFN0
YXJ0IHN0YXJ0ZWQsICBnTTIuV2FpdE9uZSggKSIpOw0KICAgICAgZ00yLldhaXRPbmUoICk7DQog
ICAgICAgICAgICAvLyBXYWl0cyB1bnRpbCBNdXRleCBnTTIgaXMgcmVsZWFzZWQuDQogICAgICBD
b25zb2xlLldyaXRlTGluZSgidDRTdGFydCBmaW5pc2hlZCwgZ00yLldhaXRPbmUoICkiKTsNCiAg
ICAgIEV2ZW50NC5TZXQoICk7DQogICAgICAgICAgICAvLyBBdXRvUmVzZXRFdmVudC5TZXQoICkg
ZmxhZ2dpbmcgbWV0aG9kIGlzIGRvbmUuDQogICB9DQp9DQo=

--=-Iqb5GTK37mFuvKn1OWWy--

--=-Yy/VVaBvtEu7vrX1UlWb
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: Esta parte del mensaje =?ISO-8859-1?Q?est=E1?= firmada
	digitalmente

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQA/TjQ7fGXf5g7Sz50RAscLAJ9XmbPg0N3jarNkyjQIMsgewJNPRgCg6dcN
W9FBAjMnH2vFWi4yx1fxkPQ=
=R//R
-----END PGP SIGNATURE-----

--=-Yy/VVaBvtEu7vrX1UlWb--