working with threads

This is a discussion on working with threads within the C++ Programming forums, part of the General Programming Boards category; hey im running on linux (fedora) and im tring to run this code : Code: #include <pthread.h> /* include file ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    13

    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 !

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    you need to link with -lpthread

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    13
    sorry for my noob question, but how im doing this ?
    thanks !

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    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.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,554
    > #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>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    13
    someone know how can i configure the "-lpthread" option in eclipse ?
    (im working with c++ MANAGED PROJECT)
    thanks !

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    right cklick on the project in the project explorer -> properties -> c/c++ build -> Settings -> linker -> libraries -> add -> pthread -> ok -> rebuild

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  2. incrementation of "Homework" threads
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-06-2004, 09:08 PM
  3. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  4. Threads terminate when parent process does?
    By BigDaddyDrew in forum Windows Programming
    Replies: 1
    Last Post: 04-21-2004, 04:32 PM
  5. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21