有什么取代的<&ucontext.h GT;功能? [英] Is there something to replace the <ucontext.h> functions?

查看:130
本文介绍了有什么取代的<&ucontext.h GT;功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户线程函数< ucontext.h> 是德precated,因为他们使用去precatedÇ功能(他们的use空括号中的函数声明为一个参数)。

The user thread functions in <ucontext.h> are deprecated because they use a deprecated C feature (they use a function declaration with empty parentheses for an argument).

有他们的一个标准更换?我不觉得自己羽翼丰满线程善于实现协作线程。

Is there a standard replacement for them? I don't feel full-fledged threads are good at implementing cooperative threading.

推荐答案

如果你真的想要做这样的事情是什么 ucontext.h 函数允许,我会继续使用他们。还有什么会少便携。标志着他们在荒废POSIX似乎已经有人在委员会的书卷气一个可怕的错误。 POSIX本身需要函数指针和数据指针具有相同的尺寸和函数指针被重新presentable浇铸到空隙* 和C本身需要功能之间的流延指针类型和背面是往返安全的,所以有很多办法这个问题可能已被解决。

If you really want to do something like what the ucontext.h functions allow, I would keep using them. Anything else will be less portable. Marking them obsolescent in POSIX seems to have been a horrible mistake of pedantry by someone on the committee. POSIX itself requires function pointers and data pointers to be the same size and for function pointers to be representable cast to void *, and C itself requires a cast between function pointer types and back to be round-trip safe, so there are many ways this issue could have been solved.

有一个现实的问题,即转换 INT ARGC,... 传入 makecontext 成形式传递给功能不能没有重大帮助除非可变参数和非参数可变型函数调用约定碰巧是相同的(而且当时是相当有问题的是否可以稳健地进行)来完成从编译器。这个问题但可能已被简单地去precating比其他makecontext任何形式使用 makecontext 的解决(UCP,FUNC,1, (无效*)ARG);

There is one real problem, that converting the int argc, ... passed into makecontext into a form to pass to the function cannot be done without major assistance from the compiler unless the calling convention for variadic and non-variadic functions happens to be the same (and even then it's rather questionable whether it can be done robustly). This problem however could have been solved simply by deprecating the use of makecontext in any form other than makecontext(ucp, func, 1, (void *)arg);.

但也许一个更好的问题是,为什么你认为 ucontext.h 函数来处理线程的最佳途径。如果你想和他们一起去,我可能会建议写一个封装接口,你可以实现的或者的有 ucontext.h 的使用pthreads的,然后比较性能和膨胀。这也将有优势,应该未来系统放弃对 ucontext.h 的支持,你可以简单地切换到与基于pthread的,实施编译,一切都会简单的工作。 (届时,鼓胀症可能是不太重要的,多核的益处/ SMP将可能是巨大的,并希望pthread的实现会少臃肿。)

Perhaps a better question though is why you think ucontext.h functions are the best way to handle threading. If you do want to go with them, I might suggest writing a wrapper interface that you can implement either with ucontext.h or with pthreads, then comparing the performance and bloat. This will also have the advantage that, should future systems drop support for ucontext.h, you can simply switch to compiling with the pthread-based implementation and everything will simply work. (By then, the bloat might be less important, the benefit of multi-core/SMP will probably be huge, and hopefully pthread implementations will be less bloated.)

修改(根据OP的要求):为贯彻落实合作线程与pthreads的,你需要条件变量。这里有一个像样的pthreads的教程,了解如何使用它们:

Edit (based on OP's request): To implement "cooperative threading" with pthreads, you need condition variables. Here's a decent pthreads tutorial with information on using them:

<一个href=\"https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables\">https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables

您合作多任务基本的手断执行线程X会去是这样的:

Your cooperative multitasking primitive of "hand off execution to thread X" would go something like:

self->flag = 0;
other_thread->flag = 1;
pthread_mutex_lock(other_thread->mutex);
pthread_cond_signal(other_thread->cond);
pthread_mutex_unlock(other_thread->mutex);
pthread_mutex_lock(self->mutex);
while (!self->flag)
    pthread_cond_wait(self->cond, self->mutex);
pthread_mutex_unlock(self->mutex);

希望我得到的一切权利;至少总的想法是正确的。如果有人看到错误,请发表评论,这样我就可以修复它​​。锁定( other_thread 的互斥体)的一半是可能与这种用法完全没有必要,所以你也许可以使互斥一个局部变量在 task_switch 功能。所有你真的做的是使用调用pthread_cond_wait 调用pthread_cond_signal 是睡觉和唤醒其他线程原始人。

Hope I got that all right; at least the general idea is correct. If anyone sees mistakes please comment so I can fix it. Half of the locking (other_thread's mutex) is probably entirely unnecessary with this sort of usage, so you could perhaps make the mutex a local variable in the task_switch function. All you'd really be doing is using pthread_cond_wait and pthread_cond_signal as "go to sleep" and "wake up other thread" primitives.

这篇关于有什么取代的&lt;&ucontext.h GT;功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆