Προς το περιεχόμενο

Condition Variables


jimacos89

Προτεινόμενες αναρτήσεις

Δημοσ.

έχω ένα πρόβλημα όταν χρησιμοποιώ condition variables

 

ορίζω την παρακάτω δομή. Αυτό είναι και που βάζω σε μια λίστα στην συνέχεια.

 

typedef struct tpa

{

pthread_t thread;

void* func;

tcpip_connection* args;

pthread_cond_t cv;

pthread_mutex_t m;

} tpa;

 

 

Σκοπός μου είναι οταν καλώ την παρακάτω συνάρτηση να ελέγχω αν υπάρχουν κοιμησμένα νήματα μέσα στην λίστα μου και αν υπάρχουν να ξυπνάω αυτό που θέλω. Διαφορετικά να δημιουργώ καινούργιο.

 

void Thread_pool_spawn(tcpip_connection* conn )

{

 

pthread_t ListingThread_thread;

 

struct tpa threadp;

struct tpa* threadp2;

 

int rc;

 

/* Case we have an empty list (No sleeped theads) */

if(List_empty(&threadList))

{

 

threadp.thread=ListingThread_thread;

 

threadp.func=ListingThread;

 

threadp.args=OBJDUP(* conn);

 

pthread_mutex_init(&(threadp.m),NULL);

pthread_cond_init(&(threadp.cv),NULL);

 

printf("C %p C\n", &(threadp.cv));

 

count++;

/* Creation of the thread of Listing Thread Function */

rc = pthread_create(&(threadp.thread), NULL,

(thread_proc) threadp.func, &threadp);

 

if(rc) FATAL_PERROR(rc);

}

else

{

/* Case we have sleeping threads. We awake the thread depending

* Condition Variables */

(void)pthread_mutex_lock(&m_all);

 

threadp2=List_pop_front(&threadList);

 

printf("B %p B\n", &(threadp2->cv));

(void)pthread_mutex_unlock(&m_all);

 

//printf("A %p A\n", &(threadp.cv));

pthread_cond_broadcast(&(threadp2->cv));

}

}

 

 

 

Στην συνέχεια αφού δημιουργώ thread αυτής της συνάρτησης θέλω να εκτέλω την διεργασία μου και στην συνέχεια αμα έχω πάνω από 10 νήματα να σκοτώνω το εκάστοτε νήμα διαφορετίκα να το κοιμίζω και να το βάζω μεσα σε μια λίστα.

 

Και τώρα ερχόμαστε στο πρόβλημα. Οταν το βάζω στην λιστα το condition variable που το κοιμήσα έχει μια τιμή. Οταν όμως βγάλω καάτι απο την λίστα αλλάζει η τιμή του condition variable με αποτέλεσμα να μην μπορώ να ξυπνήσω κάποιο νήμα που κοιμάται. για αυτό το λόγο έβαλα και τις printf .

 

 

void ListingThread(tpa* values)

{

tcpip_connection* conn;

struct tpa* threadp2;

struct tpa* tpa1;

 

 

while(1)

{

conn=values->args;

/* Run server_handler */

server_handler(conn , values->m);

 

/* Kill the thread if there are more than 10*/

if (count>10)

{

(void)pthread_mutex_lock(&values->m);

count--;

(void)pthread_mutex_unlock(&values->m);

break;

}

else

{

/* We lock the mutexes so no one else has access in the list */

(void)pthread_mutex_lock(&values->m);

(void)pthread_mutex_lock(&m_all);

 

tpa1 = Malloc(sizeof(struct tpa));

 

 

tpa1->args=values->args;

tpa1->cv=values->cv;

tpa1->m=values->m;

tpa1->func=values->func;

tpa1->thread=values->thread;

 

/*tpa1.args=values->args;

tpa1.cv=values->cv;

tpa1.m=values->m;

tpa1.func=values->func;

tpa1.thread=values->thread;*/

 

/* We push the thread in the List*/

List_push_back(&threadList,tpa1);

 

printf("TL %p TL\n", &(values->cv));

(void)pthread_mutex_unlock(&m_all);

 

threadp2 = Malloc(sizeof(struct tpa));

threadp2 = List_pop_back(&threadList);

printf("EE %p EE\n", &(threadp2->cv));

 

puts("\n\n");

pthread_cond_wait(&(values->cv),&(values->m));

(void)pthread_mutex_unlock(&values->m);

}

}

}

 

Καμία ιδέα στο πώς θα μπορέσω να ξυπνήσω τα νήματα μου?

επίσης ο λόγος που βγάζω κάτι απο την λίστα αφου το βάλω είναι για να ελένξω κατευθείαν αν είναι ίδια η τιμή. όμως δυστηχως δεν είναι. Επίσης κάτι άλλο που πάρατηρησα είναι οτι κάθε φόρα που δημιουργώ ενα νήμα αυτό εχει την ιδια διεύθυνση για το condition variable ενώ κάθε φορά που βγάζω κάτι απο την λίστα έχει διαφορετικό.

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...