Advertisement
STEPAN_KODER

Untitled

Sep 27th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. I apologize for the confusion. The issue is likely due to an error in how the `mythread_create` function is used. Let's fix it.
  2.  
  3. The issue lies in how you're using `mythread_detach`. In the code provided, you're detaching `t`, but then later you try to join it, which is incorrect. A detached thread cannot be joined.
  4.  
  5. Here's the corrected code:
  6.  
  7. ```c
  8. void *work(void *arg) {
  9. for(int i = 0; i < 100; i++) {
  10. printf("hello world %d\n", i);
  11. }
  12. return NULL;
  13. }
  14.  
  15. void *work2(void *arg) {
  16. for(int i = 0; i < 10; i++) {
  17. printf("ADS %d\n", i);
  18. }
  19. return (void *) 10;
  20. }
  21.  
  22. int main(int argc, char *argv[]) {
  23. mythread_t t, t2;
  24. mythread_create(&t, work, NULL);
  25. mythread_create(&t2, work2, NULL);
  26. int *res = malloc(sizeof(int));
  27.  
  28. // Detach t, as you're not planning to join it later
  29. mythread_detach(&t);
  30.  
  31. // Join t2, which is joinable
  32. mythread_join(&t2, (void **) res);
  33. mythread_exit();
  34. return 0;
  35. }
  36. ```
  37.  
  38. With this code, `t` is detached, `t2` is joined, and there should be no segfault.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement