为什么要在C / C ++中使用协程 [英] Why should I use coroutine in C/C++

查看:170
本文介绍了为什么要在C / C ++中使用协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此图片来自 setjmp和longjmp在C语言中的实际用法。



据我了解,协程是两个进程,它们看起来像是为 human 并行执行的,但实际上是为 machine 做单个进程。

>但是使用 setjmp & longjmp 我觉得很难阅读代码。如果需要写相同的一个。例如过程A& B,我将serval States 分配给将它们分为不同部分的过程,
依次执行:

 进程A 

切换(状态)
情况A1:如果(A1完成)
B1
休息;
...

进程B

切换(状态)
情况B1:如果(B1完成)
A2
休息一下;
...

我需要一个理由来支持我使用 setjmp & longjmp & C / C ++中的协程
有什么优势?

解决方案

setjmp / longjmp()当今的程序员很少使用。相反,您应该使用功能更强大的boost ::协程,或我的 QtNetworkNg 。 Coroutine在网络编程中被广泛使用,并且大部分用于网络编程中。


我是QtNetworkNg的作者,它提供了一个堆栈式的协程实现。在QtNetworkNg的文档中,我写道:


传统的网络编程使用线程。 send()/ recv()被阻止,然后操作系统将当前线程切换到另一个就绪线程,直到数据到达为止。这非常简单,并且易于网络编程。但是线程使用大量资源,数千个连接可能会消耗大量内存。更糟糕的是,线程会导致数据争用,数据崩溃甚至崩溃。


基于协程的范例是网络编程的现在和功能。协程是轻量级线程,它具有自己的堆栈,不由操作系统管理,而是由QtNetworkNg管理。像基于线程的范例一样,send()/ recv()被阻止,但在到达同一线程的同一线程中切换到另一个协程。可以低成本创建许多协程。因为只有一个线程,所以不需要锁或其他同步化。该API非常简单,就像基于线程的范例一样,但是避免了使用线程的复杂性。


除此之外,协程还可以更清晰地处理状态机和时间轴。一些在线游戏服务器使用协程来处理成千上万的同龄人之间的交互。


this image comes from Practical usage of setjmp and longjmp in C.

From my understanding, the coroutine is two process looks like doing parallelly for human but actually doing a single process for machine.

But using setjmp & longjmp I feel very hard to read the code. If need to write the same one. For example process A & B, I will give serval States to the processes to split them into different pieces(states), do sequentially like:

Process A

switch (state)
    case A1:
        if (A1 is done)
           do B1
        break;
    ...

Process B

switch (state)
    case B1:
        if (B1 is done)
           do A2
        break;
    ...

I need a reason to support me use setjmp & longjmp & coroutine in C/C++. What's advantage?

解决方案

setjmp/longjmp() is rarely used by nowaday programmer. Instead, you should use more powerful boost::coroutine, or my QtNetworkNg. Coroutine is used widely, and mostly, in network programming.

I am the author of QtNetworkNg which provides a stackful coroutine implementation. In the document of QtNetworkNg, I wrote:

The traditional network programming use threads. send()/recv() is blocked, and then the Operating System switch current thread to another ready thread until data arrived. This is very straightforward, and easy for network programming. But threads use heavy resources, thousands of connections may consume many memory. More worst, threads cause data races, data currupt, even crashes.

Coroutine-based paradigm is the now and feature of network programming. Coroutine is light-weight thread which has its own stack, not managed by Operating System but QtNetworkNg. Like thread-based paradigm, send()/recv() is blocked, but switch to another coroutine in the same thread unitl data arrived. Many coroutines can be created at low cost. Because there is only one thread, no locks or other synchoronization is needed. The API is straightforward like thread-based paradigm, but avoid the complexities of using threads.

Besides of that, coroutines can handle state machine and timeline more cleanly. Some online game server use coroutine to handle the interacting between thousands of peers.

这篇关于为什么要在C / C ++中使用协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆