Creating Linux Threads in C++
Hello,
I am trying to create a thread in C++, using the LinuxThreads library.
The relative parts of my code are the following:
#include <pthread.h>
...
void *Regular_PA_Thread (void *){
connect_to_PM (3000, hostip, 1); //Call another function
pthread_exit(NULL);
}
void PA_Thread_Create (){
int irc, rc, error;
pthread_t *PAthread = new pthread_t;
pthread_attr_t *PAthreadAttr = new pthread_attr_t;
error = pthread_attr_init(PAthreadAttr);
if (error != 0) printf("Error in Attribute Init\n");
else printf("Attribute Initialized\n");
error = pthread_attr_setstacksize(PAthreadAttr, 16*1024);
if (error != 0) printf("Error in Stack Size Set\n");
else printf("Stack Size Set\n");
error = pthread_attr_setdetachstate(PAthreadAttr, PTHREAD_CREATE_DETACHED);
if (error != 0) printf("Error in State Set\n");
else printf("State Set\n");
error = pthread_create(PAthread, PAthreadAttr, Regular_PA_Thread, NULL);
if (error != 0) printf("Error in Creating the Thread\n");
else printf("Thread Created Succesfully\n");
error = pthread_attr_destroy(PAthreadAttr);
if (error != 0) printf("Error in Destroying the Attribute\n");
else printf("Attribute Destroyed\n");
}
...
The function PA_Thread_Create is called by the main function of my program.
*********************************************************************************************
When I compile/link I get the following link error (compiling is successful):
In function `PA_Thread_Create()':
: undefined reference to `pthread_attr_setstacksize'
collect2: ld returned 1 exit status
*********************************************************************************************
When I leave this part of code out, i.e. use the default attributes:
error = pthread_create(PAthread, NULL, Regular_PA_Thread, NULL);
Compiling and Linking do not fail, but I get a "Segmentation fault" message while executing the program, and more specifically while calling the function pthread_create.
*********************************************************************************************
The generation part of my makefile is the following:
$(BIN_DIR)/PA: $(OBJECTS)
@echo "Linking..."
g++ $(OBJECTS) -o $(BIN_DIR)/PA
$(LIB_DIR)/PA.o: $(SRC_DIR)/PA.cpp
@echo "Compiling PA.cpp..."
g++ -c -o $(LIB_DIR)/PA.o -l $(INC_DIR) $(SRC_DIR)/PA.cpp
*********************************************************************************************
Please note that I am not familiar in linux programing.
Thanks in advance,
George
|