问题

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. Git Push 避免用户名和密码方法

    参考这里: http://www.cnblogs.com/ballwql/p/3462104.html 亲测第一种方法有效

  2. T-SQL 基础学习 03

    局部变量 在T-SQL中,局部变量的名称必须以标记@作为前缀 语法 DECLARE @变量名数据类型 局部变量的赋值 方法一 SET @变量名 = 值 方法二 SELECT @变量名 = 值 SET和 ...

  3. AutoCompleteTextView自动补全文本框

    AutoCompleteTextView的作用是在输入框中输入我们想要输入的信息,就会出现其他与其相关的提示信息 下面是实例代码: MainActivity.java package com.shao ...

  4. Mac iTerm 很好用的终端

    配合Go2Shell使用,效果更佳!我建议你去商店下载它! 先去官网下载软件, 然后可以看看这一篇简明教程. 这里,还有一点点干货也一起带走吧! open -a Go2Shell --args con ...

  5. 踩坑事件:windows操作系统下的eclipse中编写SparkSQL不能从本地读取或者保存parquet文件

    这个大坑... .... 如题,在Windows的eclipse中编写SparkSQL代码时,编写如下代码时,一运行就抛出一堆空指针异常: // 首先还是创建SparkConf SparkConf c ...

  6. WPF 四种样式

    1.内联样式<TextBlock FontSize="20" Foreground="Blue">好啊</TextBlock> 2.页面 ...

  7. Hadoop生态圈

    1.Hadoop是什么? 适合大数据的分布式存储与计算平台 HDFS: Hadoop Distributed File System分布式文件系统 MapReduce:并行计算框架 解决的问题: HD ...

  8. 快排查找第K小的数

    #include "iostream.h" using namespace std; int findMedian(int *A,int left,int right){ int ...

  9. win7 hosts文件更改后无效,不生效问题

    通过记事本修改完hosts文件后,域名解析没有生效. 注:可以正常保存修改.服务dnsclient也已经启动 最后发现是hosts文件最后一行不是空行,回车后问题解决

  10. Spring boot 学习记录

    java的三种配置方式 基于xml的配置 基于注解的配置 基于java的配置 Spring boot推荐的配置方式:java配置+注解配置 一.注解 SpringBootApplication :等价 ...