|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Sep 2009
Messaggi: 18
|
[C]Pthread e Mutex sempre in deadlock
salve a tutti, ho fatto un programma con thread POSIX ma non so perchè anche usando il MUTEX credo mi vadi in deadlock, qualcuno ha riscontrato un errore?
Codice:
#include<iostream>
#include<pthread.h>
using namespace std;
int valore1=100;
pthread_mutex_t blocco;
void*funzione(void*arg)
{
pthread_mutex_lock(&blocco);
valore1=valore1-50;
cout<<valore1;
pthread_mutex_unlock(&blocco);
}
void*funzione2(void*arg)
{
pthread_mutex_lock(&blocco);
valore1=valore1-50;
cout<<valore1;
pthread_mutex_unlock(&blocco);
}
int main(void)
{
pthread_t t1;
pthread_t t2;
pthread_mutex_init(&blocco,NULL);
pthread_create(&t1,NULL,&funzione,NULL);
pthread_create(&t2,NULL,&funzione2,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return 0;
}
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Il programma funziona correttamente. Che cosa fa e cosa ti aspetti come risultato invece ?
Se hai il dubbio che sia in deadlock (cosa che non e') puoi utilizzare il debugger per verificare.
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Sep 2009
Messaggi: 18
|
è normale che si impalli il compilatore??? ho riavviato il pc e adesso funziona...
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
direi proprio di no, il problema penso sia altrove.
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
#5 | |
|
Senior Member
Iscritto dal: May 2001
Messaggi: 12904
|
Quote:
a) che compilatore usi? b) usi qualche IDE in particolare? c) che distribuzione usi? d) che linea di comando passi al compilatore? |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 18:32.




















