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

问题描述

此图片来自。 Coroutine在网络编程中被广泛使用,并且大部分用于网络编程中。


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

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


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:

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 ++中使用协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 03:26