proble bei thread

Disclaimer: Dieser Thread wurde aus dem alten Forum importiert. Daher werden eventuell nicht alle Formatierungen richtig angezeigt. Der ursprüngliche Thread beginnt im zweiten Post dieses Threads.

proble bei thread
thread1 gibt ‘+’ aus, thread2 gibt ‘-’ aus.
wie kann man den code ändern ,das die ausgabe “±±±±±±±+” so aussieht :finger:

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

static pthread_cond_t cond;
static pthread_mutex_t mutex;
int count = 1;

void P(int *sem)
{
pthread_mutex_lock(&mutex);
while(*sem == 0) {
pthread_cond_wait(&cond, &mutex);
}
(*sem)–;

pthread_mutex_unlock(&mutex);

}

void V(int *sem)
{
pthread_mutex_lock(&mutex);
(*sem)++;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
}

static void *thread1 (void *arg) {
int i;
printf(“thread1 start\n”);
for (i = 0; i < 10; i++) {

	P(&count);
	printf("+");
	V(&count);
}
printf("\n");
pthread_exit ((void *) 0);

}

static void *thread2 (void *arg) {
int i;
printf(“thread2 start\n”);
for (i = 0; i < 10; i++) {
P(&count);
printf(“-”);
V(&count);
}
printf(“\n”);
pthread_exit ((void *) 0);
}

int main (void) {
pthread_t th[2];
pthread_cond_init (&cond, NULL);
pthread_mutex_init (&mutex, NULL);

pthread_create (&th[0], NULL, thread1, NULL);
pthread_create (&th[1], NULL, thread2, NULL);	

pthread_join (th[0], NULL);
pthread_join (th[1], NULL);
return EXIT_SUCCESS;

}


Du fuehrst 2 zaehlende Semaphoren ein mit folgender Bedeutung:

Sem1: soviel ‘+’ duerfen momentan geschrieben werden.
Sem2: soviel ‘-’ duerfen momentan geschrieben werden.

Initialisieren tust du Sem1 mit 1 und Sem2 mit 0.

Die Threads versuchen immer die Semaphore fuer das Zeichen zu erniedrigen, dass sie schreiben wollen und setzen danach die andere Semaphore eins hoch.


ich habe nicht verstanden.
die ausgabe sieht moment so aus “+++++++++±---------”


Hallo Kai!

Obwohl dein Beitrag auch ein Hilfegesucht war, hat er mir sehr geholfen (ansonsten würde ich mir immer noch die Zähne ausbeisen an dem Threadgeraffel). Ich habe auch etwas gegrübelt, was frahi da meint, aber es ist so gemeint: Was frahi da von sich gibt, ist das, was du machen sollst. Du hast nur eine Semaphore namens count, du brauchst aber zwei. Eine für + und eine für -.

Das sollte so funzen:

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

static pthread_cond_t cond;
static pthread_mutex_t mutex;

int pluscount = 1;
int minuscount = 0;

void P(int *sem)
{
    pthread_mutex_lock(&mutex);
    while(*sem == 0) {
        pthread_cond_wait(&cond, &mutex);
    }
    (*sem)--;

    pthread_mutex_unlock(&mutex);
}

void V(int *sem)
{
    pthread_mutex_lock(&mutex);
    (*sem)++;
    pthread_cond_broadcast(&cond);
    pthread_mutex_unlock(&mutex);   
}

static void *thread1 (void *arg) {
    int i;   
    printf("thread1 start\n");
    for (i = 0; i < 10; i++) {
       
        P(&pluscount);
        printf("+");
        V(&minuscount);
    }
    printf("\n");
    pthread_exit ((void *) 0);
}


static void *thread2 (void *arg) {
    int i;
    printf("thread2 start\n");
    for (i = 0; i < 10; i++) {
        P(&minuscount);
        printf("-");
        V(&pluscount);
    }
    printf("\n");
    pthread_exit ((void *) 0);
}

int main (void) {
    pthread_t th[2];
    pthread_cond_init (&cond, NULL);
    pthread_mutex_init (&mutex, NULL);
       
    pthread_create (&th[0], NULL, thread1, NULL);
    pthread_create (&th[1], NULL, thread2, NULL);   
   
    pthread_join (th[0], NULL);
    pthread_join (th[1], NULL);
    return EXIT_SUCCESS;
}

Gruß,
Sebastian