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 2018/12/06 01:27 # 삭제 답글

    I appreciate you sharing this article.Thanks Again. Really Cool.
  • William 2018/12/06 02:35 # 삭제 답글

    This will be a fantastic site, will you be interested in doing an interview regarding just how you designed it? If so email me!
  • Michael 2018/12/06 23:53 # 삭제 답글

    Magnificent beat ! I wish to apprentice even as you amend your site, how could i subscribe for a blog web site? The account helped me a acceptable deal. I were a little bit acquainted of this your broadcast offered bright transparent concept
  • Donald 2018/12/08 15:57 # 삭제 답글

    I'm trying to find sites that have already fantastic useful information on what's popular and what is the optimum makeup products is..
  • William 2018/12/13 23:58 # 삭제 답글

    I went over this site and I conceive you have a lot of wonderful information, bookmarked .
  • David 2018/12/14 16:49 # 삭제 답글

    Id forever want to be update on new articles on this internet site, bookmarked ! .
  • Thomas 2018/12/14 18:45 # 삭제 답글

    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
  • Robert 2018/12/16 06:06 # 삭제 답글

    Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way Ill be subscribing to your feeds and even I achievement you access consistently rapidly.
  • Paul 2018/12/16 21:14 # 삭제 답글

    I got what you intend, thankyou for putting up.Woh I am glad to find this website through google. It is a very hard undertaking to seek to please everybody. by Publilius Syrus.
  • John 2018/12/17 04:40 # 삭제 답글

    I am no longer sure the place you're getting your info, but great topic. I needs to spend a while studying much more or understanding more. Thanks for wonderful info I was on the lookout for this info for my mission.
  • James 2018/12/19 08:37 # 삭제 답글

    whoah this blog is fantastic i really like studying your articles. Keep up the good paintings! You understand, many individuals are looking around for this info, you could aid them greatly.
  • Mark 2018/12/20 10:19 # 삭제 답글

    Nice read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch as I found it for him smile Thus let me rephrase that Thanks for lunch!
  • Mark 2018/12/21 18:07 # 삭제 답글

    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?
  • Richard 2018/12/24 04:22 # 삭제 답글

    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.
  • John 2018/12/24 12:53 # 삭제 답글

    It'll also save a lot of cash and time for those on
  • William 2018/12/26 09:47 # 삭제 답글

    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!!
  • John 2018/12/27 13:39 # 삭제 답글

    You are my inhalation, I have few web logs and rarely run out from post
  • Mark 2018/12/27 16:43 # 삭제 답글

    If you are going for best contents like myself, just pay a quick visit this web page everyday since it offers feature contents, thanks
  • Michael 2018/12/29 06:28 # 삭제 답글

    You are my breathing in, I possess few blogs and very sporadically run out from to brand.
  • Robert 2018/12/29 09:01 # 삭제 답글

    Thanks for the article, is there any way I can receive an email whenever you publish a new update?
  • Thomas 2019/01/01 01:55 # 삭제 답글

    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.
  • Richard 2019/01/04 11:11 # 삭제 답글

    Hi there. Simply just planned to request a simple dilemma.
  • Mark 2019/01/05 02:55 # 삭제 답글

    tretinoin chemotherapy
  • John 2019/01/09 06:04 # 삭제 답글

    Hi, I desire to subscribe for this web site to obtain latest updates, therefore where can i do it please assist.
  • Thomas 2019/01/11 13:52 # 삭제 답글

    Because here is a list of multiplayer games is that the leave was asked for more.
  • George 2019/01/14 02:33 # 삭제 답글

    Id forever want to be update on new articles on this internet site, bookmarked ! .
  • David 2019/01/16 12:13 # 삭제 답글

    Muchos Gracias for your blog post.
  • Joseph 2019/01/17 05:27 # 삭제 답글

    Do you have a spam problem on this site I also am
  • Donald 2019/01/18 07:10 # 삭제 답글

    Mudbox is a software for 3D sculpting and painting which is developed
  • Donald 2019/01/18 17:44 # 삭제 답글

    I got what you mean, regards for putting up.Woh I am delighted to find this website through google. You must pray that the way be long, full of adventures and experiences. by Constantine Peter Cavafy.
  • William 2019/01/23 11:55 # 삭제 답글

    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.
  • George 2019/01/24 12:35 # 삭제 답글

    Very informative article post.Really looking forward to read more. Fantastic.
  • Daniel 2019/01/25 11:49 # 삭제 답글

    very nice post, i certainly love this web site, keep on it
  • Mark 2019/01/25 14:04 # 삭제 답글

    Excellent post. I was checking continuously this blog and I'm impressed! Extremely useful information specially the last part
  • Richard 2019/01/27 01:32 # 삭제 답글

    whoah this blog is wonderful i like reading your articles.
  • Mark 2019/01/29 14:21 # 삭제 답글

    As soon as I found this internet site I went on reddit to share some of the love with them.
  • George 2019/01/30 04:42 # 삭제 답글

    This kind of game gives a real experience of building a farm and planting
  • Thomas 2019/02/01 00:12 # 삭제 답글

    Can you add a Blackberry template? This web page is tricky to read otherwise for those of us browsing with cell phones. Otherwise, in the event you can place a RSS link up, that would be good also.
  • Paul 2019/02/01 09:11 # 삭제 답글

    What's Happening i'm new to this, I stumbled upon this I have discovered It positively helpful and it has aided me out loads. I hope to contribute &amp help other users like its helped me. Good job.
  • Richard 2019/02/04 04:57 # 삭제 답글

    It is rare for me to uncover something on the web thats as entertaining and intriguing as what you have got here. Your page is sweet, your graphics are great, and whats much more, you use source that are relevant to what youre saying. That you are undoubtedly one in a million, well done!
  • Thomas 2019/02/04 13:52 # 삭제 답글

    Farmville coins are used to make experience points and to purchae things for tthe farm.
  • William 2019/02/08 15:50 # 삭제 답글

    you use disallowed a designer to make the style? Superb vocation!
  • William 2019/02/13 00:25 # 삭제 답글

    Your style is really unique compared to other folks I've read stuff from.
  • Richard 2019/02/15 13:04 # 삭제 답글

    It is rare for me to uncover something on the web thats as entertaining and intriguing as what you have got here. Your page is sweet, your graphics are great, and whats much more, you use source that are relevant to what youre saying. That you are undoubtedly one in a million, well done!
  • Michael 2019/02/17 02:44 # 삭제 답글

    I'm really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, its rare to see a great blog like this one these days..
  • George 2019/02/18 14:13 # 삭제 답글

    weblog. Loads of gratitude sharing.
  • David 2019/02/19 11:09 # 삭제 답글

    Pretty great post. I just stumbled upon your blog and wanted
  • Charles 2019/02/20 02:44 # 삭제 답글

    Regards for helping out, superb information.
  • Richard 2019/02/20 07:25 # 삭제 답글

    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.
  • Robert 2019/02/21 13:06 # 삭제 답글

    I've been absent for a while, but now I remember why I used to love this website. Thanks, I'll try and check back more frequently. How frequently you update your site?
  • Charles 2019/02/23 20:44 # 삭제 답글

    Good website! I truly love how it is easy on my eyes and the data are well written. I'm wondering how I could be notified whenever a new post has been made. I have subscribed to your RSS feed which must do the trick! Have a great day!
  • Richard 2019/02/23 22:57 # 삭제 답글

    tretinoin chemotherapy
  • James 2019/02/27 11:01 # 삭제 답글

    I am no longer sure the place you're getting your info, but great topic. I needs to spend a while studying much more or understanding more. Thanks for wonderful info I was on the lookout for this info for my mission.
  • Daniel 2019/02/28 21:13 # 삭제 답글

    Merely a smiling visitor here to share the adore , btw outstanding style. Audacity, more audacity and always audacity. by Georges Jacques Danton.
  • Charles 2019/03/02 20:54 # 삭제 답글

    Every weekend i used to pay a visit this website, for the reason that i wish for enjoyment, for the reason
  • Richard 2019/03/07 06:52 # 삭제 답글

    hello!,I like your writing so much! share we keep up a correspondence extra about your article on AOL? I require an expert on this space to resolve my problem. May be that is you! Looking ahead to look you.
  • John 2019/03/08 15:03 # 삭제 답글

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

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

    I will immediately clutch your rss feed as I can not to find your email subscription link or enewsletter service. Do you have any? Please let me know in order that I may just subscribe. Thanks.
  • Donald 2019/03/14 09:29 # 삭제 답글

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

    I'm curious to uncover out what weblog system youre employing? Im experiencing some small security problems with my latest weblog and Id like to locate something a lot more safeguarded. Do you have any recommendations?
  • Robert 2019/03/17 20:19 # 삭제 답글

    Have you ever considered about including
  • Mark 2019/03/20 23:36 # 삭제 답글

    I've been absent for a while, but now I remember why I used to love this site. Thank you, I'll try and check back more frequently. How frequently you update your site?
  • Donald 2019/03/24 00:46 # 삭제 답글

    My brother recommended I might like this website. He was totally right. This post truly made my day. You cann't imagine simply how much time I had spent for this info! Thanks!
  • Donald 2019/03/30 04:35 # 삭제 답글

    Its actually a nice and helpful piece of information. Im glad that you shared this useful information with us. Please keep us up to date like this. Thanks for sharing.
  • Joseph 2019/04/02 23:45 # 삭제 답글

    erythromycin medication drug
  • Paul 2019/04/03 05:04 # 삭제 답글

    plus offering extras which can announce this kind of the main benefit of the skepticism.
  • Michael 2019/04/04 17:14 # 삭제 답글

    If you are going for best contents like myself, just pay a quick visit this web page everyday since it offers feature contents, thanks
  • Paul 2019/04/05 13:32 # 삭제 답글

    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
  • Donald 2019/04/06 23:39 # 삭제 답글

    I went over this site and I conceive you have a lot of wonderful information, bookmarked .
  • Charles 2019/04/09 11:06 # 삭제 답글

    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
  • Robert 2019/04/11 09:45 # 삭제 답글

    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
  • David 2019/04/14 08:11 # 삭제 답글

    Hey. Cool post. There's a dilemma together with your website in chrome, and you may want to check this The browser will be the market leader and a huge component of other people will omit your great writing because of this issue.
  • James 2019/04/17 18:48 # 삭제 답글

    Several of these games are worth some time and are actually quite fun.
  • David 2019/04/18 17:46 # 삭제 답글

    Today, while I was at work, my sister stole my apple ipad and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!
  • Mark 2019/04/19 15:11 # 삭제 답글

    you use disallowed a designer to make the style? Superb vocation!
  • Charles 2019/04/21 03:55 # 삭제 답글

    Hi! This is my first visit to your blog! We are a
  • John 2019/04/21 09:42 # 삭제 답글

    Howdy! Would you mind if I share your weblog with my twitter group? Theres lots of people that I think would truly enjoy your content material. Please let me know. Thanks
  • Donald 2019/04/22 15:34 # 삭제 답글

    What's up Jackson, if you are a new internet user then you have to visit daily this web site and read the updated content at at this place.
  • George 2019/04/22 19:59 # 삭제 답글

    permethrin toxicity in cats bessant
  • Michael 2019/04/23 12:36 # 삭제 답글

    I value the post.Thanks Again. Awesome.
  • Mark 2019/04/26 06:26 # 삭제 답글

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

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

    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
  • William 2019/04/28 17:53 # 삭제 답글

    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.
  • Mark 2019/04/30 06:53 # 삭제 답글

    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.
  • James 2019/05/02 00:54 # 삭제 답글

    betamethasone acetatena phos
  • George 2019/05/02 07:39 # 삭제 답글

    Android Game Development takes more than knowledge
  • James 2019/05/03 15:34 # 삭제 답글

    We're a group of volunteers and opening a brand new scheme in our community. Your site offered us with useful information to paintings on. You've performed a formidable job and our entire group can be thankful to you.
  • Joseph 2019/05/05 09:29 # 삭제 답글

    Thanks for this article. I'd also like to express that it can often be hard if you are in school and simply starting out to initiate a long credit standing. There are many students who are just trying to endure and have an extended or favourable credit history are often a difficult matter to have.
  • Paul 2019/05/05 23:04 # 삭제 답글

    Just wanna remark on couple of general issues, The web site style is perfect, the subject matter is rattling excellent
  • George 2019/05/07 04:51 # 삭제 답글

    Thanks so much for the article.Much thanks again. Great.
  • David 2019/05/09 07:40 # 삭제 답글

    Admiring the hard work you put into your site and detailed information you offer. It's good to come across a blog every once in a while that isn't the same unwanted rehashed material. Wonderful read! I've bookmarked your site and I'm adding your RSS feeds to my Google account.
  • Joseph 2019/05/09 19:07 # 삭제 답글

    Wynn documented revenue before last number of quartersit could possibly preserve stagnating, or maybe counterpicking.
  • William 2019/05/17 08:53 # 삭제 답글

    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!
  • Joseph 2019/05/18 21:00 # 삭제 답글

    Hi there. Simply just planned to request a simple dilemma.
  • Charles 2019/05/19 19:35 # 삭제 답글

    I discovered your blog site on google and test a number of of your early posts. Proceed to keep up the excellent operate. I simply extra up your RSS feed to my MSN Information Reader. Looking for forward to reading extra from you in a while!
  • Richard 2019/05/22 20:37 # 삭제 답글

    Today, while I was at work, my sister stole my apple ipad and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!
  • William 2019/05/25 08:17 # 삭제 답글

    Farmville coins are used to make experience points and to purchae things for tthe farm.
  • Mark 2019/05/26 04:11 # 삭제 답글

    hi!,I love your writing so much! share we communicate more approximately your post on AOL? I need an expert in this space to resolve my problem. Maybe that's you! Looking forward to peer you.
댓글 입력 영역
◀ 다음덧글    이전덧글 ▶