LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2005, 11:42 AM   #1
George_gk
LQ Newbie
 
Registered: Jan 2005
Location: Athens
Posts: 11

Rep: Reputation: 0
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
 
Old 01-26-2005, 01:02 PM   #2
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
If I remember correctly, you need to explicitly link the pthread library into your code (via -lpthread) otherwise you'll get lots of problems. I'm not sure if this is a linux specific thing, or if it's some kind of standard (ie no matter what the platform you MUST use -lpthread) but I'm not sure.

-Aaron
 
Old 01-27-2005, 03:29 AM   #3
George_gk
LQ Newbie
 
Registered: Jan 2005
Location: Athens
Posts: 11

Original Poster
Rep: Reputation: 0
Aaron,

I would like to thank you very much for your help.
After adding the option -lpthread, it is working fine now.

I am not familiar with linux programming, so I am unaware of things that sound easy for you!!!

Anyway, thanks again,
George
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Threads lucky6969b Linux - Software 2 12-02-2005 01:44 AM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM
Creating a milisecond timeout timer for threads cardias Programming 5 08-05-2004 07:18 AM
Problems creating a milisecond timeout timer for threads cardias Linux - Software 1 08-04-2004 03:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration