Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread - How to start running a new thread without calling join?

I want to start a new thread from the main thread. I can't use join since I don't want to wait for the thread to exit and than resume execution.
Basically what I need is something like pthread_start(...), can't find it though.

Edit:
As all of the answers suggested create_thread should start thread the problem is that in the simple code below it doesn't work. The output of the program below is "main thread". It seems like the sub thread never executed. Any idea where I'm wrong?
compiled and run on Fedora 14 GCC version 4.5.1

void *thread_proc(void* x)
{
   printf ("sub thread.\n");
   pthread_exit(NULL);
}


int main()
{
    pthread_t t1;
    int res = pthread_create(&t1, NULL, thread_proc, NULL);
    if (res)
    {
        printf ("error %d\n", res);
    }

    printf("main thread\n");
    return 0;
}
like image 212
Ohad Horesh Avatar asked May 31 '11 08:05

Ohad Horesh


People also ask

How do I start a pthread thread?

In main(), we declare a variable called thread_id, which is of type pthread_t, which is an integer used to identify the thread in the system. After declaring thread_id, we call pthread_create() function to create a thread.

Is pthread join necessary?

Yes if thread is attachable then pthread_join is must otherwise it creates a Zombie thread. Agree with answers above, just sharing a note from man page of pthread_join. After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated.

What happens if you don't join a thread?

Another scenario is spawning helper threads when processing tasks. If you don't join these threads, you might end up using more resources than there are concurrent tasks, making it harder to measure the load. To be clear, if you don't call join , the thread will complete at some point anyway, it won't leak or anything.

Can a pthread join itself?

A thread cannot join itself because a deadlock would occur and it is detected by the library. However, two threads may try to join each other. They will deadlock, but this situation is not detected by the library. The pthread_join subroutine also allows a thread to return information to another thread.


2 Answers

The function to start the thread is pthread_create, not pthread_join. You only use pthread_join when you are ready to wait, and resynchronize, and if you detach the thread, there's no need to use it at all. You can also join from a different thread.

Before exiting (either by calling exit or by returning from main), you have to ensure that no other thread is running. One way (but not the only) to do this is by joining with all of the threads you've created.

like image 92
James Kanze Avatar answered Sep 28 '22 20:09

James Kanze


the behaviour of your code depends on the scheduler; probably the main program exits before printf in the created thread has been executed. I hope simple sleep(some_seconds) at the end of the main() will cause the thread output to appear :)

like image 33
user396672 Avatar answered Sep 28 '22 19:09

user396672