Threads

References

https://computing.llnl.gov/tutorials/pthreads/

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#BASICS

http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthreads.html


Create a Thread

int pthread_create   (pthread_t *restrict thread,  const pthread_attr_t *restrict attr,  void *(*start_routine)(void*),  void *restrict arg ) ;

– Must create a pthread_t variable to hold the thread pointer, used further to control thread
– Thread attr can be 0, and I usually use 0
– Pointer to a function defined as: void* start_routine(void*)
– arg is the void* to the arguments


Compiling

When compiling programs that use the pthread library you need to link the library.
This is done like this :

$ gcc program.c -lpthread

$ gcc  program.c  -lpthread –o program


Eclipse

When compiling programs that use the pthread library you need to link the library.

.

Make sure you have in Project -> Settings -> C/C++ Build the option “Generate Makefile automatically” selected! Otherwise Eclipse assumes you are providing your own makefile and doesn’t show the “Tool setting” tab shown below..

.

Project -> Properties -> C/C++ Build -> Settings -> Gcc C Compiler -> Miscellaneous , add  “-pthread” in Other flags

1

.

Project -> Properties -> C/C++ Build -> Settings -> Gcc C++ linker -> Libraries , add “pthread”

2

.

When you will compile your project you´ll  can see the configurations/parameters you have added.

3

.

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Leave a comment