Un ejemplo simple de pthread. Un hilo debe ir asociado a una funcion. Una funcion c se puede pasar como un parametro mas.
/**
* hilos.c
* Pello Xabier Altadill Izura
* un ejemplo simplon de hilos linux
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
void haz_algo(int *);
void haz_algo_simple(int *);
int h1 = 0, h2 = 0;
extern int
main(void)
{
pthread_t thread1, thread2;
printf("vamos a ver...
");
// creamos el primer
if (pthread_create(&thread1, NULL, (void *) haz_algo, (void *) &h1) != 0)
perror("pthread_create"), exit(1);
// creamos el segundo hilo
if (pthread_create(&thread2, NULL,(void *) haz_algo_simple, (void *) &h2) != 0)
perror("pthread_create"), exit(1);
// esperamos que acabe
if (pthread_join(thread1, NULL) != 0)
perror("pthread_join"),exit(1);
printf("
OK!a
");
return 0;
}
// funcion haz_algo
void haz_algo(int *veces)
{
int i;
for (i = 0; i < veces; i++)
printf("Aqui, haciendo algo: %d
",i);
}
//funcion haz_algo_simple
void haz_algo_simple(int *veces)
{
int i;
for (i = 0; i < veces; i++)
printf("Aqui, haciendo algo simple: %d
",i);
}
7 comments |
PELMAlink |
viagra asdasdas viagra online fsde cheap viagra fsde buy viagra fsde viagra 665445 viagra 1232 viagra 1232 viagra online without prescription 553434 viagra online without prescription 65456 buy cialis 9876 cialis 1232 cialis 1232 cialis 1232 viagra 1232 generic viagra 1232 buy cialis gdfgdfg cialis 5345 cialis 543543 phentermine without prescription 66544 phentermine without prescription 7766 cialis 5345 cialis 543543 cheap viagra fsde viagra online without prescription asdasdas viagra