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:
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
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
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.