Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Bartlomiej
New Contributor I
293 Views

How to compile programs with TBB, using GCC in Linux

Jump to solution
Dear Forum Users,

The question in the title might seem extremely silly, but, actually, I haven't gotten a satisfying answer on the pthread.comp.programming Usenet group:

http://groups.google.pl/group/comp.programming.threads/browse_thread/thread/4574f30ec2c10ec9?hl=pl#

My problem is: should I use the ``-pthread'' option or not ? it would seem to me that linking the TBB library (and, obviously, including proper headers) should be sufficient, but the opinions on the Usenet group seem to be different.

Moreover - as I've written there - examples packaged with TBB are not consistent: the Makefiles for examples of parallel_for use ``-lpthread'' (but not ``-pthread'' !) and the other examples link ``-ltbb'' only.

Which variant is correct ?
Programs I've written up to now worked fine without ``-pthread'' or ``-lpthread'', but possibly I was just lucky.

Thank you very much in advance for any clarifications.

Best regards
Bartlomiej Kubica
0 Kudos
1 Solution
adunsmoor
New Contributor I
293 Views
I think that there are a few points to consider.

1. I don't think you will need -lpthread unless you call the pthread_* functions directly and need it to get the application to link. On Linux, libtbb.so.2 is built such that it has a dependency on libpthread.so and the linker and/or loader should be able to resolve the dependency automatically. (try "ldd libtbb.so.2" and see what gets printed)

Also, if you use -pthread then gcc should take care of the libpthread linking for you.

2.The TBB examples and tutorials that I've seen are simple enough that that compiling without -pthread should be fine. TBB doesn't communicate errors through errno with client code so that shouldn't be an issue. As long as the client code (yours or the examples) doesn't need access to the reentrant versions of other libraries then you can get away with not having -pthread.

3. Finally, I find it's easier to just always use the -pthread option... for the reasons you state and to keep everything consistent. I found the following discussion helpful:

http://gcc.gnu.org/ml/gcc-help/2007-01/msg00135.html

It would be interesting to know more about the performance differences you note with -pthread vs. without. If you hadn't mentioned that I would have simply said use -pthread and be done with it.

Good luck.

View solution in original post

4 Replies
Alexey_K_Intel3
Employee
293 Views
The TBB library itself uses POSIX threads API internally, and links with pthread as a shared library. Thus specifying -ltbb should be enough to resolve any pthread symbols introduced into your app due to TBB use (e.g. referenced in TBB headers).

So if you only use TBB, the -lpthread option is not required. If you find out that some TBB program can not be linked that way, please submit a bug report. The -lpthread option in examples must be left accidentally; I will look at it.

If you however use the POSIX threads API directly in your application, I'd recommend you to keep -lpthread, as a matter of good engineering practice to explicitly specify direct dependencies.
Bartlomiej
New Contributor I
293 Views

Thanks, that's what it seemed to me.
Still, I'm not concerned about one thing.

In GCC the option -pthread not only links the library, but also sets some preprocessing options, e. g. programs with this option use a different errno, AFAIK.
So ommiting this option might not result in errors for most programs, but result in obscure errors sometimes.
That's how I see it, but possibly I'm overlooking something.

Best regards
Bartlomiej Kubica

adunsmoor
New Contributor I
294 Views
I think that there are a few points to consider.

1. I don't think you will need -lpthread unless you call the pthread_* functions directly and need it to get the application to link. On Linux, libtbb.so.2 is built such that it has a dependency on libpthread.so and the linker and/or loader should be able to resolve the dependency automatically. (try "ldd libtbb.so.2" and see what gets printed)

Also, if you use -pthread then gcc should take care of the libpthread linking for you.

2.The TBB examples and tutorials that I've seen are simple enough that that compiling without -pthread should be fine. TBB doesn't communicate errors through errno with client code so that shouldn't be an issue. As long as the client code (yours or the examples) doesn't need access to the reentrant versions of other libraries then you can get away with not having -pthread.

3. Finally, I find it's easier to just always use the -pthread option... for the reasons you state and to keep everything consistent. I found the following discussion helpful:

http://gcc.gnu.org/ml/gcc-help/2007-01/msg00135.html

It would be interesting to know more about the performance differences you note with -pthread vs. without. If you hadn't mentioned that I would have simply said use -pthread and be done with it.

Good luck.

View solution in original post

Bartlomiej
New Contributor I
293 Views
Thanks for pointing this discussion to me and for the explanation.
Using -pthread as a default sounds as a reasonable solution, indeed.

If I do any performance tests and find out something interesting, I'll keep you informed.

Best regards

Reply