0

When I try to compile this I get an error:

warning: passing argument 1 of pthread_create makes pointer from integer without a cast.

Please if anyone can help me..

    int Traveler(int id, int numBags)
    {
           int i;
           int err;
         err = pthread_create(id, NULL, Traveler, NULL);
         if(err!=0)
         {
                      return -1;
         }
         else
         {

         }
    }

1 Answer 1

4

The error is pretty clear. The first argument should be a pointer rather than an integer.

man pthread:

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

   The  pthread_create()  function  shall  create  a  new   thread,   with
   attributes  specified  by  attr, within a process. If attr is NULL, the
   default attributes shall be used. If the attributes specified  by  attr
   are modified later, the thread's attributes shall not be affected. Upon
   successful completion, pthread_create() shall store the ID of the  cre-
   ated thread in the location referenced by thread.

Re-read that last sentence before sticking an ampersand before id in your pthread_create call. EDIT2: you will also have to define id as a pthread_t.

EDIT:

Actually, there are two other issues on the same line: start_routine needs to be a function that takes one argument rather than two, but worst of all this would be a fork bomb since you're passing the same function you're calling from!

Sign up to request clarification or add additional context in comments.

2 Comments

+1, but sticking an & in front of id isn't the right thing to do - the first argument needs to be a pointer to a pthread_t not a pointer to an int. The compiler might accept it (if pthread_t happens to be a typedef for an int), but not in general.
Good catch. Have updated my answer. Well, I guess that makes 4 errors in one line... :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.