-
working with threads
hey
im running on linux (fedora) and im tring to run this code :
Code:
#include <pthread.h> /* include file for pthreads - the 1st */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <sys/fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include </usr/include/signal.h>
void *Thread(void *string)
{
int i;
/* writes five messages and exits */
for (i=0; i<5; i++)
printf("%s\n", (char *)string);
pthread_exit(NULL);
}
int main()
{
char *e_str = "Hello!";
char *f_str = "Bonjour !";
pthread_attr_t attr;
pthread_t e_th;
pthread_t f_th;
int rc;
/* creates the right attribute */
//pthread_attr_init(&attr);
//pthread_attr_setdetachstate(&attr,1);//PTHREAD_CREATE_UNDETACHED
/* creates both threads */
rc = pthread_create(&e_th, NULL, Thread, (void *)e_str);
if (rc)
exit(-1);
rc = pthread_create(&f_th, NULL, Thread, (void *)f_str);
if (rc)
exit(-1);
//pthread_attr_destroy(&attr);
/* joins the threads */
pthread_join(e_th, NULL);
pthread_join(f_th, NULL);
pthread_exit(NULL);
}
and im getting the next error on compilation time:
"undefined reference to `pthread_create' " and "undefined reference to `pthread_join'"
does someone know what is the problem ?
thanks !
-
you need to link with -lpthread
-
sorry for my noob question, but how im doing this ?
thanks !
-
by adding -lpthread to the command line you compile your code with. Or do you use some IDE? then there is often an entry in the project options to list libraries you want to link. just add "pthread" (without "") there if that is the case.
-
> #include <malloc.h>
This isn't a standard header file, remove it.
malloc is in stdlib.h
> #include </usr/include/signal.h>
Remove the path and just have
#include <signal.h>
-
someone know how can i configure the "-lpthread" option in eclipse ?
(im working with c++ MANAGED PROJECT)
thanks !
-
right cklick on the project in the project explorer -> properties -> c/c++ build -> Settings -> linker -> libraries -> add -> pthread -> ok -> rebuild