c - How to make 4 threads interact with each other? -
i have program 4 threads, @ end should print thread , pipeline (2 cycles) stage. like:
thread 2: stage 1 , 2 thread 3: stage 2 , 3 thread 1: stage 3 , 4 thread 4: stage 4 , 5
but don't know how counter stages because i'm doing can't show stage 1 , 2 each thread, instead of 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); = = clock(); while( (now-then) < pause ) = 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 = 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; }
i think did wrong explaination question. sorry that, think ended right now. 4 threads. 4 instructions, pipeline(maybe) , didn't used mutex, semaphores. btw language barricade has been made more difficult, ask here.
@edit final , maybe right code
#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); = = clock(); while( (now-then) < pause ) = 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 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; }
Comments
Post a Comment