问题

attempt to yield across metamethod/C-call boundary

需求跟如下帖子中描述一致:

http://bbs.chinaunix.net/forum.php?mod=viewthread&action=printable&tid=4065715

模拟一个场景,在C中创建出coroutine来执行Lua脚本,并且提供C API给Lua使用,当某些操作可能会阻塞时(如网络I/O),C函数中执行yield将协程切换出去,然后未来的某个时刻,如果条件满足则resume 继续执行后面的脚本.我写了个demo程序是这样的:

但是使用此帖子中的 方法的, 将resume和yield都放在c中实现, 也没有解决问题。

使用lua版本为 5.1.4

解法

http://stackoverflow.com/questions/8459459/lua-coroutine-error-tempt-to-yield-across-metamethod-c-call-boundary

There are several things you can do if you cannot change your code to avoid the C/metamethod boundary:

  • If you are using standard Lua, and are compiling it yourself, try patching it with Coco — True C Coroutines for Lua.

    True C coroutine semantics mean you can yield from a coroutine across a C call boundary and resume back to it.

  • Try using LuaJIT instead of the standard Lua interpreter. It uses a fully resumable VM meaning the boundary is not an issue.

  • Try using Lua 5.2. It features yieldable pcall and metamethods which means that it can handle your problem. However, there are some changes and incompatibilities between Lua 5.1 and Lua 5.2.

不改代码的话,有三种解法。

第一种,  打个补丁,最简单,对现有代码影响最小。

故使用这个方法。

coco patch

http://coco.luajit.org/

Coco is a small extension to get True C Coroutine semantics for Lua 5.1. Coco is available as a patch set against the standard Lua 5.1.5 source distribution.

Coco is also integrated into LuaJIT 1.x to allow yielding for JIT compiled functions. But note that Coco does not depend on LuaJIT and works fine with plain Lua.

Coco is Copyright © 2004-2016 Mike Pall. Coco is free software, released under the MIT license (same license as the Lua core).

由于其是基于5.1.5开发, 将patch移植到5.1.4版本还有一些比对工作。 如何将patch还原为old new, 可以使用前面博文介绍。

将patch移到版本中后发现,还是不生效。

最后确定是, c中开辟线程空间的时候, 没有使用 coco的接口, 还是使用 lua的原生接口。

http://lua-users.org/lists/lua-l/2011-10/msg00461.html

On 10/15/2011 5:35 PM, Szymon Gatner wrote:
> lua_State* coro = lua_newthread(L); You need to use lua_newcthread(). Otherwise LuaJIT 1.x doesn't create a
C stack.

http://coco.luajit.org/api.html

lua_State *lua_newcthread(lua_State *L, int cstacksize)

This is an (optional) new function that allows you to create a coroutine with an associated C stack directly from the C API. Other than that it works the same as lua_newthread(L).

You have to declare this function as extern yourself, since it's not part of the official Lua API. This means that a C module that uses this call cannot be loaded with standard Lua. This may be intentional.

lua协程一则报错解决“attempt to yield across metamethod/C-call boundary”的更多相关文章

  1. (报错解决)Exception encountered during context initialization

    转: (报错解决)Exception encountered during context initialization 关键词 JavaEE JavaWeb eclipse XML AspectJ ...

  2. sphinx :undefined reference to `libiconv' 报错解决办法

    sphinx :undefined reference to `libiconv' 报错解决办法   2013-11-30 21:45:39 安装sphinx时不停报错...郁闷在make时报错,错误 ...

  3. redis运用连接池报错解决

    redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not g ...

  4. linux下启动dbca或netmgr类的图形界面报错解决

    linux下启动dbca或netmgr类的图形界面报错解决    Xlib: connection to ":0.0" refused by server Xlib: No pro ...

  5. CentOS 6.5 Maven 编译 Apache Tez 0.8.3 踩坑/报错解决记录

    最近准备学习使用Tez,因此从官网下载了最新的Tez 0.8.3源码,按照安装教程编译使用.平时使用的集群环境是离线的,本打算这一次也进行离线编译,无奈一编译就开始报缺少jar包的错,即使手动下载ja ...

  6. 同时操作两个数据库:报错Illegal attempt to associate a collection with two open sessions

    今天我在一个操作两个数据库的SSH里 同时插入1条数据 报错 Illegal attempt to associate a collection with two open sessions 在这里有 ...

  7. spring boot jpa 使用update 报错解决办法

    在spring boot jpa 中自定义sql,执行update操作报错解决办法: 在@Query(...)上添加 @Modifying@Transactional注解

  8. eclipse创建的maven项目,pom.xml文件报错解决方法

    [错误一:]maven 编译级别过低 [解决办法:] 使用 maven-compiler-plugin 将 maven 编译级别改为 jdk1.6 以上: <!-- java编译插件 --> ...

  9. 搭建oracle linux虚拟机报错解决

    sysctl -P 报错解决办法问题症状修改 linux 内核文件 #vi /etc/sysctl.conf后执行sysctl  -P 报错error: "net.bridge.bridge ...

随机推荐

  1. iOS存储的三种方式

    iOS中存储的3中方式: 1.NSUserDefaults 注意存储基本对象类型是没有问题的,但是要存储自定义对象,则要将对象内所有的属性(或是需要存储的属性)序列化,实现NSCoding协议序列化. ...

  2. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  3. HDU 3038 How Many Answers Are Wrong(种类并查集)

    题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[ ...

  4. css记录

    padding padding-top是在绿色边框内,从顶部向下移20像素位置,默认padding-top 为0时,红色边框为20像素高,通过padding-top属性,为顶部增加了20像素,这时顶部 ...

  5. 两个多项式相加 ( C++ )

    实现两个多项式进行相加 不开辟空间 ( 这要求实现进行相加,代价为两个原链表将被修改) 分析: this>other 就把other当前结点放置在this之前 this<other 就th ...

  6. Volley

    Volley 是 Google 推出的轻量级 Android 异步网络请求框架和图片加载框架.在 Google I/O 2013 大会上发布.其适用场景是数据量小,通信频繁的网络操作. 主要特点: ( ...

  7. 设计模式之六大原则——迪米特法则(LoD,LKP)

    转载于:http://www.cnblogs.com/muzongyan/archive/2010/08/05/1793000.html  定义: 迪米特法则(Law of Demeter,LoD)也 ...

  8. http详解

    mac地址是网卡出厂的时候给定的固定地址.ip是当前节点被分配的地址,mac和ip相互配对: ip间通信- http本身是无状态无连接的,因为有这样的特性才能让他适用于当今这么复杂的网络环境. 但是有 ...

  9. mysql导入大文件sql

    备份mysqldump mysqldump -u root -p ao2012 > /mnt/www/zq_ao2012/backup.sql 然后数据库密码 导入 mysql -h local ...

  10. 采用DBCP连接池技术管理连接

    DBCP的使用步骤步骤一:导包,使用第三方的道具,必须导入相应的jar包. 一般需要导入两个jar包: -commons-dbcp-1.x.jar包 -commons-pool-1.x.x.jar包 ...