pthread——如何在不调用join的情况下开始运行一个新线程?

时间:2022-01-16 23:46:46

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.

我想从主线程开始一个新的线程。我不能使用join,因为我不想等待线程退出并恢复执行。基本上我需要的是pthread_start(…)之类的东西,但是找不到。

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

编辑:正如所有的答案都建议create_thread应该启动thread一样,问题是在下面的简单代码中它不能工作。下面程序的输出是“主线程”。似乎子线程从未执行过。知道我哪里错了吗?在Fedora 14版本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;
}

7 个解决方案

#1


16  

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.

启动线程的函数是pthread_create,而不是pthread_join。您只在准备等待和重新同步时才使用pthread_join,并且如果分离线程,则根本不需要使用它。您还可以从不同的线程连接。

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.

在退出(通过调用exit或从main返回)之前,必须确保没有其他线程正在运行。实现这一点的一种方法(但不是唯一的一种)是加入您创建的所有线程。

#2


5  

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

代码的行为取决于调度程序;在创建的线程中,主程序可能在printf执行之前退出。我希望main()末尾的简单睡眠(some_seconds)将导致线程输出出现:)

#3


3  

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

join调用等待线程终止和退出。

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...

如果希望主线程在子线程执行时继续执行,请不要调用join:子线程将与主线程同时执行……

#4


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.

只需创建具有分离属性的线程。为了实现这一点,您可以在线程创建之前调用pthread_detach,或者在创建之前调用pthread_attr_setdetachstate。

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

当线程被分离时,父线程不必等待它,也不能获取它的返回值。

#5


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

您需要在man()末尾调用pthread_exit,这会导致main等待其他线程启动和退出。或者可以显式地调用pthread_join来等待新创建的线程

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

否则,当主返回时,进程被杀死,它创建的所有线程将被杀死。

#6


0  

The thread starts automatically when you create it.

当您创建线程时,该线程将自动启动。

#7


0  

Don't you just need to call pthread_create?

难道你不需要调用pthread_create吗?

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

int main(void) {
    pthread_t thread;
    pthread_create(&thread, NULL, thread_body, NULL);
    /* ... */

#1


16  

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.

启动线程的函数是pthread_create,而不是pthread_join。您只在准备等待和重新同步时才使用pthread_join,并且如果分离线程,则根本不需要使用它。您还可以从不同的线程连接。

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.

在退出(通过调用exit或从main返回)之前,必须确保没有其他线程正在运行。实现这一点的一种方法(但不是唯一的一种)是加入您创建的所有线程。

#2


5  

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

代码的行为取决于调度程序;在创建的线程中,主程序可能在printf执行之前退出。我希望main()末尾的简单睡眠(some_seconds)将导致线程输出出现:)

#3


3  

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

join调用等待线程终止和退出。

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...

如果希望主线程在子线程执行时继续执行,请不要调用join:子线程将与主线程同时执行……

#4


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.

只需创建具有分离属性的线程。为了实现这一点,您可以在线程创建之前调用pthread_detach,或者在创建之前调用pthread_attr_setdetachstate。

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

当线程被分离时,父线程不必等待它,也不能获取它的返回值。

#5


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

您需要在man()末尾调用pthread_exit,这会导致main等待其他线程启动和退出。或者可以显式地调用pthread_join来等待新创建的线程

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

否则,当主返回时,进程被杀死,它创建的所有线程将被杀死。

#6


0  

The thread starts automatically when you create it.

当您创建线程时,该线程将自动启动。

#7


0  

Don't you just need to call pthread_create?

难道你不需要调用pthread_create吗?

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

int main(void) {
    pthread_t thread;
    pthread_create(&thread, NULL, thread_body, NULL);
    /* ... */