problem handling POSIX thread on FreeBSD
Pablo Mora
fbsd.hackers at gmail.com
Thu Jun 30 05:06:37 GMT 2005
>Not sure I understand the question. What do you mean by S.O?
, sorry by my badly english, the correct word is O.S (operative system).
>you saying that since the threads are POSIX, that you would >expect
the program to act the same on all Operating Systems?
exactly, that thought before your answer. I thought that a same code
was executed of the same form so much in Solaris, in GNU/Linux and
FreeBSD. At least than had shown the same results.
really I do not know because Linux and solaris they show me:
hilo1: puntero contiene 0
hilo2: puntero contiene 0
hilo1: puntero contiene 0
hilo2: puntero contiene 3
hilo1: puntero contiene 2
hilo2: puntero contiene 6
hilo1: puntero contiene 4
hilo2: puntero contiene 9
hilo1: puntero contiene 6
hilo2: puntero contiene 12
hilo1: puntero contiene 8
hilo2: puntero contiene 15
hilo1: puntero contiene 10
hilo2: puntero contiene 18
hilo1: puntero contiene 12
hilo2: puntero contiene 21
hilo1: puntero contiene 14
hilo2: puntero contiene 24
hilo1: puntero contiene 16
finaliza hilo1 con id 1082231728
hilo2: puntero contiene 27
finaliza hilo2 con id 1090624432
fin hilo 2
sadly in my university we work with Solaris:' (
--------------------------------------------------------------------------------------------------
I repeat part of the code:
/* file: test.c */
....
#include<pthread.h>
char buffer[512];
pthread_mutex_t mutex, mutex2;
pthread_t hilo1, hilo2;
void f1(void* ptr)
{
int i,n=10;
int valor=0;
char*p=(char*)ptr;
for(i=0;i<n;i++)
{
pthread_mutex_lock(&mutex);
sscanf(p,"%d",&valor);
printf("\thilo1: puntero contiene %d\n", valor);
valor=i*3;
sprintf(p, "%d",valor);
pthread_mutex_unlock(&mutex2);
}
valor=(int)pthread_self();
printf("finaliza hilo1 con id %d\n", valor);
pthread_exit(&valor);
}
void f2(void* ptr)
{
int i,n=10;
int valor=0;
char*p=(char*)ptr;
for(i=0;i<n;i++)
{
pthread_mutex_lock(&mutex2);
sscanf(p,"%d",&valor);
printf("hilo2: puntero contiene %d\n", valor);
valor=i*2;
sprintf(p, "%d",valor);
pthread_mutex_unlock(&mutex);
}
valor=(int)pthread_self();
printf("finaliza hilo2 con id %d\n", valor);
pthread_exit(&valor);
}
int main()
{
pthread_attr_t atributos;
memset(buffer, '\0', sizeof(buffer));
pthread_mutex_init(&mutex, NULL); //linux
pthread_mutex_init(&mutex2, NULL); //linux
pthread_mutex_lock(&mutex2); /* ¿? */
if(pthread_attr_init(&atributos) < 0)
{
perror("pthread_attr_init");
exit(-1);
}
if(pthread_attr_setscope(&atributos,PTHREAD_SCOPE_PROCESS) < 0)
{
perror("pthread_attr_setscope");
exit(-2);
}
if(pthread_create (&hilo1, &atributos, (void*)&f1, (void*)buffer)<0)
{
perror("pthread_create hilo1");
exit(-2);
}
if(pthread_create(&hilo2, &atributos, (void*)&f2, (void*)buffer)<0)
{
perror("pthread_create hilo2");
exit(-2);
}
.....
}
you believe that a mutex not necessarily must be unlocked by the same
thread that locks it?
sorry but my badly english, but I am making an effort to me so that
you manage to understand to me.
--
Concepción, Chile.
More information about the freebsd-hackers
mailing list