Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Threading
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 05-16-2008   #1 (permalink)
martin910
Just Joined!
 
Join Date: May 2008
Posts: 3
Threading

hi,
i am developed a number of problems while implementing a program that creates threads.

int status=pthread_create (&x2[i],x1,startMain,(void*)i);

Error shown:
In function `main':
threading.c.text+0x6b): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

Can anybody help me? i would liked to be briefed regarding multithreading.

Thanks
martin910 is offline   Reply With Quote
Old 05-16-2008   #2 (permalink)
digvijay.gahlot
Just Joined!
 
Join Date: Mar 2008
Location: Hyderabad
Posts: 88
Have you included the header
<pthread.h>
digvijay.gahlot is offline   Reply With Quote
Old 05-16-2008   #3 (permalink)
Javasnob
Linux Engineer
 
Javasnob's Avatar
 
Join Date: Jul 2005
Location: Wisconsin
Posts: 942
You need to provide the compiler option -lpthread to let the compiler know where to find the pthread object code.
__________________
Flies of a particular kind, i.e. time-flies, are fond of an arrow.

Registered Linux User #408794
Javasnob is offline   Reply With Quote
Old 05-16-2008   #4 (permalink)
martin910
Just Joined!
 
Join Date: May 2008
Posts: 3
thanks a lot the -lpthread option worked. can you please elaborate on the working of -lpthread.

Thanks
martin910 is offline   Reply With Quote
Old 05-16-2008   #5 (permalink)
wje_lf
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 868
Quoth digvijay.gahlot:
Quote:
Have you included the header
<pthread.h>
He may or may not have. But that isn't his main problem. We know this because the error
Code:
In function `main':
threading.c.text+0x6b): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
is produced by the linker, not the compiler. It is the compiler, not the linker, that would complain if a header file were not included.

Six thoughts here.

First, martin910, you can fix your error by adding this option:
Code:
-pthread
to your compiling/linking command. That will include the proper library at link time, thus removing the error message which you report to us. It is slightly preferable over

Code:
-lpthread
Second, digvijay.gahlot is correct in that you should add
Code:
#include <pthread.h>
to your source file if you haven't already.

Third, if you are not already doing this, you should add
Code:
-Wall
to your compiling options. It will catch many things that could otherwise give you trouble without you knowing exactly why.

Fourth, this is dangerous:
Quote:
i would liked to be briefed regarding multithreading.
The reason that this is dangerous is that programming using POSIX threads is rather complex. It's not exactly rocket science, but there are many traps for the unwary, and there are many things you need to know that it will not occur to you to ask questions about.

So run (do not walk) to your nearest bookseller and order the Addison Wesley book Programming with POSIX Threads, by David R. Butenhof. Then settle down and read most of it.

O'Reilly publishes a similar book. But my copy of it, while it seems to present all the required material, isn't anywhere near as thorough in discussing the traps you can get into.

Fifth, even if you know everything about POSIX threads programming, it can still be nasty. The reason is that all threads share the same heap space. If the code for one thread contains a bug which causes memory to be stomped on, it might be a completely different thread which blows up because of this. It might take much detective work (I have seen it take weeks) to determine just where the fault lies.

Moral: Keep your POSIX threads programs as absolutely simple as possible. And don't use POSIX threads unless running speed is absolutely crucial and the communication between threads is frequent enough to even make this an issue. Don't use POSIX threads if all it will do is increase your running speed by, say, two percent. POSIX threads can drive your program maintenance costs through the roof, all for the sake of optimizing your code a tiny bit.

Sixth, two rules for programmers.

1. Do not optimize your code.

And for expert programmers:

2. Do not optimize your code yet.
__________________
--
Bill

Old age and treachery will overcome youth and skill.
wje_lf is online now   Reply With Quote
Old 05-16-2008   #6 (permalink)
digvijay.gahlot
Just Joined!
 
Join Date: Mar 2008
Location: Hyderabad
Posts: 88
We know this because the error
Code:
In function `main':
threading.c.text+0x6b): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
is produced by the linker, not the compiler. It is the compiler, not the linker, that would complain if a header file were not included.


Hey thanks for enlightment.
digvijay.gahlot is offline   Reply With Quote
Old 05-16-2008   #7 (permalink)
martin910
Just Joined!
 
Join Date: May 2008
Posts: 3
thanks wje_lb

but let me know how did you find that the error was provided by the linker and not the compiler. By the error 'undefined reference' it is pretty obvious, but it has sort off shaken my concepts about the various tasks of a compiler. Please, can you explicitly define the jobs of a compiler.
martin910 is offline   Reply With Quote
Old 05-16-2008   #8 (permalink)
wje_lf
Linux Engineer
 
wje_lf's Avatar
 
Join Date: Sep 2007
Location: Mariposa
Posts: 868
The compiler you're using translates your C source code into machine language. But that isn't enough. You also need to include code written and compiled by others and stored in libraries. These libraries are installed on your system at the same time that your C compiler is installed. These libraries do many things. One of these libraries implements POSIX threads for you.

Once the compiler has translated your code, it is the job of the linker to pull together your code and all necessary libraries to produce a program that can run under Linux.

The command "cc" or "gcc" normally runs the compiler and the linker as needed.
__________________
--
Bill

Old age and treachery will overcome youth and skill.
wje_lf is online now   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 11:12 PM.

Powered by vBulletin 3.6.8 ©2000 - 2007, content relevant URLs by vBSEO, Property of Core Root.

Content Relevant URLs by vBSEO 3.0.0