【问题标题】:How to make 4 threads interact with each other?如何让4个线程相互交互?
【发布时间】:2017-07-25 22:07:03
【问题描述】:

我有一个有 4 个线程的程序,最后我应该打印线程和管道(2 个周期)阶段。比如:

Thread 2: Stage 1 and 2
Thread 3: Stage 2 and 3
Thread 1: Stage 3 and 4
Thread 4: Stage 4 and 5

但我不知道如何为阶段做这个计数器,因为我正在做的事情除了每个线程的阶段 1 和 2,而不是 1 和 2、2 和 3,我无法显示任何东西......

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void delay (int miliseconds){

    long pause;
    clock_t now,then;

    pause = miliseconds*(CLOCKS_PER_SEC/1000);
    now = then = clock();
    while( (now-then) < pause )
        now = clock();
}

int print(int n, int parar){

    if (n == 5) {
        printf("\nEstágio 4 e 5");

    } else {
        printf("Estágio %i e %i\n",n, n+1);
        if (parar == 2) {
            return (n+1, parar+0);
        }

        return print(n+1, parar+1);
    }
}

void thread_cont(void *arg){

    int *pvalor;
    pvalor=arg;

    pthread_mutex_lock(&mutex);
    printf ("\n---Thread %i--- \n", *pvalor);
    int a = print(1, 1);
    delay(2000);
    pthread_mutex_unlock(&mutex);

/*
    if (*pvalor == 1){
        printf ("Estágio 1 e 2");
    }

    if (*pvalor == 2){
    printf ("Estágio 2 e 3");
    }

    if (*pvalor == 3){
    printf ("Estágio 3 e 4");
    }

    if (*pvalor == 4){
    printf ("Estágio 4 e 5");
    }

*/

}

int main() {

    pthread_t id1;
    int offset1 = 1;
    pthread_create(&id1, NULL, thread_cont, &offset1);

    pthread_t id2;
    int offset2 = 2;
    pthread_create(&id2, NULL, thread_cont, &offset2);

    pthread_t id3;
    int offset3 = 3;
    pthread_create(&id3, NULL, thread_cont, &offset3);

    pthread_t id4;
    int offset4 = 4;
    pthread_create(&id4, NULL, thread_cont, &offset4);

    pthread_join(id1, NULL);
    pthread_join(id2, NULL);
    pthread_join(id3, NULL);
    pthread_join(id4, NULL);

    printf("\n");

    return 0;
}

【问题讨论】:

  • 由于每个线程都调用print(1, 1),因此很难理解为什么您期望不同的行为。也许你应该使用print(*pvalor, *pvalor+1)?我不清楚print() 函数真正应该做什么,以及为什么它递归地调用自己。另外,return (n+1, parar+0);return parar; 没有任何不同——你是否也打算在那里打电话给print()?为什么+ 0

标签: c multithreading pthreads pipeline


【解决方案1】:

我认为我对我的问题的解释有误。对此感到抱歉,但我想我现在就结束了。 4个线程。 4条指令,管道(可能)并且没有使用互斥锁,而是信号量。顺便说一句,语言障碍让一切变得更加困难,甚至在这里问。

@edit 最终可能是正确的代码

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

sem_t mutex;
pthread_t id1;
pthread_t id2;
pthread_t id3;
pthread_t id4;

void delay (int miliseconds){

    long pause;
    clock_t now,then;

    pause = miliseconds*(CLOCKS_PER_SEC/1000);
    now = then = clock();
    while( (now-then) < pause )
        now = clock();
}

pthread_mutex_t lock;

void *thread_cont(void *arg){

    int *pvalor;
    pvalor=arg;


    pthread_mutex_lock(&lock);
    sem_wait(&mutex);
    printf ("\n---Thread %i---", *pvalor);
    printf ("Stage 1");
    delay(2000);


    printf ("\n---Thread %i---", *pvalor);
    printf ("Stage 2");
    sem_post(&mutex);
    pthread_mutex_unlock(&lock);


    printf ("\n---Thread %i---", *pvalor);
    printf ("Stage 3");
    delay(2000);

    printf ("\n---Thread %i---", *pvalor);
    printf ("Stage 4");
    delay(2000);

    pthread_exit(0);

}


int main(){

    //create threads

    sem_init(&mutex, 0, 2);


//ONE FOR EACH CREATE

int offset1 = 1;
int offset2 = 2;
int offset3 = 3;
int offset4 = 4;


    pthread_create(&id1, NULL, thread_cont, &offset1);      
    delay(2000);
    pthread_create(&id2, NULL, thread_cont, &offset2);
    delay(2000);
    pthread_create(&id3, NULL, thread_cont, &offset3);
    delay(2000);
    pthread_create(&id3, NULL, thread_cont, &offset3);
    delay(2000);
    pthread_create(&id4, NULL, thread_cont, &offset4);
    delay(2000);


    pthread_join(id1, NULL);
    pthread_join(id2, NULL);
    pthread_join(id3, NULL);
    pthread_join(id4, NULL);


    sem_destroy(&mutex);

    printf("\n");

    return 0;
}

【讨论】:

    猜你喜欢
    • 2014-12-24
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多