pthread.h

the correct use of pthread_create and pthread_join

Page 1 of 1

4 Replies - 9000 Views - Last Post: 06 June 2008 - 08:14 AM Rate Topic: -----

#1 Beca   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-June 08

pthread.h

Posted 05 June 2008 - 02:15 PM

Hello everybody, I’m new using PthreadsVC2.dll and I have some problems with the following code. I hope someone can help me. Thank you very much.

  // 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*)&param[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

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: pthread.h

#2 skater_00   User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 257
  • Joined: 30-April 08

Re: pthread.h

Posted 05 June 2008 - 03:05 PM

To question 1: void* functions simply require a return value, void functions don't. That's just the way it is.

You can check this easily:

The void* function below will give you the compile error saying it needs a return value.

void* Test();

int main()
{
	return 0;
}

void* Test()
{

}



The void function below won't.

void Test();

int main()
{
	return 0;
}

void Test()
{

}



void* != void

Hope this helps you enough about your first question.

------
EDIT:
------

To question 2: Wouldn't the "funcion_thr" function in the line below need a parameter passed to it?

pthread_create(&tabla_thr[i], NULL, funcion_thr, (void*)&param[i]);



I think it probably needs a parameter passed to it considering the prototype of the "funcion_thr" function is:

void* funcion_thr(void* parm);



For now, that's the one thing that caught my eye immediately. However, I'm absolutely not sure about this.. I'm not able to test anything at the moment.

This post has been edited by skater_00: 05 June 2008 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 perfectly.insane   User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: pthread.h

Posted 05 June 2008 - 04:37 PM

The call to pthread_join is suspect here. The second parameter should be passed as void**, but you should not use one directly. Example:

void* value_ptr;
pthread_join(thread, &value_ptr);

The ampersand makes it a void**. In C, the reference notation is not supported, so passing by reference is implemented using pointers. Since the value passed to pthread_exit is a void*, a pointer to this would be a void**. The pointer needs to be valid, or it needs to be NULL. Passing an uninitialized stack variable will likely cause it to crash.

Edit: I see that your argument is an int*. You could simply add an ampersand to that, and it should work correctly.
pthread_join(tabla_thr[i], (void**)&res);

This post has been edited by perfectly.insane: 05 June 2008 - 04:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 Beca   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-June 08

Re: pthread.h

Posted 06 June 2008 - 08:01 AM

View Postskater_00, on 5 Jun, 2008 - 03:05 PM, said:

To question 1: void* functions simply require a return value, void functions don't. That's just the way it is.

You can check this easily:

The void* function below will give you the compile error saying it needs a return value.

void* Test();

int main()
{
	return 0;
}

void* Test()
{

}



The void function below won't.

void Test();

int main()
{
	return 0;
}

void Test()
{

}



void* != void

Hope this helps you enough about your first question.

------
EDIT:
------

To question 2: Wouldn't the "funcion_thr" function in the line below need a parameter passed to it?

pthread_create(&tabla_thr[i], NULL, funcion_thr, (void*)&param[i]);



I think it probably needs a parameter passed to it considering the prototype of the "funcion_thr" function is:

void* funcion_thr(void* parm);



For now, that's the one thing that caught my eye immediately. However, I'm absolutely not sure about this.. I'm not able to test anything at the moment.


Thank you very much skater_00, now I have clear the use of void* and void.
Was This Post Helpful? 0
  • +
  • -

#5 Beca   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 05-June 08

Re: pthread.h

Posted 06 June 2008 - 08:14 AM

Thank you very much 'perfectly.insane' , I used your suggestions and I could run the code without problems, but I have noticed that the threads were created in a disorder way.
Something that cacht my attention is that the thread5 was created twice, can you tell me something about it? please.

Look the attach file, please.

Thank you very much!!!

Best Regards

Beca

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1