Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you really want to do something like what the <code>ucontext.h</code> 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 <code>void *</code>, 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.</p> <p>There is one real problem, that converting the <code>int argc, ...</code> passed into <code>makecontext</code> 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 <code>makecontext</code> in any form other than <code>makecontext(ucp, func, 1, (void *)arg);</code>.</p> <p>Perhaps a better question though is why you think <code>ucontext.h</code> 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 <em>either</em> with <code>ucontext.h</code> <em>or</em> with pthreads, then comparing the performance and bloat. This will also have the advantage that, should future systems drop support for <code>ucontext.h</code>, 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.)</p> <p><strong>Edit</strong> (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:</p> <p><a href="https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables">https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables</a></p> <p>Your cooperative multitasking primitive of "hand off execution to thread X" would go something like:</p> <pre><code>self-&gt;flag = 0; other_thread-&gt;flag = 1; pthread_mutex_lock(other_thread-&gt;mutex); pthread_cond_signal(other_thread-&gt;cond); pthread_mutex_unlock(other_thread-&gt;mutex); pthread_mutex_lock(self-&gt;mutex); while (!self-&gt;flag) pthread_cond_wait(self-&gt;cond, self-&gt;mutex); pthread_mutex_unlock(self-&gt;mutex); </code></pre> <p>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 (<code>other_thread</code>'s mutex) is probably entirely unnecessary with this sort of usage, so you could perhaps make the mutex a local variable in the <code>task_switch</code> function. All you'd really be doing is using <code>pthread_cond_wait</code> and <code>pthread_cond_signal</code> as "go to sleep" and "wake up other thread" primitives.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload