tag:blogger.com,1999:blog-2349434231090974738.post4589526416621832293..comments2023-10-24T19:15:52.177+01:00Comments on Advanced C++ with Examples: Another Multi-Threading example with MutexZahid Ghadialyhttp://www.blogger.com/profile/11618208626682295272noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-2349434231090974738.post-13529472471068315242012-02-24T22:16:56.965+00:002012-02-24T22:16:56.965+00:00See comment of Oct 22, 2010 09:06 AM.See comment of Oct 22, 2010 09:06 AM.Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-50715910080510218432012-02-24T20:59:39.842+00:002012-02-24T20:59:39.842+00:00Zahid, thanks for this interesting blog. But I tes...Zahid, thanks for this interesting blog. But I tested it in my computer(win7+core8+vs2008) and got the different result from yours, which was also pointed out by two anonymous readers. Could you tell me what configuration I did wrong ? thanks.<br /><br />Func1 loop 1<br />Func1 loop 2<br />Func1 loop 3<br />Func1 loop 4<br />Func1 loop 5<br />Func1 loop 6<br />Func1 loop 7<br />Func1 loop 8<br />Func1 loop 9<br />Func1 loop 10<br />Func2 loop 10<br />Func2 loop 9<br />Func2 loop 8<br />Func2 loop 7<br />Func2 loop 6<br />Func2 loop 5<br />Func2 loop 4<br />Func2 loop 3<br />Func2 loop 2<br />Func2 loop 1<br />Main exitZhibinhttps://www.blogger.com/profile/05268251331821951621noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-67776028823808202362011-09-13T17:05:25.908+01:002011-09-13T17:05:25.908+01:00Hey Zahid,
I see - you create the instance before...Hey Zahid,<br /><br />I see - you create the instance before everything starts multitasking so there is no contention. Great idea - thanks for the help and fast response :) <br /><br />Cheers,<br /><br />MichaelMichaelhttp://www2.pcdc.net/~vic5com/wp_site/noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-66309900629606504092011-09-13T16:32:25.920+01:002011-09-13T16:32:25.920+01:00Hi Michael, yes you can do that. Singleton's s...Hi Michael, yes you can do that. Singleton's should be created in the main function and should be created before any threads are created. I am sure not everyone strictly follows this so a Mutex would certainly help.Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-28992751881489235222011-09-13T16:01:22.487+01:002011-09-13T16:01:22.487+01:00Hi Zahid,
Should there be a mutex protecting
LOCK...Hi Zahid,<br /><br />Should there be a mutex protecting<br />LOCK()<br />if(!instance_)<br /> instance_ = new aSingletonClass; so that <br />UNLOCK()<br /><br />So that it is executed atomically? I feel if this is not done multiple threads may perform this 'new' command simultaneously creating multiple instances which may create a memory leak.<br /><br />Your Thoughts <br /><br />Cheers,Michaelhttp://www2.pcdc.net/~vic5com/wp_site/noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-31725861426340549802011-08-09T09:19:09.206+01:002011-08-09T09:19:09.206+01:00Raksha, if you remove Lock guard(mutex_); it will ...Raksha, if you remove Lock guard(mutex_); it will become asynchronous. By default the threads are asynchronous but you can use approaches like Mutex to make it synchronous.Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-20238349209651521532011-08-09T05:42:46.656+01:002011-08-09T05:42:46.656+01:00do we also have asynchronous multithreading in C++...do we also have asynchronous multithreading in C++ ? how to code synchronous and asynsynchronous multi threading in c++?Rakshahttps://www.blogger.com/profile/05571940970796148156noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-24063657174425837952011-05-11T13:11:30.881+01:002011-05-11T13:11:30.881+01:00Bharath, yes you can create multiple instances but...Bharath, yes you can create multiple instances but what is the point of using Singleton?Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-31836453641077441562011-05-09T19:42:19.906+01:002011-05-09T19:42:19.906+01:00Any Answers/suggestions for this Question?
If one...Any Answers/suggestions for this Question?<br /><br />If one uses a friend function in the singleton class declaration, then its possible to create multiple instances???bharathhttps://www.blogger.com/profile/01983539197960124091noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-38493440603616240262011-03-13T10:10:36.600+00:002011-03-13T10:10:36.600+00:00I guess this comment is based on the earlier comme...I guess this comment is based on the earlier comment. mutexAcquired should be set to false in Mutex constructor.Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-60276740277888436282011-03-13T07:10:19.412+00:002011-03-13T07:10:19.412+00:00What value would you assign mutexAcquired initiall...What value would you assign mutexAcquired initially? and where would you declare it?<br />Thanks!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-20306597593720532172010-10-22T17:30:42.666+01:002010-10-22T17:30:42.666+01:00Strange I didnt see this problem of multiple PC...Strange I didnt see this problem of multiple PC's.Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-36344233036508872242010-10-22T17:06:52.969+01:002010-10-22T17:06:52.969+01:00u need 1) change to
void Acquire ()
{
whil...u need 1) change to <br />void Acquire ()<br /> {<br /> while(mutexAcquired){Sleep(1);};<br /> EnterCriticalSection (& _critSection);<br /> mutexAcquired = true;<br /> }<br /> void Release ()<br /> {<br /> LeaveCriticalSection (& _critSection);<br /> mutexAcquired = false;<br /> }<br /><br />2) put Sleep(1) f.e. after<br /> aSingletonClass::getInstance()->printSomething(...<br />Without delay first thread completing processing before second one starts)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-25412414155502299632010-10-22T16:40:10.154+01:002010-10-22T16:40:10.154+01:00the same, doesnot work.the same, doesnot work.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-72640623294766879172010-07-30T22:11:10.777+01:002010-07-30T22:11:10.777+01:00I got the same problem, my output is this:
[code]...I got the same problem, my output is this:<br /><br />[code]<br />Func1 loop 1<br />Func1 loop 2<br />Func1 loop 3<br />Func1 loop 4<br />Func1 loop 5<br />Func1 loop 6<br />Func1 loop 7<br />Func1 loop 8<br />Func1 loop 9<br />Func1 loop 10<br />Func2 loop 10<br />Func2 loop 9<br />Func2 loop 8<br />Func2 loop 7<br />Func2 loop 6<br />Func2 loop 5<br />Func2 loop 4<br />Func2 loop 3<br />Func2 loop 2<br />Func2 loop 1<br />Main exit<br />[/code]<br /><br />Compiled with VS2008, Vista 64 bit, Intel Quad Q9660Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-49910140374410216432010-06-07T17:56:01.845+01:002010-06-07T17:56:01.845+01:00Please copy and paste your output in comments and ...Please copy and paste your output in comments and then we can see what you are getting.Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-36900412155006886402010-06-07T16:08:38.615+01:002010-06-07T16:08:38.615+01:00it doesn't synchronized in my computer (window...it doesn't synchronized in my computer (windows 7 with Intel core 2)DQhttps://www.blogger.com/profile/17836129575301057226noreply@blogger.com