Single Threaded Apartment 정리되지 않은 기술

Single Thread Apartment(STA) 는 Visual BASIC 에서 ActiveX DLL을 개발하거나 AX EXE 를 개발하면 default 로 갖게 되는Thread Model 입니다. 또한, Multiple Thread Apartment(MTA)로 구성 될 수 없습니다. 오히려 MTA 는 이해하기가 쉬운데, STA가 좀처럼 이해 하기가 쉽지 않은 모델이라 몇 가지 정리해볼까 합니다.
 

Thread

Thread 는 실행 최소 단위입니다. Application 관점에서 main () 함수에서 나열된 구문을 모두 해석하여 시스템에서 수행이 되면, 그냥 하나의 Thread가 수행된 겁니다. 만일, 나열된 구문 중에Thread를Fork 하는CreateThread API가 호출된다면, 기존의Thread는 그냥 흘러가고 동시에 또 다른Thread 가 만들어져서 걔도 걔 나름대로 흘러가게 됩니다.

Apartment

Thread가 실행되다가 필요에 의해서 개발된AX(또는COM)에서 제공되는 Object를 호출하고 수행할 필요가 있을 겁니다. 이 때 이와 같은Object 는 임의의Apartment 에 위치하고 그 위치를Thread가 접근하게 됩니다. 이Apartment 는 일종의 보호 장벽일 수 있고, 뭔가 끼리끼리 모이기 위한 공간이라고 생각할 수도 있을 겁니다. 하여튼, CoInitialize()/CoInitializeEx() 함수는Apartment 를 만들고, CoUninitialize() 는 해당Apartment 를 해제합니다. 그 안에서 COM 이 존재합니다. VB COM 은 Single Thread Apartment 라는 곳에 위치합니다. 해당Apartment 에서VB COM 을 접근하는Thread는 단지1개만 존재해야 합니다. 그래서Single Thread Apartment 라는 용어가 됩니다.

Thread affinity

Single Thread Apartment 안에서 1개 이상의Thread가 동일한 또는 다른Object 를 이용(Object Context의 생성이 되겠죠.)하려고 한다면 STA 안에서는Message Queue 를 이용하게 합니다. Thread의 요청이Queue 안에 순차적으로 쌓이고 Queue out 하면서 한번에 하나씩 처리를 하면서 동기화 합니다. 물론, MTA는 이와 같은 구조가 아니므로, 동기화에 해당하는 강구(WaitForSingleObject/WaitForMultipleObjejcts API 이용)가 필요합니다만, STA는 내부적으로 이런 동기화 지원을 합니다. 그러므로, Thread affinity 라는 용어도 사용합니다. 일반적으로 동기화가 되기 때문에Thread Local Storage 와 같은 영역이 보장이 됩니다.

Causality

조금은 다른 말이 될 수 있는 데, DCOM 구조가 있습니다. 필요에 의해서 다른Process에서 수행되고 있는Object 를 이용하는 경우도 있습니다. DLLHOST.exe에서 surrogate 되고 있는COM의 경우가 대표적인 상황입니다. 이 경우에는 다른Process안의STA 에Object가 존재하고 있지만, RPC Channel 을 통해서 해당Object를 이용할 수 있습니다. 만일, A 라는Process안의 임의의 Thread가 A process안의STA 에 있는Object a 를 호출하고, 해당Object a 는 RPC Channel을 통해 B 라는Process안의 임의의 Thread로 하여금 MTA 혹은STA 안의 b 라는Object를 이용하는 구조를 생각해 봅시다. 이런 경우A Process의Thread와B process의 Thread는 당연히 서로 다른 구별된Thread입니다. 하지만, Logical 하게는 하나의Thread 로 , 하나의 수행흐름으로 보입니다. 하여 이와 같은 논리적인Thread를 Causality라고 합니다. 그래서 이들은 하나의 CID 값을 내부적으로 갖습니다. 보통 MSDTC의Transaction 처리는 하나의CID로 길게 수행이 됩니다.

*Reentrancy

Causality 예에서는 다른 Process의Apartment에 접근하는Cross apartment형태입니다. Cross apartment 의 경우RPC Channel을 통해서Call 한 후Callback 을Asynchronous 하게Return 받아야 하는 구조입니다. MTA Model 에서는 Apartment에 다른 여러 개의 Thread가 존재하기 때문에 임의의Thread가Callback을return 받지만, STA Model 은 그것이 불가능합니다. 하여, 이러한 Cross Apartment Call을 위해서 마찬가지로Message Loop 를 이용합니다. message Queue에 던져놓고Callback 을 기다리는 겁니다. 그러므로, Cross Apartment Call에 대한 Callback은 일단Blocking이 되고 해당Apartment는 다른Thread의 요청을 받을 수 있습니다. 이 경우에Reentrancy 가 발생합니다. 또 다른Thread 또는Causality가 해당Apartment에 진입할 수 있습니다. 기존의Cross Apartment Call에 대한Causality 는 종료되지 않은 상태로 또 다른Causality 가 진행됩니다. 이 경우 두 번째Causality가STA를 소유하고 있으므로, Cross Apartment Callback은 두 번째Causality 가 완료될 때 까지는 처리되지 못합니다. 물론, 두 번째Causality가 또 다시Cross Apartment Call이 될 수 도 있고 또 다른 세 번째, 네 번째Causality 가 들어오게 되어Dead Lock 과 같은 현상이 발생할 수도 있습니다. 물론, 두 번째Causality가 종료되고 첫 번째Callback이 이어서 종료되는 것이 정상적인 동작이 될 수도 있습니다.


이와 같은STA Reentrancy 현상은TLS 에도 영향을 미칩니다. 특히, VB Application의 경우는TLS에Global Variable이나, App, Err 내부적인Objects를 저장하고 있습니다. 이와 같은Reentrancy 는TLS가share되는 현상이 발생할 수 있기 때문에TLS Lock( *Activity Lock의 형태*)이나, Multiple Thread 환경에서 사용되는VB Application은Global Variable 을 사용하지 말라는Guide (http://support.microsoft.com/?id=815053) 를 하고 있습니다.


*Activity Lock 은 COM+ 에서 제공하는 Serialization을 이용한다고 보시면 됩니다.
*COM+ Catalog에서 STA Pool에 존재하는Thread 수를Monitoring 하다 보면, Reentrancy 로 인하여Max Pool 수보다 많은 수가 나오는 것도 경험할 수 있습니다.

STA는 Thread의 동기화를 걱정 할 필요가 없기때문에 편리하긴 하지만, Message Queue 의구조나 Message Queue를 사용하면서 발생하는 Thread switch, 그리고, Reentrancy에 의한 Dead Lock 이 발생할 수 도 있음을 고려해야 합니다. 그러므로, Multiple Threads 환경인 COM+ 나 IIS, 그리고, IE에서 VB AX(STA 기반의Component)를 사용 하는것은 많은 Issue를 유발 할 수 밖에 없을 것 같습니다. ( 사실VB 로만든 Component가 아니라면 더이상 STA는 보기 어려울 듯 합니다만.. 이제 그수명이 한 10개월정도 남은 것 같습니다.. )


덧글

  • cia_lis 2016/07/10 14:01 # 삭제 답글

    Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks
  • cia_lis 2016/07/10 14:02 # 삭제 답글

    Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks
  • generic 2016/07/10 14:02 # 삭제 답글

    Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks
  • purchase_via_gra 2016/07/10 14:02 # 삭제 답글

    Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks
  • fast 2016/07/10 14:03 # 삭제 답글

    Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks
  • cia_lis 2016/07/12 00:54 # 삭제 답글

    Appreciating the time and energy you put into your blog and in depth information you provide. It's good to come across a blog every once in a while that isn't the same unwanted rehashed information. Excellent read! I've saved your site and I'm adding your RSS feeds to my Google account.
  • via_gra 2016/07/12 00:55 # 삭제 답글

    Appreciating the time and energy you put into your blog and in depth information you provide. It's good to come across a blog every once in a while that isn't the same unwanted rehashed information. Excellent read! I've saved your site and I'm adding your RSS feeds to my Google account.
  • cia_lis 2016/07/12 00:55 # 삭제 답글

    Appreciating the time and energy you put into your blog and in depth information you provide. It's good to come across a blog every once in a while that isn't the same unwanted rehashed information. Excellent read! I've saved your site and I'm adding your RSS feeds to my Google account.
  • via_gra 2016/07/12 00:56 # 삭제 답글

    Appreciating the time and energy you put into your blog and in depth information you provide. It's good to come across a blog every once in a while that isn't the same unwanted rehashed information. Excellent read! I've saved your site and I'm adding your RSS feeds to my Google account.
  • cia_lis 2016/07/12 18:17 # 삭제 답글

    I just like the helpful information you provide on your articles. I will bookmark your blog and test once more here frequently. I'm moderately certain I will learn many new stuff right here! Best of luck for the next!
  • buy_via_gra 2016/07/13 17:15 # 삭제 답글

    I loved your blog article. Really Cool.
  • purchase_cia_lis 2016/07/13 17:15 # 삭제 답글

    I loved your blog article. Really Cool.
  • online 2016/07/13 17:16 # 삭제 답글

    I loved your blog article. Really Cool.
  • purchase_cia_lis 2016/07/13 17:16 # 삭제 답글

    I loved your blog article. Really Cool.
  • purchase_via_gra 2016/07/13 17:17 # 삭제 답글

    I loved your blog article. Really Cool.
  • cia_lis_pills 2016/07/16 01:31 # 삭제 답글

    Hello, i think that i saw you visited my site so i came to return the favor.I am trying to find things to enhance my web site!I suppose its ok to use some of your ideas!!
  • via_gra 2016/07/16 01:32 # 삭제 답글

    Hello, i think that i saw you visited my site so i came to return the favor.I am trying to find things to enhance my web site!I suppose its ok to use some of your ideas!!
  • cia_lis 2016/07/16 01:32 # 삭제 답글

    Hello, i think that i saw you visited my site so i came to return the favor.I am trying to find things to enhance my web site!I suppose its ok to use some of your ideas!!
  • via_gra 2016/07/16 01:33 # 삭제 답글

    Hello, i think that i saw you visited my site so i came to return the favor.I am trying to find things to enhance my web site!I suppose its ok to use some of your ideas!!
  • cia_lis_india 2016/07/20 21:25 # 삭제 답글

    Mudbox is a software for 3D sculpting and painting which is developed by Autodesk.
  • india 2016/07/20 21:25 # 삭제 답글

    Mudbox is a software for 3D sculpting and painting which is developed by Autodesk.
  • cia_lis 2016/07/20 21:26 # 삭제 답글

    Mudbox is a software for 3D sculpting and painting which is developed by Autodesk.
  • via_gra 2016/07/20 21:26 # 삭제 답글

    Mudbox is a software for 3D sculpting and painting which is developed by Autodesk.
  • cia_lis_price 2016/07/23 08:10 # 삭제 답글

    Hey there! Do you know if they make any plugins to
  • via_gra_price 2016/07/23 08:10 # 삭제 답글

    Hey there! Do you know if they make any plugins to
  • online 2017/03/14 17:21 # 삭제 답글

  • from 2017/03/24 07:13 # 삭제 답글

  • buy_viagra 2017/03/27 08:57 # 삭제 답글

  • generic 2017/03/28 20:51 # 삭제 답글

  • payday 2017/04/04 21:54 # 삭제 답글

  • 5mg 2017/04/05 16:07 # 삭제 답글

  • viagra_dosage 2017/04/07 23:34 # 삭제 답글

  • custom_writing 2017/04/09 17:19 # 삭제 답글

  • viagra,buy 2017/04/11 08:20 # 삭제 답글

    The /erectial-disfunction/ This should alone prescription for http://clia2online.com/ , mordellgardens.
  • once 2017/04/19 22:06 # 삭제 답글

  • super 2017/04/20 03:54 # 삭제 답글

  • cialis_online 2017/04/20 06:45 # 삭제 답글

  • viagra_generic 2017/04/24 01:56 # 삭제 답글

    Some really nice and useful information on this web site, also I conceive the style and design holds superb features.
  • male 2017/04/27 04:15 # 삭제 답글

    Woah! I'm really loving the templatetheme of this website. It's simple, yet effective. A lot of times it's hard to get that perfect balance between usability and visual appeal. I must say you have done a great job with this. In addition, the blog loads super quick for me on Firefox. Excellent Blog!
  • viagra_eu 2017/04/29 19:43 # 삭제 답글

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why throw away your intelligence on just posting videos to your site when you could be giving us something enlightening to read?
  • viagra_generic 2017/05/02 16:11 # 삭제 답글

    Thank you for some other informative website. Where else may just I am getting that kind of info written in such an ideal means? I've a venture that I am just now working on, and I have been on the glance out for such info.
  • George 2017/05/22 08:42 # 삭제 답글

    This will be a fantastic site, will you be interested in doing an interview regarding just how you designed it? If so email me!
  • jgftiiti 2017/05/24 22:11 # 삭제 답글

    breast enhancement pills online

    <a href=http://breastenlargementpillstzv.info/> breast enhancement pills permanent results</a>

    <a href=http://breastenlargementpillstzv.info/> breast enhancement pills permanent results</a>

    breast enlargement hormone pills
  • Joseph 2017/05/27 08:14 # 삭제 답글

    Actually its referred to as Search engine optimization that when i search for this post I found this web page at the top of all web pages in search engine.
  • John 2017/05/30 20:50 # 삭제 답글

    I appreciate, cause I found just what I was looking for. You have ended my 4 day long hunt! God Bless you man. Have a great day. Bye
  • Donald 2017/05/30 21:01 # 삭제 답글

    Hey very nice blog!! Man .. Excellent .. Amazing .. I will bookmark your site and take the feeds alsoI'm satisfied to find a lot of useful info right here within the post, we'd like develop extra strategies in this regard, thanks for sharing.
  • Robert 2017/06/04 20:08 # 삭제 답글

    Do you mind if I quote a few of your posts as long as I provide credit and sources back to your weblog? My blog is in the very same niche as yours and my users would truly benefit from a lot of the information you present here. Please let me know if this alright with you. Thank you!
  • David 2017/06/16 05:56 # 삭제 답글

    How To Buy Cheap Generic Ceclor 500mg
  • George 2017/06/18 18:07 # 삭제 답글

    Thank you for some other informative website. Where else may just I am getting that kind of info written in such an ideal means? I've a venture that I am just now working on, and I have been on the glance out for such info.
  • David 2017/06/22 13:46 # 삭제 답글

    coumadin valtrex interaction
  • George 2017/07/19 10:54 # 삭제 답글

    you are in point of fact a just right webmaster. The site loading velocity is amazing. It sort of feels that you are doing any distinctive trick. Furthermore, The contents are masterpiece. you've performed a magnificent process in this subject!
  • Joseph 2017/08/02 05:35 # 삭제 답글

    It's truly a great and useful piece of information. I'm satisfied that you shared this helpful information with us. Please stay us informed like this. Thank you for sharing.
  • Michael 2017/08/05 23:58 # 삭제 답글

    Great, thanks for sharing this article. Really Cool.
  • Michael 2017/08/09 00:22 # 삭제 답글

    My own wife and I fell over here by a poles apart trap forward also contemplated I'd personally restraint points unconscious. I like exactly what I envision i really am located absolutely charting you. Look forward to
  • Mark 2017/08/29 05:08 # 삭제 답글

    Hello! Someone in my Facebook group shared this website with us so I came to check it out. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers! Outstanding blog and superb design and style.
  • Robert 2017/09/07 16:40 # 삭제 답글

    Perfect webpage you have at this website! Precisely how could i add in this blog's feed into my Rss reader?
  • Robert 2017/09/28 00:49 # 삭제 답글

    Enjoyed examining this, very good stuff, thankyou. Talk sense to a fool and he calls you foolish. by Euripides.
  • Paul 2017/10/04 23:37 # 삭제 답글

    Whats Taking place i am new to this, I stumbled upon this I have found It positively helpful and it has aided me out loads. I hope to contribute &amp assist other users like its helped me. Great job.
  • George 2017/10/13 07:25 # 삭제 답글

    We're a group of volunteers and starting a new scheme in our community.
  • Mark 2017/10/16 02:17 # 삭제 답글

    This design is wicked! You obviously know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog well, almostHaHa! Wonderful job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!
  • Paul 2017/10/20 00:31 # 삭제 답글

    hello!,I like your writing very a lot! proportion we keep in touch more approximately your article on AOL? I require a specialist on this area to unravel my problem. May be that's you! Looking forward to look you.
  • Charles 2017/10/28 04:59 # 삭제 답글

    Hi there. Merely desired to question an instant dilemma.
  • James 2017/10/28 20:30 # 삭제 답글

    My brother recommended I may like this website. He used to be totally right. This post truly made my day. You can not consider simply how so much time I had spent for this info! Thanks!
  • James 2017/11/03 06:45 # 삭제 답글

    Today, I went to the beach front with my kids.
  • Charles 2017/11/16 14:39 # 삭제 답글

    Ive learn some excellent stuff here. Certainly worth bookmarking for revisiting. I wonder how a lot attempt you set to create such a fantastic informative web site.
  • Daniel 2017/11/29 10:19 # 삭제 답글

    nasa, , harold hill
  • Thomas 2017/12/01 05:21 # 삭제 답글

    Amoxicillin And Clavulanate 250mg With No Prescription in Indianapolis
  • Donald 2017/12/02 09:29 # 삭제 답글

    While think this kind of Real estate property industry hope against hope turn back in the positive course? Or maybe can it be at a standstill very young to tell? We're glimpse many terracing property foreclosures modish
  • Michael 2017/12/03 06:37 # 삭제 답글

    My spouse and I stumbled over here by a different internet address and thought I might check items out. I like what I see so i am just following you. Appear forward to checking out your web page again.
  • James 2017/12/05 01:01 # 삭제 답글

    Howdy very nice web site!! Guy .. Excellent .. Superb .. I'll bookmark your web site and take the feeds additionallyI am glad to search out so many helpful info here in the put up, we want develop more techniques in this regard, thanks for sharing.
  • David 2017/12/07 21:34 # 삭제 답글

    This website is mostly a walkby for all the info you wished about this and didnt know who to ask. Glimpse right here, and also youll undoubtedly uncover it.
  • Mark 2017/12/09 00:19 # 삭제 답글

    what are some superior and in demand websites for blogs? ?? .
  • Daniel 2017/12/12 04:28 # 삭제 답글

    Wow! This could be one particular of the most useful blogs We've ever arrive across on this subject. Basically Wonderful. I'm also a specialist in this topic so I can understand your hard work.
  • James 2017/12/15 03:22 # 삭제 답글

    I do not even know how I stopped up here, however I assumed this put up was good. I do not recognise who you are however definitely you're going to a wellknown blogger for those who are not already
  • Mark 2017/12/16 22:15 # 삭제 답글

    Some really nice and useful information on this web site, also I conceive the style and design holds superb features.
  • George 2017/12/20 06:12 # 삭제 답글

    Thanks for this article. I might also like to say that it can end up being hard if you find yourself in school and merely starting out to create a long credit rating. There are many learners who are only trying to endure and have long or good credit history are often a difficult issue to have.
  • Thomas 2017/12/26 13:01 # 삭제 답글

    I'm really enjoying the themedesign of your website. Do you ever run into any internet browser compatibility issues? A handful of my blog audience have complained about my site not operating correctly in Explorer but looks great in Firefox. Do you have any solutions to help fix this problem?
  • Donald 2017/12/28 10:38 # 삭제 답글

    Your goal is to breed all the different dragons available
  • James 2018/01/03 01:52 # 삭제 답글

    Magnificent website. Lots of useful information here. Im sending it to some friends ans also sharing in delicious. And obviously, thanks for your sweat!
  • William 2018/01/08 22:11 # 삭제 답글

    Hello, Neat post. There is a problem with your website in web explorer, may check this IE still is the market leader and a large component of folks will leave out your magnificent writing because of this problem.
  • James 2018/01/13 08:42 # 삭제 답글

    Some genuinely great information, Glad I discovered this. Good teaching is onefourth preparation and threefourths theater. by Gail.
  • John 2018/01/16 12:09 # 삭제 답글

    I have recently started a site, the info you provide on this website has helped me tremendously. Thanks for all of your time &amp work. The achievements of an organization are the results of the combined effort of each individual. by Vince Lombardi.
  • James 2018/01/20 09:22 # 삭제 답글

    Article Source a viral game app is not
  • William 2018/01/23 08:40 # 삭제 답글

    Because here is a list of multiplayer games is that the leave was asked
  • Robert 2018/01/31 10:41 # 삭제 답글

    I conceive this website has got some rattling superb info for everyone. Dealing with network executives is like being nibbled to death by ducks. by Eric Sevareid.
  • William 2018/02/01 22:18 # 삭제 답글

    you are in point of fact a just right webmaster. The site loading velocity is amazing. It sort of feels that you are doing any distinctive trick. Furthermore, The contents are masterpiece. you've performed a magnificent process in this subject!
  • Robert 2018/02/01 22:18 # 삭제 답글

    An interesting dialogue is value comment. I feel that it is best to write extra on this subject, it won't be a taboo subject but generally persons are not sufficient to speak on such topics. To the next. Cheers
  • Mark 2018/02/02 10:10 # 삭제 답글

    Hello there, i am interested
  • Richard 2018/02/07 07:22 # 삭제 답글

    Please add more movies related to cooking if you have, because I wish for to learn more and more about all recipes of cooking.
  • Paul 2018/02/24 11:17 # 삭제 답글

    Hmm it looks like your site ate my first comment it was extremely
  • Donald 2018/02/26 06:47 # 삭제 답글

    Really informative article post.Thanks Again. Awesome.
  • Mark 2018/03/31 04:15 # 삭제 답글

    Awesome article post.Thanks Again. Much obliged.
  • David 2018/04/24 12:16 # 삭제 답글

    I have read several good stuff here. Certainly worth bookmarking for revisiting. I surprise how much effort you put to make such a magnificent informative web site.
  • Mark 2018/05/05 01:28 # 삭제 답글

    I will immediately grab your rss feed as I can not find your email subscription link or enewsletter service. Do you have any? Kindly let me know so that I could subscribe. Thanks.
  • Paul 2018/06/02 17:13 # 삭제 답글

    A megapolis megabucks gratuit failed to seem to cause waste materials in addition
  • William 2018/06/05 19:31 # 삭제 답글

    Nice post. I study one thing more difficult on completely different blogs everyday.
  • Thomas 2018/06/13 01:51 # 삭제 답글

    When choosing the colours for you office, there are a few basic points to consider.
  • Donald 2018/06/13 17:52 # 삭제 답글

    I really appreciate this post. I've been looking all over for this! Thank goodness I found it on Bing. You've made my day! Thanks again!
  • George 2018/06/16 15:16 # 삭제 답글

    This kind of game gives a real experience of building a farm and planting
  • Michael 2018/06/18 17:45 # 삭제 답글

    I've read some excellent stuff here. Definitely price bookmarking for revisiting. I surprise how much effort you place to create this type of great informative site.
댓글 입력 영역
◀ 다음덧글    이전덧글 ▶