Thread: Semaphores and threads

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Semaphores and threads

    I'm trying to run this code on fedora but it gives me an error for the commands being run in main saying they're undefined:
    here's the code:

    #include<pthread.h>
    #include<stdio.h>

    #include<semaphore.h>

    using namespace std;
    sem_t s1,s2;
    void *ping(void*)
    {
    for(int i=0;i<5;i++)
    {
    sem_wait(&s1);
    printf("ping ");
    sem_post(&s2);
    }
    }
    void *pong(void*)
    {
    for(int i=0;i<5;i++)
    {
    sem_wait(&s2);
    printf("pong \n");
    sem_post(&s1);
    }
    }
    int main()
    {
    pthread_t t1,t2;
    // pthread_attr_t attr;
    // pthread_attr_init(&attr);
    sem_init(&s1,0,1);
    sem_init(&s2,0,0);
    pthread_create(&t1,NULL,&pong,NULL);
    pthread_create(&t2,NULL,&ping,NULL);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    return 0;
    }

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Your mixing some C++ with C (using namespace std; ). Also be sure to link libraries you use (-pthread).

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    In addition to what carrotcake said, your ping() and pong() functions should return values.

    Also, please use code tags when posting in the future to make your code easier to read.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by bithub View Post
    In addition to what carrotcake said, your ping() and pong() functions should return values.
    actually, if they're thread procedures, they should call

    Code:
    pthread_exit(NULL);
    instead of returning. granted, they should still have a return statement for standards-compliance, but in order to behave as expected, you really need to have the pthread_exit call.

    edit: when creating your threads, you should not put the '&' symbol in front of the function names.

    Code:
    pthread_create(&t1, NULL, pong, NULL);
    is the correct usage. putting the '&' in front of the function names will likely result in compiler errors.
    Last edited by Elkvis; 04-27-2009 at 07:31 AM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Elkvis View Post
    actually, if they're thread procedures, they should call

    Code:
    pthread_exit(NULL);
    instead of returning. granted, they should still have a return statement for standards-compliance, but in order to behave as expected, you really need to have the pthread_exit call.
    That's wrong. Returning from the thread procedure is the same as calling pthread_exit(). There is no need to explicitly call it.

    Quote Originally Posted by pthread docs
    If the start_routine returns, the effect is as if there was an implicit call to pthread_exit() using the return value of start_routine as the exit status.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by bithub View Post
    That's wrong. Returning from the thread procedure is the same as calling pthread_exit(). There is no need to explicitly call it.
    after looking it up myself, it seems you are correct. I guess I was thinking that it was necessary to call pthread_exit() from all thread procedures, when it turns out that the only one you MUST call it from is main(), and only then if you don't join all threads before exiting.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> putting the '&' in front of the function names...
    Is optional.

    >> the only one you MUST call it from is main()
    That's optional as well - and should only be done if all existing threads are in a detached state.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threads and semaphores
    By jas_atwal in forum Linux Programming
    Replies: 1
    Last Post: 12-25-2007, 07:33 PM
  2. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM
  3. Threads, Linked Lists, Semaphores, Critical Section ...
    By _jr in forum Windows Programming
    Replies: 4
    Last Post: 06-21-2006, 08:14 AM
  4. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  5. Protected Threads or Semaphores
    By cloudy in forum C++ Programming
    Replies: 2
    Last Post: 08-16-2005, 07:10 AM