// PPtreads01.cpp : Defines the entry point for the console application.
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define el numero maximo de hilos
#define MAX_THREADS 10
// tabla con los identificadores de los threads
pthread_t tabla_thr[MAX_THREADS];
// tipo de datos y tabla con los parametros
typedef struct
{
int id;
char* cadena;
}thr_param_t;
thr_param_t param[MAX_THREADS];
// La siguiente funcion es que la ejecutan los threads
void* funcion_thr(void* parm)
{
thr_param_t* p = (thr_param_t*)parm;
printf("%s%d\n",p->cadena, p->id);
pthread_exit(&(p->id));
return NULL; // QUESTION NUMBER 1
}
int main()
{
int i, **res;
// creamos los threads
printf("Creando threads...\n");
for(i=0; i<MAX_THREADS; i++)
{
printf("contando \n");
param[i].cadena = strdup("Hola, soy el thread");
param[i].id = i;
pthread_create(&tabla_thr[i], NULL, funcion_thr, (void*)¶m[i]);
}
printf("Threads creados. Esperando que terminen \n");
for(i=0; i<MAX_THREADS; i++)
{
pthread_join(tabla_thr[i], (void**)res);
printf("El thread %d devolvio el valor %d\n",i,*res);
}
// sacamos el mensajito y salimos del programa
printf("Todos los threads finalizados. Adios!\n");
return 0;
}
QUESTIONS
1. If I don’t write the code line “return NULL” in the function “void* funcion_thr(void* parm)” I get the following error line:
…:error C4716:’funcion_thr’:must return a value
The question is: Why need the function ’funcion_thr’ return a value if it’s void*?
2. When I run the code I get the following result:
Contando threads
Contando
Contando
Contando
Contando
Contando
Contando
Contando
Hola, soy el thread0
Hola, soy el thread1
Hola, soy el thread2
Hola, soy el thread3
Hola, soy el thread4
Hola, soy el thread5
Hola, soy el thread6
Hola, soy el thread7
Contando
Contando
Threads creados, Esperando que terminen
Hola, soy el thread8
Hola, soy el thread9
And finally appear a pop-up window, it said:
¨PPthreads01.exe has detected a problem and should be closing….¨
Please try to help me, maybe my mistakes are simple but how I wrote at the beginning I’m new in this topic.
In
Thank you very much.
Best regards
Beca

New Topic/Question
Reply



MultiQuote





|