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개월정도 남은 것 같습니다.. )


덧글

  • Mark 2019/07/10 08:51 # 삭제 답글

    You produced some decent points there. I looked on the net to the problem and discovered many people goes together with along along with your web site.
  • William 2019/07/10 13:49 # 삭제 답글

    An attentiongrabbing dialogue is worth comment. I believe that you must write extra on this subject, it won't be a taboo topic but typically persons are not enough to speak on such topics. To the next. Cheers
  • Richard 2019/07/12 03:08 # 삭제 답글

    Enjoyed examining this, very good stuff, thankyou . While thou livest keep a good tongue in thy head. by William Shakespeare.
  • James 2019/07/12 14:18 # 삭제 답글

    Informative Site Hello guys here are some links that contains information which you might find useful yourselves. It is Worth Checking out.
  • Michael 2019/07/13 14:15 # 삭제 답글

    I'm really enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Excellent work!
  • Donald 2019/07/13 17:16 # 삭제 답글

    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
  • Daniel 2019/07/13 21:05 # 삭제 답글

    Good post. I study something more challenging on completely different blogs everyday. It is going to all the time be stimulating to learn content material from different writers and practice somewhat one thing from their store. I
  • Charles 2019/07/17 23:26 # 삭제 답글

    I know this if off topic but I'm looking into starting my own blog and was curious what all is required to get set up? I'm assuming having a blog like yours would cost a pretty penny? I'm not very web savvy so I'm not 100 positive. Any recommendations or advice would be greatly appreciated. Thanks
  • Richard 2019/07/18 04:12 # 삭제 답글

    obviously like your website however you need to check the spelling on several of your posts. Several of them are rife with spelling issues and I to find it very bothersome to tell the truth on the other hand I will definitely come again again.
  • Joseph 2019/07/20 10:04 # 삭제 답글

    Outstanding post, you have pointed out some fantastic points, I as well think this is a very great website.
  • Daniel 2019/07/20 21:10 # 삭제 답글

    This will be a fantastic site, will you be interested in doing an interview regarding just how you designed it? If so email me!
  • Daniel 2019/07/21 22:38 # 삭제 답글

    I simply couldn't depart your site prior to suggesting that I actually loved the standard info an individual supply for your guests? Is going to be back regularly in order to inspect new posts.
  • Charles 2019/07/22 07:16 # 삭제 답글

    What is the website that means it is easy to understand podcasts and blog sites? I don't get an iPod, does that make any difference? .
  • Robert 2019/07/22 08:30 # 삭제 답글

    I'm searching for websites that may have excellent tips about what's popular and what the most effective makeup products is..
  • Robert 2019/07/22 20:17 # 삭제 답글

    Definitely pent topic matter, appreciate it for selective information.
  • Thomas 2019/07/24 11:50 # 삭제 답글

    I like the helpful information you provide in your articles. Ill bookmark your weblog and check again here regularly. I'm quite certain I will learn plenty of new stuff right here! Good luck for the next!
  • Paul 2019/07/25 02:31 # 삭제 답글

    Beneficial Location Hi gentleman the following transpire a number of web page link with the aim of represses information that will a person may possibly acquire positive yourselves. The Significance Verifying away from home.
  • Donald 2019/07/26 01:32 # 삭제 답글

    Very neat article post.Much thanks again. Much obliged.
  • Mark 2019/07/26 15:14 # 삭제 답글

    You are my inhalation, I have few web logs and rarely run out from post
  • Joseph 2019/07/27 15:15 # 삭제 답글

    cheers for the actual article i've recently been on the lookout with regard to this kind of advice on the net for sum time proper now so numerous thanks
  • George 2019/07/30 20:35 # 삭제 답글

    Really enjoyed reading ur blog.
  • Richard 2019/07/31 15:36 # 삭제 답글

    Over the course of the initial period, they began to flrm group identities.
  • William 2019/08/01 12:26 # 삭제 답글

    Howdy would you mind sharing which blog platform you're working
  • Paul 2019/08/01 14:13 # 삭제 답글

    I am sure this post has touched all the internet people, its really really nice post on building up new web site.
  • John 2019/08/01 16:00 # 삭제 답글

    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.
  • Thomas 2019/08/02 18:00 # 삭제 답글

    Hey there! Do you know if they make any plugins to help with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good results. If you know of any please share. Thank you!
  • Richard 2019/08/03 09:37 # 삭제 답글

    Specifically where web can a certified psyciatrist submit content or sites for them to become fashionable?
  • Michael 2019/08/04 14:03 # 삭제 답글

    Very efficiently written post. It will be valuable to anyone who usess it, as well as myself. Keep doing what you are doing i will definitely read more posts.
  • William 2019/08/06 17:38 # 삭제 답글

    Farmville farms even include free gift that is
  • Mark 2019/08/07 22:11 # 삭제 답글

    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.
  • Mark 2019/08/08 03:08 # 삭제 답글

    Hi my friend! I want to say that this post is amazing, great written and include almost all significant infos. I'd like to see more posts like this.
  • Mark 2019/08/10 04:04 # 삭제 답글

    Do you agree with my statement that this post is awesome?
  • John 2019/08/13 13:51 # 삭제 답글

    Hi, Neat post. There is an issue together with your site in internet explorer, may test this IE nonetheless is the marketplace leader and a huge part of folks will leave out your magnificent writing because of this problem.
  • Charles 2019/08/15 20:47 # 삭제 답글

    Wow, this article is fastidious, my younger sister is analyzing these kinds of things, thus I am going to convey her.
  • Donald 2019/08/16 00:33 # 삭제 답글

    very nice post, i certainly love this web site, keep on it
  • Donald 2019/08/18 12:27 # 삭제 답글

    As soon as I found this internet site I went on reddit to share some of the love with them.
  • James 2019/08/18 23:50 # 삭제 답글

    Hi, yup this post is truly fastidious and I have learned lot of things from it
  • Donald 2019/08/19 03:11 # 삭제 답글

    Keep working ,fantastic job!
  • George 2019/08/20 14:22 # 삭제 답글

    Wow, this article is fastidious, my younger sister is analyzing these kinds of things, thus I am going to convey her.
  • Donald 2019/08/21 02:09 # 삭제 답글

    Valuable information. Lucky me I found your site by accident, and I am shocked why this accident did not happened earlier! I bookmarked it.
  • James 2019/08/22 08:38 # 삭제 답글

    You are my inspiration, I possess few web logs and rarely run out from to brand.
  • Thomas 2019/08/23 18:55 # 삭제 답글

    Very energetic blog, I enjoyed that a lot. Perhaps there is a part 2?
  • John 2019/08/24 08:17 # 삭제 답글

    Thanks for another wonderful article. Where else could anybody get that kind of information in such an ideal way of writing? I have a presentation next week, and I'm on the look for such information.
  • James 2019/08/24 19:26 # 삭제 답글

    Hello.This post was extremely motivating, especially because I was browsing for thoughts on this subject last Thursday.
  • Joseph 2019/09/02 06:55 # 삭제 답글

    Farmville farms even include free gift that is especially
  • David 2019/09/03 18:51 # 삭제 답글

    you may have an important weblog here! would you prefer to make some invite posts on my blog?
  • Donald 2019/09/06 12:22 # 삭제 답글

    Mudbox is a software for 3D sculpting and painting which
  • Michael 2019/09/07 03:13 # 삭제 답글

    Wonderful site. Plenty of useful info here. I'm sending it to a few pals ans additionally sharing in delicious. And naturally, thank you to your sweat!
  • Donald 2019/09/07 18:13 # 삭제 답글

    Enjoyed examining this, very good stuff, thankyou. Talk sense to a fool and he calls you foolish. by Euripides.
  • William 2019/09/07 18:34 # 삭제 답글

    whoah this weblog is wonderful i really like studying your posts. Keep up the great paintings! You already know, lots of individuals are searching around for this information, you can help them greatly.
  • James 2019/09/13 09:18 # 삭제 답글

    plus offering extras which can announce this kind of the main benefit of the skepticism.
  • Richard 2019/09/17 13:31 # 삭제 답글

    I'm seeking weblogs which all have fantastic guidance on what's popular and specifically what the top rated makeup is..
  • John 2019/09/18 15:43 # 삭제 답글

    You made some really good points there. I checked on the internet for additional information about the issue and found most people will go along with your views on this site.
  • John 2019/09/19 23:32 # 삭제 답글

    Hello my family member! I want to say that this article is amazing, great written and come with almost all significant infos. Id like to peer extra posts like this .
  • Daniel 2019/09/21 08:47 # 삭제 답글

    It's a mammoth playground built of mountains, hills, lakes, rivers, valleys, woodlands,and beaches.
  • Joseph 2019/09/24 11:10 # 삭제 답글

    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.
  • Thomas 2019/09/25 12:51 # 삭제 답글

    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!
  • Paul 2019/09/26 13:22 # 삭제 답글

    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?
  • Paul 2019/09/30 22:08 # 삭제 답글

    nasa, , harold hill
  • Paul 2019/10/02 16:12 # 삭제 답글

    I'm seeking weblogs which all have fantastic guidance on what's popular and specifically what the top rated makeup is..
  • William 2019/10/04 15:20 # 삭제 답글

    What sites and blogs perform searching district interact most on?
  • Mark 2019/10/06 01:10 # 삭제 답글

    I'm glad that it turned out so effectively and I hope it will continue in the future because it is so worthwhile and meaningful to the community.
  • Mark 2019/10/09 23:41 # 삭제 답글

    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 2019/10/11 21:36 # 삭제 답글

    Very interesting subject , appreciate it for posting . All human beings should try to learn before they die what they are running from, and to, and why. by James Thurber.
  • Donald 2019/10/12 20:33 # 삭제 답글

    Great, thanks for sharing this blog.Really thank you!
  • Thomas 2019/10/12 23:33 # 삭제 답글

    I like the valuable info you provide in your articles. I will bookmark your blog and check again here regularly. I am quite certain I'll learn a lot of new stuff right here! Best of luck for the next!
  • Thomas 2019/10/13 13:01 # 삭제 답글

    I really like your writing style, good information, regards for putting up
  • William 2019/10/15 03:12 # 삭제 답글

    Aw, this was a very nice post. In thought I want to put in writing like this moreover taking time and actual effort to make an excellent article but what can I say I procrastinate alot and not at all seem to get something done.
  • Michael 2019/10/16 00:10 # 삭제 답글

    I have recently started a web site, the info you provide on this site has helped me greatly. Thank you for all of your time &amp work. There can be no real freedom without the freedom to fail. by Erich Fromm.
  • Donald 2019/10/19 05:04 # 삭제 답글

    I discovered your blog web site website on the search engines and check several of your early posts. Always sustain up the very excellent operate. I lately additional increase Rss to my MSN News Reader. Looking for toward reading much far more on your part later on!
  • Mark 2019/10/19 17:38 # 삭제 답글

    Vigamox Without A Script in Columbus
  • Mark 2019/10/20 03:24 # 삭제 답글

    This is really attentiongrabbing, You're a very professional blogger. I have joined your rss feed and sit up for in search of extra of your fantastic post. Also, I have shared your site in my social networks!
  • Michael 2019/10/21 22:56 # 삭제 답글

    I am really impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, its rare to see a great blog like this one nowadays
  • David 2019/10/22 07:04 # 삭제 답글

    I needed to thank you for this good read!! I certainly enjoyed every little bit of it. I've got you bookmarked to check out new stuff you post
  • Donald 2019/10/24 02:28 # 삭제 답글

    It is also possible that Zynga's chosen advertising network is to blame if we consider the case of the New York Times' website
  • David 2019/10/24 15:38 # 삭제 답글

    I think this is one of the most significant information for me. And i'm glad reading your article. But should remark on some general things, The web site style is wonderful, the articles is really excellent D. Good job, cheers
  • James 2019/10/25 06:53 # 삭제 답글

    Hi there, I found your web site by the use of Google while searching for a similar matter, your site came up, it seems to be good. I've bookmarked it in my google bookmarks.
  • Donald 2019/10/25 10:53 # 삭제 답글

    I genuinely enjoy studying on this website, it holds good content. Never fight an inanimate object. by P. J. O'Rourke.
  • Mark 2019/10/27 12:16 # 삭제 답글

    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.
  • Robert 2019/10/28 06:15 # 삭제 답글

    Outstanding post, I conceive people should acquire a lot from this web blog its rattling user genial. So much superb info on here
  • William 2019/10/29 03:29 # 삭제 답글

    weblog. Loads of gratitude sharing.
  • William 2019/10/30 17:26 # 삭제 답글

    I just couldn't depart your site before suggesting that I actually enjoyed the standard information a person provide for your visitors? Is gonna be back often in order to check up on new posts
  • Donald 2019/10/30 21:31 # 삭제 답글

    Wow! This could be one particular of the most useful blogs We've ever arrive across on this subject. Actually Excellent. I'm also an expert in this topic so I can understand your hard work.
  • Charles 2019/10/31 01:06 # 삭제 답글

    Im not that much of a online reader to be honest but your blogs really nice, keep it up! I'll go ahead and bookmark your site to come back later. All the best
  • Robert 2019/10/31 07:04 # 삭제 답글

    This is really interesting, You're a very skilled blogger. I have joined your feed and look forward to seeking more of your magnificent post. Also, I have shared your web site in my social networks!
  • Thomas 2019/11/02 02:14 # 삭제 답글

    Hi there! I could have sworn I've been to this blog before but after checking through some of the post I realized it's new to me. Nonetheless, I'm definitely happy I found it and I'll be bookmarking and checking back frequently!
  • Joseph 2019/11/03 16:36 # 삭제 답글

    Thank you for some other informative website. Where else may I get that type of info written in such an ideal way? I've a mission that I'm just now operating on, and I've been on the look out for such information.
  • John 2019/11/04 14:53 # 삭제 답글

    I'm searching for websites that may have excellent tips about what's popular and what the most effective makeup products is..
  • Richard 2019/11/06 03:29 # 삭제 답글

    You are my inhalation, I have few blogs and sometimes run out from to brand.
  • Charles 2019/11/07 16:44 # 삭제 답글

    Would you be taken with exchanging links?
  • Donald 2019/11/07 17:30 # 삭제 답글

    I'm curious to uncover out what blog system youre employing? Im experiencing some small security troubles with my latest weblog and Id like to locate something more safeguarded. Do you've any recommendations?
  • David 2019/11/08 07:28 # 삭제 답글

    Someone essentially help to make significantly posts I might state. This is the first time I frequented your web page and so far? I surprised with the research you made to create this actual submit amazing. Magnificent activity!
  • Paul 2019/11/08 18:32 # 삭제 답글

    Nice blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog stand out. Please let me know where you got your design. Bless you
  • William 2019/11/09 13:16 # 삭제 답글

    Very efficiently written post. It will be valuable to anyone who usess it, as well as myself. Keep doing what you are doing i will definitely read more posts.
  • David 2019/11/13 07:36 # 삭제 답글

    Over the course of the initial period, they began to flrm group identities.
  • John 2019/11/14 01:40 # 삭제 답글

    I discovered your blog web site website on the search engines and check several of your early posts. Always sustain up the very excellent operate. I lately additional increase Rss to my MSN News Reader. Looking for toward reading much far more on your part later on!
  • Donald 2019/11/16 02:28 # 삭제 답글

    Thanks so much for sharing this excellent info! I'm seeking forward to see much more posts!
  • George 2019/11/18 01:37 # 삭제 답글

    Thank you for your blog article. Great.
  • Charles 2019/11/18 15:00 # 삭제 답글

    Im genuinely enjoying the style and layout of one's website. Its a very uncomplicated on the eyes which makes it a great deal much more enjoyable for me to come here and pay a visit to far more typically. Did you hire out a designer to make your theme? Excellent perform!
  • Daniel 2019/11/19 06:19 # 삭제 답글

    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.
댓글 입력 영역
◀ 다음덧글    이전덧글 ▶