Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I apologize for the oversight. I see the problem now. The issue lies in the `routine_wrapper` function where you're calling `mythread_exit()`. This leads to a recursive call which results in a segmentation fault.
- To fix this, you should use `longjmp` to jump directly to the exit point, bypassing the call stack.
- Here's the corrected `routine_wrapper` function:
- ```c
- void* routine_wrapper(void *arg){
- mythread_t* t = (mythread_t *)arg;
- if (setjmp(t->env) == 0) { // Only enter here on initial setup, not on longjmp
- t->res = t->start_routine(t->arg);
- t->completed = 1;
- longjmp(t->env, 1); // Jump to exit point
- }
- else {
- if (__sync_fetch_and_add(&THREAD_COMPLETED, 1) == NUM_ALIVE_THREADS - 1) {
- // All threads completed, resume the main thread
- __sync_synchronize();
- longjmp(t->env, 1); // Jump back to the main thread
- }
- else {
- pause(); // Suspend the current thread until all other threads complete
- }
- }
- }
- ```
- Please replace the `routine_wrapper` function in your code with this corrected version. This should resolve the segmentation fault issue.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement