2017-03-27 6 views
0

Ich habe den folgenden Code unten. Ich möchte nur die Hälfte der Threads gleichzeitig in die Threading-Funktion eingeben. Wie erstelle ich einen Semaphor, um die anderen Prozesse zu blockieren? Und wie würde ich die zuvor blockierten Prozesse freigeben, wenn die Threads die Funktion nicht mehr verwenden?Implementierung eines Semaphor

#include <iostream> 
#include <unistd.h> 
#include <sys/wait.h> 
#include <pthread.h> 

using namespace std; 

#define NUM_THREADS 4 

long int sharedcount; 
pthread_mutex_t count_mutex; 

//Function that will be run by multiple threads 
//Needs to return a void pointer and if it takes arguments 
//it needs to be a void pointer 
void *ThreadedFunction(void *threadid) 
{ 
    int success; 
    long id = (long)threadid; 

    //Lock mutex preventing the other threads from ru nning 
    success = pthread_mutex_lock(&count_mutex); 
    cout << "Thread " << id << " beginning.\n"; 
    for(int i = 0; i < 100000000; i++) 
     sharedcount++; 

    cout << "Thread " << id << " exiting.\n"; 
    cout << sharedcount << endl; 

    //Unlock the mutex after the thread has finished running 
    pthread_mutex_unlock(&count_mutex); 

    //Kill the thread 
    pthread_exit(NULL); 
} 

int main() 
{ 
    //Initialize mutex 
    pthread_mutex_init(&count_mutex, NULL); 

    //Create an array of threads 
    pthread_t threads[NUM_THREADS]; 
    int rc; 
    int i; 

    sharedcount = 0; 

    for(i=0; i < NUM_THREADS; i++) 
    { 
     cout << "main() : creating thread, " << i << endl; 

     //Create thread by storing it in a location in the array. Call the 
     //function for the threads to run inside. And pass the argument (if any). 
     //If no arguments pass NULL 
     rc = pthread_create(&threads[i], NULL, ThreadedFunction, (void *)i); 

     if (rc) 
     { 
      cout << "Error:unable to create thread," << rc << endl; 
      exit(-1); 
     } 
    } 

    //Have main thread wait for all other threads to stop running. 
    for(i = 0; i < NUM_THREADS; i++) 
    pthread_join(threads[i], NULL); 

    //cout << sharedcount << endl; 

    pthread_exit(NULL); 
} 
+1

Warum verwenden Sie 'pthread' anstelle von' std :: thread' und 'std :: mutex'? – Xirema

+0

Unser Lehrer hat Pthread in seinem Beispiel benutzt, also habe ich es auch in diesem Projekt benutzt. – Jose

Antwort

1

Was Sie tun können, ist eine Zählung Semaphore verwenden (im Gegensatz zu einem binären Semaphore gegen). Ein Zählsemaphor hat einen Anfangswert größer als 1, was es mehreren Threads erlaubt, auf dem Semaphor "Warten" zu nennen und diese Threads nicht tatsächlich blockiert und in die Semaphor-Warteschlange gesetzt zu haben.

Was ich in Ihrem Fall getan hätte, initialisiert einen Semaphor in der Hauptfunktion mit einem Anfangswert von NUM_THREADS/2. Dann würde ich eine Zeile am Anfang von threadedFunction einfügen, wo ich eine Wartezeit (Semaphore) und eine Zeile am Ende der Funktion, wo Sie ein Signal (Semaphor) tun. Auf diese Weise signalisiert ein Thread, der die Funktion beendet, einen Thread, der blockiert wurde, nachdem er auf den Semaphor gewartet hat, und er lässt diesen Thread ein. Hoffe, das hilft.