Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 05-15-2009, 09:15 PM   #1
Computer Guru
Prospect
 
Join Date: May 2009
Posts: 2
OS X semaphore irregularities

Hi all,

I have some very basic semaphore code that works great on Linux, but cannot for the life of me get it to run properly on OS X... It returns the oddest of results...

Code:
#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <semaphore.h>

int main()
{
	sem_t* test;
	test = sem_open("test", O_CREAT, 0, 1);
	
	int value;
	sem_getvalue(test, &value);
	printf("Semaphore initialized to %d\n", value);
}
Compiling this on OS X with g++ returns the following output:
Code:
iQudsi:Desktop mqudsi$ g++ test.cpp
iQudsi:Desktop mqudsi$ ./a.out 
Semaphore initialized to -1881139893
Whereas on Ubuntu, I get the decidedly more-sane result:
Code:
iQudsi: Desktop mqudsi$ g++ test.cpp -lrt
iQudsi:Desktop mqudsi$ ./a.out 
Semaphore initialized to 1
I've been at this for 3 hours straight, and cannot figure out why OS X is returning such bizarre results...

I've tried using file paths as the semaphore name, it didn't make a difference.
Computer Guru is offline   Reply With Quote
Old 05-15-2009, 11:05 PM   #2
hayne
Moderator
 
Join Date: Jan 2002
Location: Montreal
Posts: 29,448
Try instead this program (which checks for errors):
Code:
#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <semaphore.h>

int main()
{
	sem_t* test;
	test = sem_open("test", O_CREAT, 0, 1);
        if (test == SEM_FAILED)
        {
            perror("sem_open");
        }
	
	int value;
	if (sem_getvalue(test, &value) != 0)
        {
            perror("sem_getvalue");
        }
	printf("Semaphore initialized to %d\n", value);
        return 0;
}
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 05-17-2009, 03:01 PM   #3
Computer Guru
Prospect
 
Join Date: May 2009
Posts: 2
Thanks.

It turns out (despite how much ADC talks about it) sem_getvalue is not implemented on OS X.
Computer Guru is offline   Reply With Quote
Reply

Tags
os x, posix, semaphore

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 06:26 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Site design © Mac Publishing LLC; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of Mac Publishing LLC.