Forum Discussion
Altera_Forum
Honored Contributor
21 years agoHi Ken/Wentao
Here below is the code I use. I have noticed that the kernel also reboots if I call the func "pthread_attr_getstacksize(&attr, &stacksize);" The "pthread_attr_init(&attr);" called before the "pthread_attr_getstacksize" call apperently gets called correct. I have modified the "pthread_create" function a bit(have made alot of printf's). But all these printf's don't get printed(have usleep(1000000) after each printf so they have time to get printed) I am thinking this might be a memory problem. Perhaps something is not allocated as it should be, but I am not sure.#include <stdio.h># include <stdlib.h># include <pthread.h># include <unistd.h>
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
return (NULL);
}
int main()
{
pthread_t thread1, thread2;
pthread_attr_t attr;
size_t stacksize;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
//Will try to allocate some memory to see if this is possible
void **newstack;
printf("\n##################################\n");
printf("Will now allocate 8096 bytes\n");
newstack = (void **) malloc(8096);
printf("\n**********************************\n");
printf("8096 bytes allocated\n");
//Just give some time to the UART to printf the messages.
usleep(1000000);
/* Create independant threads each of which will execute function */
printf("Will now create threads!!!\n");
//Just give some time to the UART to printf the messages.
usleep(1000000);
//Attributes for the thread.
pthread_attr_init(&attr);
printf("1!\n");
//Just give some time to the UART to printf the messages.
usleep(1000000);
//Get the stacksize of the thread.
//pthread_attr_getstacksize(&attr, &stacksize);
//printf("Stacksize = %d\n", stacksize);
//Just give some time to the UART to printf the messages.
usleep(1000000);
//Setting the stack size
//pthread_attr_setstacksize(&attr, 1000);
//Creating the thread.
pthread_create(&thread1, &attr, print_message_function, (void *)message1);
//iret1 = pthread_create( &thread1, NULL, print_message_function, NULL);
printf("Thread One created!!!\n");
//Just give some time to the UART to printf the messages.
usleep(1000000);
//Creating a thread with standard attributes "NULL"
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
return 1;
} Regards GreateWhite.DK