免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234
最近访问板块 发新帖
楼主: TiGEr.zZ

[C] 在ANSI C下设计和实现简便通用的signal-slot机制 [复制链接]

论坛徽章:
0
发表于 2010-02-23 15:28 |显示全部楼层
作一些改进:

对assert附加了错误信息输出,有助于使用;
更好的Arm C编译器支持,包括RVDS和ADS,在RVDS下如果要编译成thumb代码模式的话,请在SIGNAL_CONNECT局部使用ARM模式,或者定义SLOT_USE_LONGJMP为1。

下载地址:
http://code.google.com/p/c-sigslot/downloads/list

论坛徽章:
0
发表于 2010-02-23 19:31 |显示全部楼层
QT 用的就是这种机制。。。

论坛徽章:
0
发表于 2010-02-25 22:24 |显示全部楼层
太长了,没完全看。请问可以把它做成一个组件吗?
本来想分析下Qt的信号槽机制,但一直没有挤出时间

论坛徽章:
2
天秤座
日期:2013-08-20 16:44:51狮子座
日期:2014-04-09 17:53:48
发表于 2013-08-09 14:16 |显示全部楼层
这是一个很有深度 广度的帖子,得洗洗咀嚼.慢慢消化.

论坛徽章:
0
发表于 2013-08-09 18:21 |显示全部楼层
写的篇幅有些长,看起来会头晕,真知灼见来自于对具体问题的揣摩,不一定要一一看完。

(1)掌握设计的准则,灵活性第一,增加灵活性可以通过提升抽象层次,延迟问题解决得到实现。
(2)难以直接解决的问题,可以通过引入附加的层次进行转换解决。

目前从语言层面上,一个最直接提升抽象层次的方法就是Closure,Coroutine,Continuation,Lambda Expression等等,文章里面的核心是实际上是C中一个实现方法,但具体实现还不够完善,包括最新更新到googlecode上的代码,其实在写这个代码之前,也有其他人有过类似的想法,但似乎也都不够完善。最终这种形式应该被并入到语言中,类似于函数式那样,使用堆来动态维护回调的代码及局部变量(运行环境)(文中的实现使用的是栈复制/构建/渲染方法,而c++ lambda的实现,不支持动态变量管理,需要维护引用变量的生命周期,我认为便利性上没有充分满足需求)。

很多人都把这种提升抽象层次的方法认为是实现异步的手段,甚至把两者直接等同以来,后者实际上是缩小的应用范围。反过来说signal-slot本身的终结目标应该是能够适应异步事件的处理,这样才可能发挥出其真正的威力,从操作系统到应用软件,从计算机到现实世界,我们时时刻刻都在处理着各种各样的异步并发事件。

所以googlecode上的项目将会更新到一个通用的更加完善异步signal-slot的基础设施上来,同时提供一个通用的异步调用的方式,保证以任意形式并行的事件可以以高层次一致性需求得到处理,而这种并行性的基础设施构建与一个通用的无锁优先队列上,从而完全从下而上的满足了并行世界的本质要求。

注:无锁并行化需要的最小原语是CAS,Compare and Swap,这已经并证明是充分的。

论坛徽章:
2
天秤座
日期:2013-08-20 16:44:51狮子座
日期:2014-04-09 17:53:48
发表于 2013-08-12 11:34 |显示全部楼层
[root@localhost c-sigslot]# ls
c-sigslot  c-sigslot.pro  Makefile  sigslot  sigslot.c  sigslot.h  sigslot.o  test.c  test.o
[root@localhost c-sigslot]# ./c-sigslot
[SIGSLOT DEBUG] Warning: using a signal connection without fetching args at: file "test.c", line 61
[SIGSLOT DEBUG] Warning: using a signal connection without fetching args at: file "test.c", line 75
[SIGSLOT DEBUG] Warning: using a signal connection without fetching args at: file "test.c", line 88
[SIGSLOT DEBUG] Warning: using a signal connection without fetching args at: file "test.c", line 101
[SIGSLOT DEBUG] Warning: using a signal connection without fetching args at: file "test.c", line 113
signal: 1, 2
dynamic slot: int=-1, float=-20.000000
[SIGSLOT DEBUG] These variables modification commited to the fetching args: "j". at: file "test.c", line 38
slot 1: int=10, float=20.000000
[SIGSLOT DEBUG] Warning: without fetching args, the modification of local variables inside slot will not commit: file "test.c", line 61
slot 2: int=100, float=200.000000
slot 3: int=1000, float=2000.000000
slot 3 disconnected.
slot 5: int=100000, float=200000.000000

signal: 1, 2
dynamic slot: int=-1, float=-200.000000
[SIGSLOT DEBUG] These variables modification commited to the fetching args: "j". at: file "test.c", line 38
slot 1: int=10, float=20.000000
[SIGSLOT DEBUG] Warning: without fetching args, the modification of local variables inside slot will not commit: file "test.c", line 61
slot 2: int=100, float=200.000000
slot 5: int=100000, float=200000.000000
slot 5 disconnected.

signal: 1, 2
dynamic slot: int=-1, float=-2000.000000
[SIGSLOT DEBUG] These variables modification commited to the fetching args: "j". at: file "test.c", line 38
slot 1: int=10, float=20.000000
[SIGSLOT DEBUG] Warning: without fetching args, the modification of local variables inside slot will not commit: file "test.c", line 61
slot 2: int=100, float=200.000000

signal free
dynamic slot disconnected!
slot 1 disconnected.
slot 2 disconnected.
slot 4 disconnected.
[root@localhost c-sigslot]#

论坛徽章:
2
天秤座
日期:2013-08-20 16:44:51狮子座
日期:2014-04-09 17:53:48
发表于 2013-08-12 14:00 |显示全部楼层
[root@localhost c-sigslot]# ./c-sigslot
signal: 1, 2
dynamic slot: int=-1, float=-20.000000
slot 1: int=10, float=20.000000
slot 2: int=100, float=200.000000
slot 3: int=1000, float=2000.000000
slot 3 disconnected.
slot 5: int=100000, float=200000.000000

signal: 1, 2
dynamic slot: int=-1, float=-200.000000
slot 1: int=10, float=20.000000
slot 2: int=100, float=200.000000
slot 5: int=100000, float=200000.000000
slot 5 disconnected.

signal: 1, 2
dynamic slot: int=-1, float=-2000.000000
slot 1: int=10, float=20.000000
slot 2: int=100, float=200.000000

signal free
dynamic slot disconnected!
slot 1 disconnected.
slot 2 disconnected.
slot 4 disconnected.
[root@localhost c-sigslot]#

论坛徽章:
10
戌狗
日期:2013-10-17 09:43:0215-16赛季CBA联赛之广东
日期:2018-02-05 11:22:1215-16赛季CBA联赛之八一
日期:2016-07-04 12:26:1815-16赛季CBA联赛之青岛
日期:2016-06-08 11:15:4115-16赛季CBA联赛之辽宁
日期:2016-04-05 10:10:1415-16赛季CBA联赛之辽宁
日期:2016-03-11 11:11:48酉鸡
日期:2014-12-18 14:35:48狮子座
日期:2014-02-20 10:14:07寅虎
日期:2013-12-02 13:48:2915-16赛季CBA联赛之广夏
日期:2018-03-21 08:51:10
发表于 2013-08-22 15:51 |显示全部楼层
Mark.

论坛徽章:
0
发表于 2019-01-16 15:02 |显示全部楼层
为什么不直接用函数指针,用setjmp/longjmp  搞这么复杂?

论坛徽章:
0
发表于 2019-01-25 19:56 |显示全部楼层
收藏,可以手动实现一个。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP