← Back to Lua Course | Chapter 7: Coroutines | Lesson 1 of 7

Coroutines क्या हैं

एक coroutine एक ऐसा function है जो बीच में pause हो सकता है और बाद में जहाँ छोड़ा था वहीं से resume हो सकता है।

Coroutines क्या हैं

एक coroutine अपने stack के साथ execution का एक independent thread है, लेकिन एक समय पर सिर्फ एक चलता है। यह तब तक चलता है जब तक यह yield न करे या खत्म न हो, और control explicitly वापस pass होता है, जिसे cooperative multitasking कहते हैं। Coroutines operating-system threads नहीं हैं और कभी parallel में नहीं चलते।

Note: Lua में Coroutines का type "thread" है, और इनमें से हज़ारों बनाना सस्ता है।

उदाहरण: What are coroutines

lua
local co = coroutine.create(function()
  print("inside the coroutine")
end)
print(type(co))
print("before resume")
coroutine.resume(co)
print("after resume")

-- Output:
-- thread
-- before resume
-- inside the coroutine
-- after resume
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह सोचना कि coroutines parallel में चलते हैं
  2. यह उम्मीद करना कि एक coroutine बनते ही चलेगा
  3. इन्हें operating-system threads से confuse करना
चैप्टर सारांश
  • Coroutines pause और resume होते हैं
  • एक समय पर सिर्फ एक चलता है
  • ये cooperatively switch होते हैं
  • इनका type thread है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.