I'm having some very basic trouble with semaphores.
#include <iostream>
using namespace std;
#include <semaphore.h>
int main() {
sem_t sem;
sem_init(&sem, 1, 1);
sem_wait(&sem);
sem_wait(&sem);
printf("Return value: %d\n", sem_wait(&sem));
return 0;
}
I expect this code to wait forever (due to the second sem_wait).
Instead, it prints out "Return value: -1", and exits.
I am compiling with g++ version 4.2.1 on a Mac (OSX 10.8.5).
OS/X does not support unnamed POSIX semaphores. Use sem_open() instead of sem_init().
Related
I have a server that I want to run, and it uses a cross platform library that only gives me a tick() to call:
int main()
{
Inst inst;
while(true)
{
inst.tick();
}
}
I need to try to lower the cpu usage so that it doesnt constantly take up 1 core.
Is there a simple way to do this without boost?
Thanks
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
//5 seconds
auto duration = chrono::duration<float>(5);
this_thread::sleep_for(duration);
return 0;
}
However, even if this code is completely fine, I can't seem to compile it with the provided MinGW compiler from Code::Blocks.
I'm quite new to the stack overflow, in fact this is my first post, So hello everyone. So let's get to the point.
Using boost library thread ver. 1.54.0
Using VS2010 32 Bit - Professional
I have built the libraries for the boost thread,
not using precompiled headers in vs C++ settings,
linked the library to the project,
here is the code
#include <boost\thread\thread_only.hpp>
#include <iostream>
#include <conio.h>
#pragma comment(lib, "libboost_thread-vc100-mt-gd-1_54.lib")
#define BOOST_LIB_NAME libboost_thread-vc100-mt-gd-1_54.lib
struct callable
{
void blah();
};
void callable::blah()
{
std::cout << "Threading test !\n";
}
boost::thread createThread()
{
callable x;
return boost::thread(x);
}
int main()
{
createThread();
_getch();
return 0;
}
after all this I get this error
Error 1 error C2064: term does not evaluate to a function taking 0 arguments ..\..\boost_1_54_0\boost\thread\detail\thread.hpp 117 1 BoostTrial
Could you help me to get this example to work. Reason why I am using this example is because I have another app which has been set up exactly the same way and it's not working because of this error :-( my goal is to get the multithreading to work and then I can take it from there.
Thanks for your time.
You need to implement operator() in callable.
Don't forget to either join() or detach() thread to prevent abnormal program termination.
See boost::thread tutorial for more examples.
#include <boost\thread\thread_only.hpp>
#include <iostream>
#pragma comment(lib, "libboost_thread-vc100-mt-gd-1_54.lib")
using namespace boost;
struct callable
{
void operator()()
{
std::cout << "Threading test !\n";
}
};
boost::thread createThread()
{
callable x;
return boost::thread(x);
}
int main()
{
boost::thread th = createThread();
th.join();
}
Example with std::thread;
#include <iostream>
#include <future>
#include <chrono>
using namespace std;
using namespace std::chrono;
int sampleFunction(int a)
{
return a;
}
int main()
{
future<int> f1=async(launch::deferred,sampleFunction,10);
future_status statusF1=f1.wait_for(seconds(10));
if(statusF1==future_status::ready)
cout<<"Future is ready"<<endl;
else if (statusF1==future_status::timeout)
cout<<"Timeout occurred"<<endl;
else if (statusF1==future_status::deferred)
cout<<"Task is deferred"<<endl;
cout<<"Value : "<<f1.get()<<endl;
}
Output -
Timeout occurred
Value : 10
In above example, I was expecting future_status to be deferred instead of timeout. sampleFunction has been launched as launch::deferred. Hence it will not be executed until f1.get() has been called. In such condition wait_for should have returned future_status::deferred and not future_status::timeout.
Appreciate if someone can help me understand this.
I am using g++ version 4.7.0 on fedora 17.
GCC and the GNU STL have no support of the complete C++ 11.
Here you can check out the C++ 11 implementation status in GCC and GNU STL:
http://gcc.gnu.org/projects/cxx0x.html
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
Also, read this discussion thread: http://blog.gmane.org/gmane.comp.gcc.bugs/month=20120201
C++ returns invalid value in the following code:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
205960
When I commnet line after return, it works fine:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
//v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
1
I am using code::blocks with mingw32-g++.exe compiler. The mingw version is: gcc version 4.4.1 (TDM-2 mingw32).
Your compiler has a bug. Fortunately, it is also obsolete. You should upgrade — G++ is up to version 4.6.2, which also implements much of C++11, which is very useful.
If you choose to stick with an older compiler, that is also a decision to accept its flaws.
Edit: If you are really stuck with 4.4 (for example due to a PHB), that series is still maintained. You can upgrade to GCC 4.4.6, released just this past April.
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...
#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:
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:
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.
I'd appreciate any help I could get.
Are you testing for errors? Try:
#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");
return 1;
}
int value;
if (sem_getvalue(test, &value)) {
perror("sem_getvalue");
return 1;
}
printf("Semaphore initialized to %d\n", value);
}
$ g++ sem-testing.cc -Wall
$ ./a.out
sem_getvalue: Function not implemented
$ man sem_getvalue
No manual entry for sem_getvalue
You are using a function that is not currently implemented in Mac OS X, and the integer you are printing out contains the default data that the integer was initialised with which was probably random data that was still in memory. Had you zero'd it out, by setting it with int value = 0; you might have caught this mistake sooner.
This is the code I used (thanks to bdonlan):
#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");
return 1;
}
int value;
if (sem_getvalue(test, &value)) {
perror("sem_getvalue");
return 1;
}
printf("Semaphore initialized to %d\n", value);
}
Well, perhaps sem_open() is failing - you didn't test.
Or, perhaps OSX doesn't default to supporting shared posix sems - if /dev/shm isn't mounted, typically the system won't support sem_open().
You may want to use SysV semaphores.
A similar question regarding Slackware was asked here: how-do-i-stop-semopen-failing-with-enosys
However, further searching shows OSX named semaphones are built on top of Mach semaphores, and you probably need to sem_unlink() them when you're done (not just sem_close(), or maybe instead), and you should be careful about permissions - I suggest starting with 0777 or maybe 0700, instead of 0. See posiz semaphores in Darwin