C程序中的多个线程

时间:2009-11-08 02:07:11

标签: c multithreading unix pthreads

我在C中编写一个Unix应用程序,它使用多个控制线程。我遇到一个问题,主要功能在它产生的线程有一个更改完成他们的工作之前终止。如何防止这种情况发生。我怀疑我需要使用pthread_join原语,但我不确定如何。谢谢!

5 个答案:

答案 0 :(得分:8)

是的,您可以使用pthread_join()(请参阅其他文章,了解如何操作)。但是,让我解释一下pthread模型并向您展示另一种选择。

在Unix中,当主线程从main返回时,当任何线程调用exit()或最后一个线程调用pthread_exit()时,进程退出。基于最后一个选项,您只需让主线程调用pthread_exit(),只要至少再运行一个线程,该进程就会保持活动状态。

答案 1 :(得分:5)

是的,这样做是使用pthread_join函数:假设您的线程处于“可加入”状态。

  • pthread_create:在此函数返回控件之后,您的线程将执行您的线程函数。

  • pthread_create之后,使用pthread_create中的 tid pthread__join

如果您的线程已分离,则必须使用其他一些技术,例如共享变量,等待信号,共享队列等。

可用的优秀参考资料here

答案 2 :(得分:3)

您可以通过多种不同方式执行此操作,但最简单的方法是在从pthread_exit()返回之前致电main()

请注意,即使您要等待的线程不可连接,此技术仍然有效。

答案 3 :(得分:1)

您可能需要查看此页面: http://publib.boulder.ibm.com/iseries/v5r2/ic2924/index.htm?info/apis/users_25.htm

 rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create()\n", rc);

  printf("Wait for the thread to exit\n");
  rc = pthread_join(thread, &status);

答案 4 :(得分:1)

检查我为其中一个库编写的这个简单的C代码

/*
 *   Copyright (c) 2011 Dino Ciuffetti <dino@tuxweb.it>, TuxWeb S.r.l., NuvolaBase Ltd
 *   
 *   This file is part of liborient.
 *
 *   Liborient is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Liborient is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Liborient.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

//pthread_rwlock_t ptr_thr_lock = PTHREAD_RWLOCK_INITIALIZER;

typedef struct {
        int t;
} thread_arguments;

void *thread_stuff(void *args) {
        thread_arguments *t_args;
        int tid;

        t_args = (thread_arguments *)args;
        //pthread_rwlock_rdlock(&ptr_thr_lock);
        tid = t_args->t;
        //pthread_rwlock_unlock(&ptr_thr_lock);

        /*while (1) {
        sleep (1);*/
        printf("Thread #%i!\n", tid);
        /*}*/

        t_args = NULL;
        pthread_exit(NULL);
        return NULL;
}

int wait_threads(pthread_t threads[], thread_arguments *t_args[], int nthreads) {
        int t;
        int rc;

        // Waiting for threads termination
        for(t=0; t<nthreads; t++) {
                rc = pthread_join(threads[t], NULL);
                free(t_args[t]);
                if (rc != 0) {
                        printf("Error waiting for termination of thread %i: %i\n", t, rc);
                        return 1;
                        break;
                }
        }

        return 0;
}

int spawn_threads(pthread_t threads[], thread_arguments *t_args[], int nthreads) {
        int t;
        int rc;

        // Spawning threads
        for(t=0; t<nthreads; t++) {
                t_args[t] = (thread_arguments *) malloc(sizeof(thread_arguments));
                //pthread_rwlock_wrlock(&ptr_thr_lock);
                t_args[t]->t = t;
                //pthread_rwlock_unlock(&ptr_thr_lock);

                printf("Spawning thread: %i\n", t);
                rc = pthread_create(&threads[t], NULL, (void *)thread_stuff, (void *)t_args[t]);
                if (rc != 0) {
                        printf("Error spawning thread %i: %i\n", t, rc);
                        wait_threads(threads, t_args, rc+1);
                        return t+1;
                        break;
                }
        }

        return 0;
}

int main() {
        pthread_t threads[20];
        thread_arguments *t_args[20];
        int rc;

        rc = spawn_threads(threads, t_args, 20);
        if (rc > 0) {
                printf("Failed spawning thread number %i\n", rc-1);
                return 1;
        }

        rc = wait_threads(threads, t_args, 20);

        return 0;
}