在C问题中创建多个线程

时间:2016-10-17 21:52:35

标签: c multithreading pthreads

我是C系统编程的初学者,我试图在C中编写一个使用线程来实现照片中图形的代码,click here to see the graph所以我写了这个

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

void *thread_1(void *arg){
    printf("je suis le thread 1.\n");
    (void) arg;
    pthread_exit(NULL);
    }
void *thread_2(void *arg){
    printf("je suis le thread 2.\n");
    (void) arg;
    pthread_exit(NULL);
    }

void *thread_3(void *arg){
    printf("je suis le thread 3.\n");
    (void) arg;
    pthread_exit(NULL);
    }

void *thread_4(void *arg){
    printf("je suis le thread 4 et je suis lance apres la fin du thread 1.\n");
    (void) arg;
    pthread_exit(NULL);
    }

void *thread_5(void *arg){
    printf("je suis le thread 5  et je suis lance apres la fin du thread 1 et le thread 2.\n");
    (void) arg;
    pthread_exit(NULL);
    }

void *thread_6(void *arg){
    printf("je suis le thread 6  et je suis lance apres la fin du thread 3 et le thread 5.\n");
    (void) arg;
    pthread_exit(NULL);
    }

int main(void){
    pthread_t thread1;
    pthread_t thread2;
    pthread_t thread3;
    pthread_t thread4;
    pthread_t thread5;
    pthread_t thread6;
    pthread_create(&thread1, NULL, thread_1, NULL);
    pthread_create(&thread2, NULL, thread_2, NULL);
    pthread_create(&thread3, NULL, thread_3, NULL);
    if(pthread_join(thread1, NULL)){
        pthread_create(&thread4, NULL, thread_4, NULL);
        perror("pthread_join");
        return EXIT_FAILURE;
    }
    if(pthread_join(thread1, NULL) && pthread_join(thread2, NULL)){
        pthread_create(&thread5, NULL, thread_5, NULL);
        perror("pthread_join");
        return EXIT_FAILURE;
    }
    if(pthread_join(thread3, NULL) && pthread_join(thread5, NULL)){
        pthread_create(&thread6, NULL, thread_6, NULL);
        perror("pthread_join");
        return EXIT_FAILURE;
    }
    if(pthread_join(thread4, NULL) && pthread_join(thread6, NULL)){
        printf("je suis l'etape 7 et la fin.\n");
        perror("pthread_join");
        return EXIT_FAILURE;
    }
    else{
        return EXIT_FAILURE;
    }
    return 0;
}

当此代码被执行时,我收到此消息

je suis le thread 2.
je suis le thread 1.
je suis le thread 3.
Segmentation fault (core dumped)

我想要的是,线程是按照排序创建的,就像图形意​​味着1-2-3然后4结束后1和2结束后5,将在3和5结束后创建6并且最后的指令将被执行 在6和4结束后,任何人都可以告诉我我做错了什么

1 个答案:

答案 0 :(得分:1)

确定后,

pthread_join返回0。您的代码中存在逻辑问题,仅在失败时运行下一个线程,并且同时返回错误!

你得到的问题是,你尝试加入未初始化的thread5时所有错误的逻辑。

尝试修改代码,只需添加几个else语句&amp;将测试值更改为0,现在有意义了:

int main(void){
    pthread_t thread1;
    pthread_t thread2;
    pthread_t thread3;
    pthread_t thread4;
    pthread_t thread5;
    pthread_t thread6;
    if (pthread_create(&thread1, NULL, thread_1, NULL))
        {
           perror("pthread_create 1");
           return EXIT_FAILURE;
        }

   if (pthread_create(&thread2, NULL, thread_2, NULL))
        {
           perror("pthread_create 2");
           return EXIT_FAILURE;
        }
    if (pthread_create(&thread3, NULL, thread_3, NULL))
      {
         perror("pthread_create 3");
         return EXIT_FAILURE;
      }

    if(pthread_join(thread1, NULL)==0){
        if (pthread_create(&thread4, NULL, thread_4, NULL))
        {
           perror("pthread_create 4");
           return EXIT_FAILURE;
        }
    }
    else
    {
        perror("pthread_join");
        return EXIT_FAILURE;
    }
    if(pthread_join(thread2, NULL)==0){
        if (pthread_create(&thread5, NULL, thread_5, NULL))
        {
           perror("pthread_create 5");
           return EXIT_FAILURE;
        }


    }
    else
    {
        perror("pthread_join");
        return EXIT_FAILURE;
    }
    if(pthread_join(thread3, NULL)==0 && pthread_join(thread5, NULL)==0){
        if (pthread_create(&thread6, NULL, thread_6, NULL))
        {
           perror("pthread_create 6");
           return EXIT_FAILURE;
        }
    }
    else
    {
        perror("pthread_join");
        return EXIT_FAILURE;
    }
    if(pthread_join(thread4, NULL)==0 && pthread_join(thread6, NULL)==0){
        printf("je suis l'etape 7 et la fin.\n");
    }
    else
    {
        perror("pthread_join");
        return EXIT_FAILURE;
    }

    return 0;
}