La descarga está en progreso. Por favor, espere

La descarga está en progreso. Por favor, espere

Programming with POSIX* Threads Intel Software College.

Presentaciones similares


Presentación del tema: "Programming with POSIX* Threads Intel Software College."— Transcripción de la presentación:

1 Programming with POSIX* Threads Intel Software College

2 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 2 Programming with POSIX* Threads Objectivos Explorar las funciones de la librería Pthreads para crear y sincronozar hilos

3 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 3 Programming with POSIX* Threads ¿Qué son los pthreads? Estándar POSIX.1c Interface en lenguaje C Los hilos existen dentro del mismo proceso Todos los hilos son pares No es un modelo explícito padre-hijo Excepto: “main thread” contiene toda la información del proceso

4 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 4 Programming with POSIX* Threads pthread_create int pthread_create(tid, attr, function, arg); pthread_t *tid descriptor del hilo creado const pthread_attr_t *attr atributos del hilo a crearse void *(*function)(void *) función que será mapeada al hilo void *arg argumento que se envía a la función

5 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 5 Programming with POSIX* Threads pthread_create Detalles Inicia un hilo ejecutando la función Descriptor del hilo retornado por medio de la estructura pthread_t Especifica NULL para usar los atributos por default Un solo argumento enviado a la función Si no tiene argumentos, especifica NULL Verificar códigos de error! EAGAIN – recursos insuficientes para crear el hilo EINVAL – atributo inválido

6 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 6 Programming with POSIX* Threads Eejemplo: Creación del hilo #include void *hello (void * arg) { printf(“Hello Thread\n”); } main() { pthread_t tid; pthread_create(&tid, NULL, hello, NULL); } ¿Qué sucede?

7 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 7 Programming with POSIX* Threads Esperando un Thread int pthread_join(tid, val_ptr); pthread_t tid manejador de un hilo a esperar void **val_ptr valor de salida devuelto por un hilo

8 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 8 Programming with POSIX* Threads pthread_join Detalles Un hilo espera a que un hilo con descriptor tid termine Solo espera a que un hilo se una El hilo debe ser unible Un valor de salida se devuelve del hilo unido Tipo devuelto es (void *) Usar NULL si no se espera un valor de retorno ESRCH - hilo (pthread_t) no encontrad EINVAL - hilo (pthread_t) no unible

9 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 9 Programming with POSIX* Threads Estados de un hilo Los hilos Pthread tienen dos estados Unible (joinable) Desacoplado (detached) Por default los hilos son unibles Los recursos se mantienen hasta el pthread_join Pueden ser reseteados con atributos o una llamada API Los hilos desacoplados no pueden unirse Los recursos pueden reclamarse en la terminación No se pueden resetear a ser unibles

10 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 10 Programming with POSIX* Threads Ejemplo: Múltiples Hilos #include #define NUM_THREADS 4 void *hello (void *arg) { printf(“Hello Thread\n”); } main() { pthread_t tid[NUM_THREADS]; for (int i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i], NULL, hello, NULL); for (int i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); }

11 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 11 Programming with POSIX* Threads ¿Qué falla? ¿Qué se muesta de myNum? void *threadFunc(void *) { void *threadFunc(void *pArg) { int* p = (int*)pArg; int* p = (int*)pArg; int myNum = *p; printf( “Thread number %d\n”, myNum);}... // from main(): for (int i = 0; i < numThreads; i++) { pthread_create(&tid[i], NULL, threadFunc, &i); pthread_create(&tid[i], NULL, threadFunc, &i);}

12 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 12 Programming with POSIX* Threads for(i=0;i<THREADS;i++) { param=i; res = pthread_create(&tid[i], NULL, thread, (void *) &param); if (res != 0) { perror("falló la creación del hilo"); exit(10); } void *thread(void *arg) { int *params; int t; params=(int *) arg; t=*params; printf(“t = %d\n",t); pthread_exit(NULL); } param=0x0001000 i=0x0001004 arg=0x0001008 params=0x000100C t=0x0001010 0 0 0x0001000 1 1 1

13 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 13 Programming with POSIX* Threads Solución – Almacenamiento “Local” void *threadFunc(void *) void *threadFunc(void *pArg){ (int*)pArg) int myNum = *((int*)pArg); printf( “Thread number %d\n”, myNum);}... // from main(): for (int i = 0; i < numThreads; i++) { tNum[i] = i; tNum[i] = i; pthread_create(&tid[i], NULL, threadFunc, &tNum[i]); pthread_create(&tid[i], NULL, threadFunc, &tNum[i]);}

14 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 14 Programming with POSIX* Threads Pthreads Variables Mutex Simple, flexible, y eficiente Permiten estructuras de programación correctas para evitar condiciones de concurso Nuevos tipos pthread_mutex_t Variable mutex pthread_mutexattr_t Atributos de mutex Antes de usar, mutex debe ser inicializada

15 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 15 Programming with POSIX* Threads pthread_mutex_init int pthread_mutex_init( mutex, attr ); pthread_mutex_t *mutex mutex a ser inicializada const pthread_mutexattr_t *attr atributos a ser establecidos a mutex ENOMEM – memoria insuficiente para mutex EAGAIN – recursos insuficientes (otros que no son memoria) EPERM - no privilegios para ejecutar la operación

16 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 16 Programming with POSIX* Threads Inicialización Alternativa Puede usarse el inicializador estático PTHREAD_MUTEX_INITIALIZER Usa los atributos por default El programador debe poner atención al alcance de los mutex Deben ser visibles a los hilos pthread_mutex_t mtx1 = PTHREAD_MUTEX_INITIALIZER;

17 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 17 Programming with POSIX* Threads pthread_mutex_lock int pthread_mutex_lock( mutex ); pthread_mutex_t *mutex mutex que intenta hacer el lock

18 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 18 Programming with POSIX* Threads pthread_mutex_lock Detalles Intenta hacer el lock del mutex Si el mutex está bloqueado por otro hilo, el hilo que llama al lock se bloquea Mutex es detenido por el hilo que lo llama hasta que se desbloquea Mutex lock/unlock deben ser pares, si no puede ocurrir un interbloqueo EINVAL - mutex invalido EDEADLK – el hilo llamante ya es dueño del mutex

19 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 19 Programming with POSIX* Threads pthread_mutex_unlock int pthread_mutex_unlock( mutex ); pthread_mutex_t *mutex mutex a ser desbloqueado EINVAL - mutex es inválido EPERM - el hilo llamante no es dueño del mutex

20 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 20 Programming with POSIX* Threads Exjemplo: Uso del mutex #define NUMTHREADS 4 pthread_mutex_t gMutex; // ¿porque debe ser global? int g_sum = 0; void *threadFunc(void *arg) { int mySum = bigComputation(); pthread_mutex_lock( &gMutex ); g_sum += mySum; // los hilos acceden uno a la vez pthread_mutex_unlock( &gMutex ); } main() { pthread_t hThread[NUMTHREADS]; pthread_mutex_init( &gMutex, NULL ); for (int i = 0; i < NUMTHREADS; i++) pthread_create(&hThread[i],NULL,threadFunc,NULL); for (int i = 0; i < NUMTHREADS; i++) pthread_join(hThread[i]); printf (“Global sum = %f\n”, g_sum); }

21 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 21 Programming with POSIX* Threads Variables Condición Los semáforos son condicionales en el contador del semáforo Las variables condición están asociadas con una condición arbitraria Las mismas operaciones: wait y signal Proveen exclusión mutua

22 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 22 Programming with POSIX* Threads Variable condición y Mutex Un mutex está asociado con una variable condición Protege la evaluación de la expresión condicional Previene condiciones de concurso entre los hilos que están enviando la señal y los hilos que esperan en la variable condición

23 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 23 Programming with POSIX* Threads Señales perdidas y falsas Una señal a una variable de condición no se guarda Si no hay un hilo esperando, la señal se pierde Un hilo puede interbloquearse esperando una señal que no será enviada Una variable de condición (rara vez) puede recibir señales falsas Si las señales se hacen predecibles la ejecución es más lenta Se requiere volver a probar la expresión de condición

24 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 24 Programming with POSIX* Threads Algoritmo de variables de condición Evita problemas con señales perdidas y señales falsas adquiere mutex; while (condición es verdadera) espera en la variable de condición; espera en la variable de condición; ejecuta región crítica; actualiza condición; envía señal a los hilos que esperan; libera mutex; Se niega la condición se necesita proceder El mutex se libera automáticamente cuando el hilo espera Puede ser opcional

25 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 25 Programming with POSIX* Threads Variables Condición pthread_cond_init, pthread_cond_destroy Inicializa/destruye variables de condición pthread_cond_wait El hilo duerme hasta que se efectúa un signal a la variable de condición pthread_cond_signal Señal que libera la variable de condición pthread_cond_broadcast Broadcast que libera la variable de condición

26 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 26 Programming with POSIX* Threads Tipos de variables de condición Tipos de datos usados pthread_cond_t La variable de condición pthread_condattr_t Atributos de la variable de condición Antes de usar, la variable condición (y el mutex) deben inicializarse

27 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 27 Programming with POSIX* Threads pthread_cond_init int pthread_cond_init( cond, attr ); pthread_cond_t *cond variable condición a ser inicializada const pthread_condattr_t *attr attributosa ser establecidos en la variable de conidición ENOMEM - insuficiente memoria para la variable condición EAGAIN - insuficientes recursos (diferentes a la memoria) EBUSY - variable de condición ya inicializada EINVAL - attr inválido

28 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 28 Programming with POSIX* Threads Inicialización alternativa Puede usuarse un inicializador estático PTHREAD_COND_INITIALIZER Usa los atributos por default Los programadores deben estar atentos a la condición y alcance (del mutex) Debe ser visible a los hilos pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;

29 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 29 Programming with POSIX* Threads pthread_cond_wait int pthread_cond_wait( cond, mutex ); pthread_cond_t *cond Variable condición a esperar pthread_mutex_t *mutex Debe desbloquearse

30 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 30 Programming with POSIX* Threads pthread_cond_wait Detalles El hilo se bloquea esperando una señal en cond El mutex se desbloquea Permite que otros hilos adquiran el lock Cuando llega una señal, el mutex será readquirido antes de salir del pthread_cond_wait EINVAL - cond o mutex inválido EINVAL - mutex diferentes para esperaas concurrentes EINVAL - el hilo llamante no es dueño del mutex

31 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 31 Programming with POSIX* Threads pthread_cond_signal int pthread_cond_signal( cond ); pthread_cond_t *cond Variable condición a la que se le enviará la señal

32 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 32 Programming with POSIX* Threads pthread_cond_signal Detalles Envía una señal a una variable condición, desbloquea un hilo bloqueado Si no hay hilos esperando, no se toma ninguna acción La señal no se guarda para futuros hilos El hilo que envía la señal no requiere tener el mutex Puede ser más eficiente Puede haber problemas si se usan prioridades de hilos EINVAL - cond es inválida

33 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 33 Programming with POSIX* Threads pthread_cond_broadcast int pthread_cond_broadcast( cond ); pthread_cond_t *cond variable condición a recibir la señal

34 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 34 Programming with POSIX* Threads pthread_cond_broadcast detalles Desbloquea todos los hilos que están esperando una variable de condición Si no hay hilos esperando, no se toma ninguna acción Broadcast no se almacena para hilos futuros El hilo que envía la señal no necesita tener el mutex EINVAL - cond is inválida

35 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 35 Programming with POSIX* Threads Programando con hilos POSIX* Temas cubiertos Como crear hilos que hagan trabajo encapsulado dentro de las funciones Coordinar acceso compartido entre hilos para evitar condiciones de concurso

36 Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 36 Programming with POSIX* Threads


Descargar ppt "Programming with POSIX* Threads Intel Software College."

Presentaciones similares


Anuncios Google