15

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;
}
Ohad Horesh
  • 4,340
  • 6
  • 28
  • 45

7 Answers7

22

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.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • 1
    +1 Actually, you don't _have_ to wait for the other threads before exiting. Calling exit() will automatically terminate all threads. If you want to terminate gracefully you may want to use pthread_barrier or pthread_join, though. – Klas Lindbäck May 31 '11 at 13:42
  • 3
    @Klas Lindback Calling `exit()` will terminate the entire process, which of course terminates all threads. But this isn't an instantaneous process; there is a long list of steps that `exit` does, and terminating the threads is near the end of them. In the meantime, destructors of static variables have been called, memory structures (including those of `malloc`?) cleaned up, etc., etc. In the meantime, other threads continue to run, eventually using resources which have been cleaned up. C++11 also adds restrictions. – James Kanze May 31 '11 at 14:12
  • Ooooooh, I see now. Was wondering why my threads didn't get executed if I called `pthread_detach`... I understand how it works now. This is actually good for daemons / services, who don't ever exit (at least in theory). Thanks! – Eduard Luca Apr 21 '13 at 01:36
9

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 :)

user396672
  • 3,106
  • 1
  • 21
  • 31
  • 1
    Thanks @user396672 indeed this was the problem. I choose James answer since it matched the original question but at least I gave you an upvote :) – Ohad Horesh Jun 01 '11 at 04:43
  • 1
    This is the correct answer. The correct solution to wait for the spawned thread to run before returning from main() is to call pthread_join(t1, NULL) before returning. pthread_create() provides no guarantee that the thread *starts running* as soon as it returns. More, the time until the thread is actually scheduled is indefinite. Either you signal with a condition variable that the spawned thread completed its job, either you wait. – user1284631 Oct 21 '14 at 14:53
3

the join call waits for the thread to terminate and exit.

if you want your main thread to continue its execution while the child thread is executing, don't call join: the child thread will execute concurrently with the main thread...

Adrien Plisson
  • 22,486
  • 6
  • 42
  • 73
  • yes it is. what makes you think not calling join() is not standard ? (to be honest, you should still call join() at the end of the main thread to ensure that all child thread terminate before exiting the application) – Adrien Plisson Mar 30 '17 at 15:31
  • Do you think that I can just simply call detach() at the of the destructor of the class? – Samer Mar 30 '17 at 15:47
  • it depends on the design. you can do it, but it will let the thread run on its own: this is a kind of resource leak that i do not like. (but we are drifting away from the original question) – Adrien Plisson Mar 30 '17 at 22:54
2

Just create the thread with the detached attribute set to on. To achieve this, you can either call pthread_detach after the thread has been created or pthread_attr_setdetachstate prior to its creation.

When a thread is detached, the parent thread does not have to wait for it and cannot fetch its return value.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
1

you need to call pthread_exit in the end of man(), which will cause main to wait other thread to start and exit. Or you can explicitly call pthread_join to wait the newly created thread

Otherwise, when main returns, the process is killed and all thread it create will be killed.

cheng yang
  • 1,692
  • 3
  • 12
  • 21
0

The thread starts automatically when you create it.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • 2
    Also, it doesn't warrant a full answer, but if you don't join the threads you created, your program will exit before they done anything as execution in the main thread will go on. – Mel May 31 '11 at 08:19
0

Don't you just need to call pthread_create?

static void *thread_body(void *argument) { /* ... */ }

int main(void) {
    pthread_t thread;
    pthread_create(&thread, NULL, thread_body, NULL);
    /* ... */
Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75