Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- #include <cstdlib>
- #include <pthread.h>
- #include <sys/types.h>
- #include <csignal>
- using namespace std;
- int n;
- int c=0;
- pthread_mutex_t monitor;
- pthread_cond_t red;
- void prekid(int sig){
- pthread_mutex_destroy(&monitor);
- pthread_cond_destroy(&red);
- kill(getpid(),SIGKILL);
- }
- void *dretva(void *x){
- int tid = (*(int*)x);
- int a;
- pthread_mutex_lock(&monitor);
- c++;
- cout << "Dretva: " << tid << ", unesite broj: ";
- cin >> a;
- if (c<n){
- pthread_cond_wait(&red, &monitor);
- }
- else{
- pthread_cond_broadcast(&red);
- }
- pthread_mutex_unlock(&monitor);
- pthread_mutex_lock(&monitor);
- cout << "Dretva: " << tid << ", unesen je broj: " << a << endl;
- pthread_mutex_unlock(&monitor);
- }
- int main(int argc, char** argv){
- int j[n];
- if (argc==2){
- n=atoi(argv[1]);
- }
- else return (0);
- pthread_t d[n];
- pthread_mutex_init(&monitor, NULL);
- pthread_cond_init(&red, NULL);
- sigset(SIGINT,prekid);
- for (int i=0;i<n;i++){
- j[i]=i+1;
- pthread_create(&d[i], NULL, dretva, &j[i]);
- }
- for (int i=0;i<n;i++){
- pthread_join(d[i],NULL);
- }
- pthread_mutex_destroy(&monitor);
- pthread_cond_destroy(&red);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement