Re: Semaphore behaviour
From: Larry I Smith (larryXiXsmith_at_verizon.net)
Date: 07/27/04
- Previous message: Larry I Smith: "Re: Semaphore behaviour"
- In reply to: Larry I Smith: "Re: Semaphore behaviour"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Previous message: Larry I Smith: "Re: Semaphore behaviour"
- In reply to: Larry I Smith: "Re: Semaphore behaviour"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|