Advertisement
STEPAN_KODER

Untitled

Sep 27th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. 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.
  2.  
  3. To fix this, you should use `longjmp` to jump directly to the exit point, bypassing the call stack.
  4.  
  5. Here's the corrected `routine_wrapper` function:
  6.  
  7. ```c
  8. void* routine_wrapper(void *arg){
  9. mythread_t* t = (mythread_t *)arg;
  10. if (setjmp(t->env) == 0) { // Only enter here on initial setup, not on longjmp
  11. t->res = t->start_routine(t->arg);
  12. t->completed = 1;
  13. longjmp(t->env, 1); // Jump to exit point
  14. }
  15. else {
  16. if (__sync_fetch_and_add(&THREAD_COMPLETED, 1) == NUM_ALIVE_THREADS - 1) {
  17. // All threads completed, resume the main thread
  18. __sync_synchronize();
  19. longjmp(t->env, 1); // Jump back to the main thread
  20. }
  21. else {
  22. pause(); // Suspend the current thread until all other threads complete
  23. }
  24. }
  25. }
  26. ```
  27.  
  28. 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