自动生成返回码检查器的工具
我正在寻找一个生成子例程来检查其他子例程的返回码的工具。
例如,pthread_create
可以返回 0
、EAGAIN
、EINVAL
和 EPERM
代码。如果有这样的检查器就好了:
void pthread_create_check(int retcode) {
switch (retcode) {
case 0:
printf("pthread_create success.\n");
break;
case EAGAIN:
printf("pthread_create EAGAIN error: insufficient resources"
" to create another thread, or a system-imposed"
" limit on the number of threads was encountered.\n");
break;
case EINVAL:
printf("pthread_create EINVAL error: invalid settings in"
" attr.\n");
break;
case EPERM:
printf("pthread_create EPERM error: no permission to set the"
" scheduling policy and parameters specified in"
" attr.\n");
break;
}
}
并以这样的方式使用它:
iret = pthread_create(&thread_desc,
NULL,
thread_function,
(void *) thread_param);
pthread_create_check(iret);
手册页中有每个错误代码的解释。创建这样的检查器只不过是复制粘贴错误代码和手册页中的解释。我认为计算机可以比人类更好地完成这项工作,因为计算机永远不会疲劳。另外,我懒得为每个子程序调用都这样做。有没有自动化工具?
I'm looking for a tool which generates subroutine for checking a return code of some other subroutine.
For example, pthread_create
can returns 0
, EAGAIN
, EINVAL
and EPERM
codes. It would be nice to have such checker:
void pthread_create_check(int retcode) {
switch (retcode) {
case 0:
printf("pthread_create success.\n");
break;
case EAGAIN:
printf("pthread_create EAGAIN error: insufficient resources"
" to create another thread, or a system-imposed"
" limit on the number of threads was encountered.\n");
break;
case EINVAL:
printf("pthread_create EINVAL error: invalid settings in"
" attr.\n");
break;
case EPERM:
printf("pthread_create EPERM error: no permission to set the"
" scheduling policy and parameters specified in"
" attr.\n");
break;
}
}
And use it in such manner:
iret = pthread_create(&thread_desc,
NULL,
thread_function,
(void *) thread_param);
pthread_create_check(iret);
There are explanation of each error code in man page. Creating such checker is nothing but copy-paste error codes and explanation from man page. I think that computer can done this job much better than human since computer never get tire. Also, I'm too lazy to do it for every subroutine call. Is there any automation tool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需制作消息表即可。它将节省编码时间和空间。
没有什么可以阻止您在函数之间共享消息列表,因此您可以根据需要编写任意内容。
如果你疯了,你可以在调用中创建一个宏:
在这种情况下,共享消息列表意味着你需要为每个附加函数创建一个具有正确名称的指针,指向第一个函数的列表。工作量还是少了很多。
作为记录,我只是编写了一个带有通用消息的检查函数(成功消息除外,它们是垃圾邮件),并在围绕 pthread 的 C++ 包装器中的各处使用了它。 (别对 Boost 吹毛求疵,那是十年前的事了。)
Just make message tables. It will save coding time and space.
There's nothing stopping you from sharing message lists between functions, so you can write as little or as much as you want.
If you're insane, you can make a macro out of the call:
In this case, to share a message list means you need to create a pointer with the proper name for each additional function pointing to the list of the first function. Still a lot less work.
For the record, I just wrote one check function with generic messages (except for the success messages, they're spammy) and used it everywhere in my C++ wrapper around pthread. (Don't carp at me about Boost, this was ten years ago.)
Valgrind 的 Helgrind 可以检测 POSIX pthreads API 的滥用。
http://valgrind.org/docs/manual/hg -manual.html#hg-manual.api-checks
Valgrind's Helgrind can detect misuses of the POSIX pthreads API.
http://valgrind.org/docs/manual/hg-manual.html#hg-manual.api-checks