Abstract | U ovom diplomskom radu bavimo se korutinama i asinkronim programiranjem. Prvenstveno opisujemo rad korutina u programskom jeziku C++ i implementiramo primjere za bolje upoznavanje asinkrone paradigme. Rad je započet detaljnim opisivanjem standardne implementacije korutina u programskom jeziku C++ i biblioteke <coroutine> uz pomoć koje su korutine standardizirane 2020. godine. Kako biblioteka ne pruža potpunu podršku za rad s korutinama pokazujemo od nule implementaciju dodatnih klasa koje su potrebne, a to su: sučelje za korutine, promise tip i Awaitable tip. Kroz poglavlje unaprijeđujemo implementaciju klasa i pokazujemo kako klase medusobno suraduju za što bolju manipulaciju korutinama. Osim standardnih asinkronih primjera, prikazujemo implementaciju generatora uz pomoć korutina. U drugom poglavlju pokazujemo open source biblioteku cppcoro s kojom korutine možemo koristiti na apstraktniji način te ih ne koristimo kao elemente niske razine što je viđeno u prvom poglavlju. Ne ulazimo u implementaciju same biblioteke nego opisujemo elemente koja one posjeduje i njihovo korištenje. Implementiramo primjere u kojima se vidi korisnost alata u suočavanju s asinkronim programiranjem. Također prikazujemo generatore koje biblioteka sadržava implementirane kao sučelja za korutinu. Na kraju tog poglavlja prikazujemo kako se biblioteka suočava s umrežavanjem (eng. networking) te komentiramo koje protokole podržava i na koji način. Tema zadnjeg poglavlja su korutine u drugim programskim jezicima. Prikazujemo njihovo korištenje u Pythonu, JavaScriptu i Kotlinu te opisujemo kakvu podršku imaju u usporedbi sa C++. Za spomenute programske jezike postoji dobra podrška za korutine te nije potrebna dodatna implementacija za korištenje korutina. U Pythonu, uz ključne riječi async i await, koristi se biblioteka asyncio koja olakšava asinkrono programiranje. U JavaScriptu se pojavljuju iste ključne riječi, a upravljanje korutinama riješavamo uz pomoć Promise objekata koji se manipuliraju uz pomoć Bluebird biblioteke. Korutine u Kotlinu se definiraju na malo drugačiji način, a za njihovo bolje upravljanje koristimo biblioteku kotlinx.coroutines. |
Abstract (english) | In this master’s thesis, we focus on coroutines and asynchronous programming. Primarily, we describe the functioning of coroutines in the C++ programming language and implement examples to better understand the asynchronous paradigm. The work begins with a detailed description of the standard coroutine implementation in the C++ programming language and the <coroutine> library, which standardized coroutines in 2020. Since the library does not provide complete support for working with coroutines, we demonstrate the implementation of additional necessary classes from scratch, namely: coroutine interface, promise type, and Awaitable type. Throughout the chapter, we improve the implementation of these classes and illustrate how they collaborate for better coroutine manipulation. In addition to standard asynchronous examples, we demonstrate the implementation of generators using coroutines. In the second chapter, we showcase the open-source library cppcoro, which allows us to use coroutines in a more abstract manner, rather than as low-level elements as seen in the first chapter. We do not delve into the implementation of the library itself but describe its elements and their usage. We implement examples that highlight the utility of the tool in dealing with asynchronous programming. Additionally, we showcase generators implemented as coroutine interfaces within the library. Towards the end of this chapter, we illustrate how the library handles networking and comment on the protocols it supports and how. The theme of the final chapter is coroutines in other programming languages. We demonstrate their usage in Python, JavaScript, and Kotlin, describing the support they have compared to C++. For the mentioned programming languages, there is robust support for coroutines, eliminating the need for additional implementation to use them. In Python, alongside the keywords async and await, the asyncio library facilitates asynchronous programming. In JavaScript, similar keywords are used, and coroutine management is handled using Promise objects manipulated with the Bluebird library. Coroutines in Kotlin are defined in a slightly different manner, and we use the kotlinx.coroutines library for their better management. |