#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    How to thread with message


    Hi I am using unix pthreads and I need to send an int from one thread to another. How do I do this using semaphores?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Threads share global integers, so getting to the integer variable itself should be no problem for either thread. Or the data passed in when the thread is created could include a pointer to that int.

    As for semaphores, I haven't played with them much, but by the book (Beginning Linux Programming, 2nd edition):
    1. declare the semaphore as a global so that all threads can access it.
    2. call sem_init(&sem) to initialize the semaphore.
    3. the thread wanting to read the int calls sem_wait(&sem), which blocks until the semaphore is posted.
    4. the thread writing to the int calls sem_post(&sem)
    5. when everyone is done with the semaphore, call sem_destroy(&sem)

IMN logo majestic logo threadwatch logo seochat tools logo