Thunk

Thunk

Als Thunk bezeichnet man im Jargon der Softwareentwicklung den Aufruf von Code, der einer anderen Plattform oder einem anderen Framework angehört. Bei der Umstellung von 16 auf 32 Bit beispielsweise, konnten die Betriebssysteme (OS/2, Windows NT etc.) 16 Bit-Code durch entsprechendes Umsetzen der Aufrufparameter und Adressen realisieren, so dass 16 Bit-Programme weiter verwendet werden konnten. In der modernen Softwareentwicklung ist ein Thunk z.B. der Aufruf von nativem Code aus Managed Code heraus und umgekehrt (siehe Java Native Access oder .NET P/Invoke). Es handelt sich also um einen Plattform-Übergang (Transition), bei dem die Aufrufkonventionen und/oder Übergabeparameter entsprechend umgesetzt werden müssen (Marshalling). Die Programmiersprache C++/CLI aus dem .NET-Framework von Microsoft wurde eigens dazu konzipiert, solche Thunks in beide Richtungen zu ermöglichen:

Managed-Unmanaged Aufruf

Gegeben sei eine native C++ Klasse z.B. in einem C++ Projekt oder als Bestandteil eines C++/CLI Projekts, die nachfolgend von managed Code verwendet wird:

 public class CNativeClass
 {
 private:
   int m_i;
 public:
   void SetValue( int i )
   {
     m_i = i;
   }
 };

Managed C++/CLI Klasse (die in dieser Form von beispielsweise C# direkt instanziert werden kann), welche die zuvor gezeigte native Klasse verwendet:

 public ref class CManagedClass
 {
 public:
   CManagedClass()
   {
     System::Int32 i = 42;
     CNativeClass* pNativeClass = new CNativeClass();
     pNativeClass->SetValue( i );//Umsetzung des Datentyps
     delete pNativeClass;
   }
 };

Unmanaged-Managed Aufruf

Managed C++/CLI Klasse:

 public ref class CManagedClass
 {
 private:
   System::Int32 m_i;
 public:
   void SetValue( int i )
   {
     m_i = i;//Umsetzung des Datentyps
   }
 };

Native C++ Klasse in einem C++/CLI Projekt. Hier ist zu sehen, dass auch der umgekehrte Weg möglich ist, nämlich das Instanzieren von managed code innerhalb einer unmanaged Klasse. Bedingung ist jedoch, dass es sich um ein C++/CLI Projekt handelt, so dass der Compiler die entsprechende Syntax versteht. Der Thunk tritt bereits bei der gcnew Anweisung auf, da hier der Konstruktor der managed Klasse aufgerufen wird:

 public class CNativeClass
 {
 public:
   void Foo()
   {
     int i = 42;
     CManagedClass^ pManagedClass = gcnew CManagedClass();
     pManagedClass->SetValue( i );
   }
 };

Literatur

  • Marcus Heege: Expert C++/CLI. Apress Verlag, Berkeley 2007, ISBN 978-1-59059-756-9, Kapitel 9, ab Seite 203.

Wikimedia Foundation.

Игры ⚽ Нужен реферат?

Schlagen Sie auch in anderen Wörterbüchern nach:

  • Thunk — es un término usado en la jerga del desarrollo de software que designa la llamada o invocación a un código que pertenece a otra plataforma o a otro Framework. En el paso de 16 a 32 bit por ejemplo, los sistemas operativos (OS/2, Windows NT etc.)… …   Wikipedia Español

  • thunk — sound of impact, attested from 1952, echoic …   Etymology dictionary

  • thunk — ☆ thunk1 [thuŋk ] n. [echoic] an abrupt, muffled sound, as of an ax hitting a tree trunk vi. to make such a sound thunk2 [thuŋk] vt., vi. Dial. pt. & pp. of THINK1 …   English World dictionary

  • Thunk — The word thunk has at least three related meanings in computer science. A thunk may be: * a piece of code to perform a delayed computation (similar to a closure) * a feature of some virtual function table implementations (similar to a wrapper… …   Wikipedia

  • thunk — 1. verb /θʌŋk/ to strike against something, without breakage, making a thunk sound Who would have thunk those guys would have a problem with a little lye? 2. noun /θʌŋk/ a) a delayed computation b) In the Scheme programming language, a function… …   Wiktionary

  • Thunk — jocular past tense or past participle of think : I thunk and thunk (by analogy with sink/sunk ) …   Dictionary of Australian slang

  • thunk — I Australian Slang jocular past tense or past participle of think : I thunk and thunk (by analogy with sink/sunk ) II Mawdesley Glossary a leather boot lace, a leather thong …   English dialects glossary

  • thunk — Used in place of thought or think. Whoodah thunk I d be the one they picked. Or; I guess I didn t thunk it through all the way …   Dictionary of american slang

  • thunk — Used in place of thought or think. Whoodah thunk I d be the one they picked. Or; I guess I didn t thunk it through all the way …   Dictionary of american slang

  • thunk — I. dialect past and past participle of think II. noun Etymology: imitative Date: 1947 a flat hollow sound III. intransitive verb Date: 1949 to produce a flat hollow sound ; make a thunk …   New Collegiate Dictionary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”