Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- 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.
- Here's the corrected code:
- ```c
- void *work(void *arg) {
- for(int i = 0; i < 100; i++) {
- printf("hello world %d\n", i);
- }
- return NULL;
- }
- void *work2(void *arg) {
- for(int i = 0; i < 10; i++) {
- printf("ADS %d\n", i);
- }
- return (void *) 10;
- }
- int main(int argc, char *argv[]) {
- mythread_t t, t2;
- mythread_create(&t, work, NULL);
- mythread_create(&t2, work2, NULL);
- int *res = malloc(sizeof(int));
- // Detach t, as you're not planning to join it later
- mythread_detach(&t);
- // Join t2, which is joinable
- mythread_join(&t2, (void **) res);
- mythread_exit();
- return 0;
- }
- ```
- 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