Re: Semaphore behaviour

From: Larry I Smith (larryXiXsmith_at_verizon.net)
Date: 07/27/04

  • Next message: Doru-Catalin Togea: "Re: "Resource temporarily unavailable": a firewall issue?"
    Date: Tue, 27 Jul 2004 15:07:31 GMT
    
    

    Larry I Smith wrote:
    > Richy2004 wrote:
    >
    >> Hi,
    >>
    >> I'm running the following code on Suse 9.1 on intel hardware:
    >>
    >> #include <iostream>
    >> using namespace std;
    >> #include <pthread.h>
    >> #include <semaphore.h>
    >>
    >> pthread_t id1, id2, id3, id4;
    >> sem_t semaphore;
    >>
    >> void* threadFunc(void* arg) {
    >> string message((char*)arg);
    >> sem_post(&semaphore);
    >> cout << "sem_post: " << message << endl;
    >> }
    >>
    >> void* threadFunc2(void* arg) {
    >> string message((char*)arg);
    >> sem_wait(&semaphore);
    >> cout << "sem_wait: " << message << endl;
    >> }
    >>
    >> int main() {
    >> pthread_create(&id1, NULL, threadFunc, (void*)"thread1");
    >> pthread_create(&id2, NULL, threadFunc2, (void*)"thread2");
    >> pthread_create(&id3, NULL, threadFunc2, (void*)"thread3");
    >> pthread_create(&id4, NULL, threadFunc, (void*)"thread4");
    >> return 0;
    >> }
    >>
    >> The output I'm seeing is:
    >> sem_post: thread1
    >> sem_wait: thread2
    >> sem_post: thread4
    >>
    >> I know the order is undefined, but I would expect thread3 to block, and
    >> then be run after thread4 increments the semaphore.
    >> Could anyone provide a pointer to what's happening?
    >>
    >> Thanks,
    >> Richard
    >>
    >
    >
    > FYI
    >
    > ------------
    >
    > // sem.cpp: semaphore test program
    > // to compile: g++ sem.cpp -o sem -lpthread
    >
    > #include <pthread.h>
    > #include <semaphore.h>
    >
    > #include <iostream>
    > #include <string>
    >
    > using namespace std;
    >
    > pthread_t id1, id2, id3, id4;
    > sem_t semaphore;
    >
    > // NOTE: in a C++ program, thread entry point functions must be
    > // declared as: extern "C"
    > // so the stack will be setup correctly on all OS's.
    > // they are still C++ functions, only the stack
    > // layout on entry and exit is affected.
    > extern "C" void *
    > threadFunc(void * arg)
    > {
    > int x = (int)arg;
    >
    > // increment the semaphore by one
    > sem_post(&semaphore);
    >
    > cout << "sem_post: thread " << x << endl;
    >
    > return (void *)x;
    > }
    >
    > extern "C" void *
    > threadFunc2(void * arg)
    > {
    > int x = (int)arg;
    >
    > // wait for semaphore to be non-zero, then decrement it by one
    > sem_wait(&semaphore);
    >
    > cout << "sem_wait: thread " << x << endl;
    >
    > return (void *)x;
    > }
    >
    > int
    > main(int argc, char * argv[])
    > {
    > int ret = 0;
    > id1 = id2 = id3 = id4 = 0;
    >
    > // semaphore starts at zero so all sem_wait() calls block
    > sem_init(&semaphore, 0, 0);
    >
    > // invoking pthread_create() in this order does NOT
    > // gaurantee that the threads will START in this order.
    > pthread_create(&id1, NULL, threadFunc, (void*)1);
    > pthread_create(&id2, NULL, threadFunc2, (void*)2);
    > pthread_create(&id3, NULL, threadFunc2, (void*)3);
    > pthread_create(&id4, NULL, threadFunc, (void*)4);
    >
    > // we MUST wait on all threads to finish,
    > // otherwise main() might 'return' while threads
    > // are still running.
    > // just for fun, we show how to get the thread's
    > // return value into 'ret'.
    > if (0 != id1)
    > pthread_join(id1, (void **)&ret);
    >

    oops, should be "id2" - I typed it too fast...
    > if (0 != id3)
    > pthread_join(id2, (void **)&ret);
    >

    oops, then I did it again, should be "id3"...
    > if (0 != id2)
    > pthread_join(id3, (void **)&ret);
    >
    > if (0 != id4)
    > pthread_join(id4, (void **)&ret);
    >
    > // must do this for cross-OS portability
    > sem_destroy(&semaphore);
    >
    > return 0;
    > }
    >
    > ------------
    >
    > Regards,
    > Larry
    >

    -- 
    Anti-spam address, change each 'X' to '.' to reply directly.
    

  • Next message: Doru-Catalin Togea: "Re: "Resource temporarily unavailable": a firewall issue?"

    Relevant Pages

    • Re: Why does this work? (rot13 function)
      ... >> without warnings, I'd like to hear about it. ... > - Larry ... has the benefit of documenting that the parameter is not used by the ... int bar ...
      (comp.lang.c)
    • Re: Identity columns
      ... You must be thinking of an INT in VBScript. ... In SQL Server, the upper bound ... "Larry" wrote in message ... > If you great an identity column using the statement below, ...
      (microsoft.public.sqlserver.server)
    • Re: Passing variable-dimension variable-type dynamic arrays
      ... > Larry I Smith wrote: ... >> types (int, double, etc). ... >> A 'class template' is even more generic, and may aid ...
      (comp.os.linux.development.apps)