I already used SDL2 before, and now I try to use it with openGL 2.1.
But when I try to create a renderer, I get "Invalid renderer".
For creating a renderer I already use the same line before for simple SDL2 project, but with openGL it appears to not work properly.
Does someone have an idea why ?
I tried to change the flags of CreateRenderer, and I have this error for all of them except "SDL_RENDERER_SOFTWARE", but with him I can't grab input properly, and when the mouse cursor goes out the Window I have this error : "Invalid window".
Here is my code :
#include <SDL2/SDL.h>
#include <GL/glut.h>
#include <iostream>
const int WIDTH = 500, HEIGHT = 500;
using namespace std;
SDL_Window* pWindow;
SDL_Renderer* pRenderer;
SDL_Event event;
void initOGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70,(double)WIDTH/HEIGHT,1,1000);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}
void initSDL()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
cout << "Video init failed" << endl;
exit(EXIT_FAILURE);
}
cout << "Video initialized" << endl;
// Window creation
pWindow = SDL_CreateWindow("Test OGL w/ SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (!pWindow)
{
cout << "Window creation failed" << endl;
exit(EXIT_FAILURE);
}
cout << "Window created" << endl;
// Renderer creation
pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);
if (!pRenderer)
{
cout << "Renderer creation failed" << endl;
}
cout << "Renderer created" << endl;
// OGL version
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
// OGL context
SDL_GLContext contexteOGL = SDL_GL_CreateContext(pWindow);
if (!contexteOGL)
{
cout << "Echec création du contexte" << endl;
}
cout << "Contexte créé" << endl;
}
int main(int argc, char const *argv[])
{
initSDL();
initOGL();
cout << "SDL_ERROR : " << SDL_GetError() << endl;
return 0;
}
I could be a "built-in supported driver" problem if you compiled SDL from source in Linux for example.
Try to use "flags = 0" in SDL_CreateRenderer(pWindow, -1, 0);
Then test the actual selected renderer (this is in C):
SDL_RendererInfo ri;
SDL_GetRendererInfo(pRenderer, &ri);
int n = SDL_GetNumRenderDrivers();
printf("Rendering drivers (%i):", n);
for(int i = 0; i < n; i++) {
SDL_RendererInfo info;
SDL_GetRenderDriverInfo(i, &info);
printf(" '%s'", info.name);
}
printf(", active = '%s'\n", ri.name);
Full source code is here
Related
I'm trying to download this link using an C++ apllication. But the it encountered exception:
terminate called after throwing an instance of 'boost::wrapexcept'
what(): body limit exceeded
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
I found out the problem is in http::write.
What on earth is going wrong? Does that mean I was just trying to download the whole file into the memory? If yes, can I continue using ssl::stream anymore? Or there's any alternative?
#ifndef __kernel_entry
#define __kernel_entry
#endif
#include <boost/asio.hpp>
#include <windows.h>
#include <boost/asio/ssl.hpp>
#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <iomanip>
#include <iostream>
void handle_batch(std::vector<size_t> params) {
std::mutex s_mx;
if (!params.empty()) {
// emulate some work, because I'm lazy
auto sum = std::accumulate(begin(params), end(params), 0ull);
// then wait some 100..200ms
{
using namespace std::chrono_literals;
std::mt19937 prng(std::random_device{}());
std::this_thread::sleep_for(
std::uniform_real_distribution<>(100,200)(prng)*1ms);
}
// simple thread id (thread::id displays ugly)
auto tid = std::hash<std::thread::id>{}(std::this_thread::get_id()) % 100;
// report results to stdout
std::lock_guard lk(s_mx); // make sure the output doesn't intermix
std::cout
<< "Thread #" << std::setw(2) << std::setfill('0') << tid
<< " Batch n:" << params.size()
<< "\tRange [" << params.front() << ".." << params.back() << "]"
<< "\tSum:" << sum
<< std::endl;
}
}
namespace net = boost::asio;
namespace ssl = net::ssl;
namespace beast = boost::beast;
namespace http = beast::http;
namespace process = boost::process;
using boost::system::error_code;
using boost::system::system_error;
using net::ip::tcp;
using stream = ssl::stream<tcp::socket>;
auto ssl_context() {
ssl::context ctx{ssl::context::sslv23};
return ctx;
}
void connect_https(stream& s, std::string const& host, tcp::resolver::iterator eps) {
net::connect(s.lowest_layer(), eps);
s.lowest_layer().set_option(tcp::no_delay(true));//dealt here
if (!SSL_set_tlsext_host_name(s.native_handle(), host.c_str())) {
throw system_error{ { (int)::ERR_get_error(), net::error::get_ssl_category() } };
}
s.handshake(stream::handshake_type::client);
}
auto get_request(std::string const& host, std::string const& path) {
using namespace http;
using std::cerr;
request<string_body> req;
req.version(11);cerr<<"__ver";
req.method(verb::get);cerr<<"__met";
req.target("https://" + host + path);cerr<<"__tar";
//req.set(field::user_agent, "test");
req.set(field::host, host);cerr<<"__set";
std::cerr << req << std::endl;
Sleep(1000);
return req;
}
int main() {
net::io_context io; // main thread does all io
net::thread_pool pool(6); // worker threads
// outside for lifetime
http::response_parser<http::buffer_body> response_reader;
beast::flat_buffer lookahead; // for the response_reader
std::array<char,512> buf{0}; // for download content
auto ctx = ssl_context();
ssl::stream<tcp::socket> s(io, ctx);
{ // synchronously write request
std::string host = "www.sfml-dev.org";
try{
connect_https(s, host, tcp::resolver{io}.resolve(host, "https"));
}catch(std::exception& e){std::cerr<<e.what()<<std::endl;}
http::write(s, get_request(host, "/files/SFML-2.5.1-windows-gcc-7.3.0-mingw-32-bit.zip"));
http::read_header(s, lookahead, response_reader);
std::cerr << "Headers: " << response_reader.get().base() << std::endl;
}//https://www.sfml-dev.org/files/SFML-2.5.1-windows-gcc-7.3.0-mingw-32-bit.zip
// now, asynchoronusly read contents
process::async_pipe pipe_to_zcat(io);
std::function<void(error_code, size_t)> receive_zip;
receive_zip = [&s, &response_reader, &pipe_to_zcat, &buf, &lookahead, &receive_zip](error_code ec, size_t /*ignore_this*/) {
auto& res = response_reader.get();
auto& body = res.body();
if (body.data) {
auto n = sizeof(buf) - body.size;
net::write(pipe_to_zcat, net::buffer(buf, n));
}
bool done = ec && !(ec == http::error::need_buffer);
done += response_reader.is_done();
if (done) {
std::cerr << "receive_zip: " << ec.message() << std::endl;
pipe_to_zcat.close();
} else {
body.data = buf.data();
body.size = buf.size();
http::async_read(s, lookahead, response_reader, receive_zip);
}
};
// kick off receive loop
receive_zip(error_code{}, 0);
process::async_pipe zcat_output(io);
process::child zcat(
process::search_path("zcat"),
process::std_in < pipe_to_zcat,
process::std_out > zcat_output,
process::on_exit([](int exitcode, std::error_code ec) {
std::cerr << "Child process exited with " << exitcode << " (" << ec.message() << ")\n";
}), io);
std::function<void(error_code, size_t)> receive_primes;
net::streambuf sb;
receive_primes = [&zcat_output, &sb, &receive_primes, &pool](error_code ec, size_t /*transferred*/) {
{
std::istream is(&sb);
size_t n = std::count(net::buffers_begin(sb.data()), net::buffers_end(sb.data()), '\n');
std::vector<size_t> batch(n);
std::copy_n(std::istream_iterator<size_t>(is), n, batch.begin());
is.ignore(1, '\n'); // we know a newline is pending, eat it to keep invariant
post(pool, std::bind(handle_batch, std::move(batch)));
}
if (ec) {
std::cerr << "receive_primes: " << ec.message() << std::endl;
zcat_output.close();
} else {
net::async_read_until(zcat_output, sb, "\n", receive_primes);
}
};
// kick off handler loop as well:
receive_primes(error_code{}, 0);
io.run();
pool.join();
}
I love to use method chaining to completely initialize objects and then store them in const variables. When analyzing the resulting code it turns out that this means the execution of many copy constructors. Therefore I have wondered whether C++ 11 move semantics might help optimizing method chaining.
Indeed I have been able to significantly speed up my code by adding overloads with ref qualifiers to my chain methods. Please consider this source code:
#include <chrono>
#include <iostream>
#include <string>
#undef DEBUGGING_OUTPUT
#undef ENABLE_MOVING
class Entity
{
public:
Entity() :
data(0.0), text("Standard Text")
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Constructing entity." << std::endl;
#endif
}
Entity(const Entity& entity) :
data(entity.data), text(entity.text)
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Copying entity." << std::endl;
#endif
}
Entity(Entity&& entity) :
data(entity.data), text(std::move(entity.text))
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Moving entity." << std::endl;
#endif
}
~Entity()
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Cleaning up entity." << std::endl;
#endif
}
double getData() const
{
return data;
}
const std::string& getText() const
{
return text;
}
void modify1()
{
data += 1.0;
text += " 1";
}
Entity getModified1() const &
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Lvalue version of getModified1" << std::endl;
#endif
Entity newEntity = *this;
newEntity.modify1();
return newEntity;
}
#ifdef ENABLE_MOVING
Entity getModified1() &&
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Rvalue version of getModified1" << std::endl;
#endif
modify1();
return std::move(*this);
}
#endif
void modify2()
{
data += 2.0;
text += " 2";
}
Entity getModified2() const &
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Lvalue version of getModified2" << std::endl;
#endif
Entity newEntity = *this;
newEntity.modify2();
return newEntity;
}
#ifdef ENABLE_MOVING
Entity getModified2() &&
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Rvalue version of getModified2" << std::endl;
#endif
modify2();
return std::move(*this);
}
#endif
private:
double data;
std::string text;
};
int main()
{
const int interationCount = 1000;
{
// Create a temporary entity, modify it and store it in a const variable
// by taking use of method chaining.
//
// This approach is elegant to write and read, but it is slower than the
// other approach.
const std::chrono::steady_clock::time_point startTimePoint =
std::chrono::steady_clock::now();
for (int i = 0; i < interationCount; ++i)
{
const Entity entity = Entity().getModified1().getModified1().getModified2().getModified2();
#ifdef DEBUGGING_OUTPUT
std::cout << "Entity has text " << entity.getText() << " and data "
<< entity.getData() << std::endl;
#endif
}
const std::chrono::steady_clock::time_point stopTimePoint =
std::chrono::steady_clock::now();
const std::chrono::duration<double> timeSpan = std::chrono::duration_cast<
std::chrono::duration<double>>(stopTimePoint - startTimePoint);
std::cout << "Method chaining has taken " << timeSpan.count() << " seconds."
<< std::endl;
}
{
// Create an entity and modify it without method chaining. It cannot be
// stored in a const variable.
//
// This approach is optimal from a performance point of view, but it is longish
// and renders usage of a const variable impossible even if the entity
// won't change after initialization.
const std::chrono::steady_clock::time_point startTimePoint =
std::chrono::steady_clock::now();
for (int i = 0; i < interationCount; ++i)
{
Entity entity;
entity.modify1();
entity.modify1();
entity.modify2();
entity.modify2();
#ifdef DEBUGGING_OUTPUT
std::cout << "Entity has text " << entity.getText() << " and data "
<< entity.getData() << std::endl;
#endif
}
const std::chrono::steady_clock::time_point stopTimePoint =
std::chrono::steady_clock::now();
const std::chrono::duration<double> timeSpan = std::chrono::duration_cast<
std::chrono::duration<double>>(stopTimePoint - startTimePoint);
std::cout << "Modification without method chaining has taken "
<< timeSpan.count() << " seconds." << std::endl;
}
return 0;
}
The version without method chaining is approximately 10 times faster here than the other one. As soon as I replace
#undef ENABLE_MOVING
by
#define ENABLE_MOVING
the version without method chaining remains only 1.5 times faster than the other one. So this is a great improvement.
Still I wonder whether I could optimize the code even more. When I switch to
#define DEBUGGING_OUTPUT
then I can see that there are new entities created for every call to getModified1() or getModified2(). The only advantage of move construction is that creation is cheaper. Is there a way to even prevent move construction and work on the original entity with method chaining?
With the help of Igor Tandetnik I guess that I can answer my question!
The modification methods have to be changed to return rvalue references:
#ifdef ENABLE_MOVING
Entity&& getModified1() &&
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Rvalue version of getModified1" << std::endl;
#endif
modify1();
return std::move(*this);
}
#endif
#ifdef ENABLE_MOVING
Entity&& getModified2() &&
{
#ifdef DEBUGGING_OUTPUT
std::cout << "Rvalue version of getModified2" << std::endl;
#endif
modify2();
return std::move(*this);
}
#endif
and the initialization has to happen like this:
const Entity entity = std::move(Entity().getModified1().getModified1().getModified2().getModified2());
Then the method chaining code is almost as efficient as the other code. The difference is one call to the move constructor and one additional destructor call for the temporary instance which is negligible.
Thank you for your help!
This is first post in the forum and need help.
I have implemented the ZeroMQ server part much same as documented here
https://github.com/egalli64/thisthread/blob/master/zmq/d2r.cpp.
Below is my code
#include <string>
#include <boost/thread.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/random.hpp>
#include "zmq2.h"
#include <pthread.h>
#include <unistd.h>
#include <cassert>
#include <string>
#include <iostream>
#include <zmq.hpp>
boost::mutex mio;
void *worker(void *arg)
{
std::cout << "in worker" << std::endl;
zmq::context_t *context = (zmq::context_t *) arg;
zmq::Socket skWorker(*context, ZMQ_DEALER);
std::cout << "in worker2" << std::endl;
skWorker.connect("inproc://backend");
std::cout<< "Worker started" << std::endl;
while(true)
{
std::cout << "in while" << std::endl;
zmq::Frames frames = skWorker.blockingRecv(2, false);
if(frames.size() == 1)
{
std::cout << "in if" << std::endl;
break;
}
// send a few replies
for(int i =0; i < 10; ++i)
{
boost::this_thread::sleep(boost::posix_time::millisec(100 * 10));
std::cout << "worker reply" << std::endl;
skWorker.send(frames);
}
}
std::cout << "terminating worker" << std::endl;
return (NULL);
}
void dumpId(const char* tag, const std::string& msg)
{
boost::lock_guard<boost::mutex> lock(mio);
std::string id = boost::lexical_cast<std::string>(boost::this_thread::get_id());
std::cout << id << ' ' << tag << ' ' << msg << std::endl;
}
int main()
{
zmq::context_t context(1);
zmq::Socket client(context, ZMQ_ROUTER);
client.bind("tcp://*:5555");
zmq::Socket workers(context, ZMQ_DEALER);
workers.bind("inproc://workers");
for (int thread_nbr = 0; thread_nbr != 2; thread_nbr++)
{
pthread_t threads;
pthread_create(&threads, NULL, worker, (void *) &context);
}
std::cout << " server started" << std::endl;
zmq_pollitem_t items [] =
{
{ client, 0, ZMQ_POLLIN, 0 },
{ workers, 0, ZMQ_POLLIN, 0 }
};
while(true)
{
if(zmq_poll(items, 2, 3000 * 1000) < 1) // 3 secs
break;
if(items[0].revents & ZMQ_POLLIN)
{
zmq::Frames frames = client.blockingRecv(2);
dumpId("Received on frontend", frames[0]);
workers.send(frames);
items[0].revents = 0; // cleanup
}
if(items[1].revents & ZMQ_POLLIN)
{
zmq::Frames frames = workers.blockingRecv(2);
dumpId("Received on backend", frames[0]);
client.send(frames);
items[1].revents = 0; // cleanup
}
}
std::cout <<"Nothing happened for a while" << std::endl;
// send a terminator to each worker
for(int i = 0; i < 2; ++i)
workers.send("");
std::cout << "Server shutdown" << std::endl;
}
After running the its getting core dumped.
Below is the output
in workerin worker server started
in worker2
in worker2
terminate called after throwing an instance of 'zmq::error_t'
terminate called recursively
Aborted (core dumped)
Could some one let me know where I m doing wrong.......
Thanks in advance....
I have SST files form Microsoft which I need to add to a java truststore.
The problem is all tools that Microsoft provides, add the certs from SST files to windows stores, so difficult to get PEM files form the SST files. If I run certmgr.exe (not certmgr.msc), I can get public key and all that but no cert (in pem or der), I saw bunch of VB scripts and powershell to load them using SST file into System.Security.Cryptography.X509Certificates.X509Certificate2Collection object, but still can't find a way to output as PEM (or der ) formated certificates.
Any suggestions?
S
You could use CAPI to open the SST file as a certificate store then enumerate over the certificates in the file. The following code does this and outputs the certificates in DER form to a file using a SHA1 hash of the certificate as a filename. The first argument is the output folder. The remaining one or more arguments are your SST files.
#include <stdio.h>
#include <tchar.h>
#include "windows.h"
#include "wincrypt.h"
#include "atlbase.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
std::string GetHexRepresentation(const unsigned char * Bytes, size_t Length)
{
std::ostringstream os;
os.fill('0');
os<<std::hex;
for(const unsigned char * ptr=Bytes;ptr<Bytes+Length;ptr++)
os<<std::setw(2)<<(unsigned int)*ptr;
std::string retval = os.str();
std::transform(retval.begin(), retval.end(),retval.begin(), ::toupper);
return retval;
}
BOOL WriteToFileWithHashAsFilename(PCCERT_CONTEXT pPrevCertContext, TCHAR* outputDir)
{
#undef RETURN
#define RETURN(rv) \
{ \
if( hHash ) CryptDestroyHash(hHash); \
if( hProv ) CryptReleaseContext(hProv, 0); \
return rv; \
}
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
BYTE byteFinalHash[20];
DWORD dwFinalHashSize = 20;
if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
std::cout << "CryptAcquireContext failed: " << GetLastError() << std::endl;
RETURN(FALSE);
}
if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
{
std::cout << "CryptCreateHash failed: " << GetLastError() << std::endl;
RETURN(FALSE);
}
if (!CryptHashData(hHash, pPrevCertContext->pbCertEncoded, pPrevCertContext->cbCertEncoded, 0))
{
std::cout << "CryptHashData failed: " << GetLastError() << std::endl;
RETURN(FALSE);
}
if (!CryptGetHashParam(hHash, HP_HASHVAL, byteFinalHash, &dwFinalHashSize, 0))
{
std::cout << "CryptGetHashParam failed: " << GetLastError() << std::endl;
RETURN(FALSE);
}
std::string strHash = GetHexRepresentation(byteFinalHash, dwFinalHashSize);
std::wostringstream filename;
filename << outputDir << strHash.c_str() << ".der" <<std::ends;
FILE* f = _wfopen(filename.str().c_str(), L"wb+");
if(!f)
{
std::wcout << "Failed to open file for writing: " << filename.str().c_str() << std::endl;
RETURN(FALSE);
}
int bytesWritten = fwrite(pPrevCertContext->pbCertEncoded, 1, pPrevCertContext->cbCertEncoded, f);
fclose(f);
if(bytesWritten != pPrevCertContext->cbCertEncoded)
{
std::cout << "Failed to write file" << std::endl;
RETURN(FALSE);
}
RETURN(TRUE);
}
//usage: DumpCertsFromSst <output directory> <SST file 1> ... <SST file n>
int _tmain(int argc, _TCHAR* argv[])
{
SECURITY_ATTRIBUTES sa;
memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
if(argc < 3)
{
std::cout << "At least two arguments must be provided: outputDirectory sstFile1 ... sstFileN etc" << std::endl;
return 0;
}
TCHAR* outputDir = argv[1];
for(int ii = 2; ii < argc; ++ii)
{
HANDLE hFile = NULL;
HCERTSTORE hFileStore = NULL;
LPCWSTR pszFileName = argv[ii];
//Open file
hFile = CreateFile(pszFileName, GENERIC_READ, 0, &sa, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(INVALID_HANDLE_VALUE == hFile)
{
std::wcout << "Failed to open file: " << pszFileName << std::endl;
continue;
}
else
{
std::wcout << "Processing file: " << pszFileName << std::endl;
}
//open certificate store
hFileStore = CertOpenStore(CERT_STORE_PROV_FILE, 0, NULL, CERT_STORE_READONLY_FLAG, hFile);
if(NULL == hFileStore)
{
CloseHandle(hFile);
continue;
}
int count = 0;
PCCERT_CONTEXT pPrevCertContext = NULL;
pPrevCertContext = CertEnumCertificatesInStore(hFileStore, pPrevCertContext);
while(NULL != pPrevCertContext)
{
if(WriteToFileWithHashAsFilename(pPrevCertContext, outputDir))
++count;
pPrevCertContext = CertEnumCertificatesInStore(hFileStore, pPrevCertContext);
}
std::wcout << "Wrote " << count << " certificates" << std::endl;
CloseHandle(hFile);
CertCloseStore(hFileStore, 0);
}
return 1;
}
After porting a code segment from Windows to Mac OS X, I found it to consume a whole CPU core while running; the responsible call for the CPU consumption is boost::interprocess::interprocess_semaphore::timed_wait.
Here follows the code portion which reproduces this behaviour.
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/thread/thread_time.hpp>
#include <iostream>
static bool gStopRequested(false);
struct ShmObj
{
boost::interprocess::interprocess_semaphore mSemaphore;
ShmObj() : mSemaphore(0) {};
~ShmObj() {};
};
int main(char* argc, const char** argv)
{
boost::interprocess::shared_memory_object* lShmObj = NULL;
std::string lShmObjName("My_Boost_Interprocess_Test");
boost::interprocess::mapped_region* lRegion;
ShmObj* lObj;
//Create shared segment
try
{
lShmObj = new boost::interprocess::shared_memory_object(boost::interprocess::create_only, lShmObjName.c_str(), boost::interprocess::read_write);
}
catch (boost::interprocess::interprocess_exception &ex)
{
if (ex.get_error_code() != boost::interprocess::already_exists_error)
{
std::cerr << "Some error" << std::endl;
exit(1);
}
else
{
std::cerr << "Already exists, just taking it back." << std::endl;
try
{
lShmObj = new boost::interprocess::shared_memory_object(boost::interprocess::open_only, lShmObjName.c_str(), boost::interprocess::read_write);
}
catch (boost::interprocess::interprocess_exception &ex2)
{
std::cerr << "D'oh !" << std::endl;
exit(1);
}
}
}
if (!lShmObj)
{
exit(1);
}
lShmObj->truncate(sizeof(ShmObj));
lRegion = new boost::interprocess::mapped_region(*lShmObj, boost::interprocess::read_write);
lObj = new (lRegion->get_address()) ShmObj;
// The loop
while (!gStopRequested)
{
boost::system_time lDeadlineAbsoluteTime = boost::get_system_time() + boost::posix_time::milliseconds(500);
if (lObj->mSemaphore.timed_wait(lDeadlineAbsoluteTime))
{
std::cout << "acquired !" << std::endl;
}
else
{
std::cout << "tick" << std::endl;
}
}
}
Then, I read that unnamed semaphores were not available under Mac OS X, so I thought it could be because unnamed semaphores were not efficiently emulated... I then tried the following, unsucessfully:
#include <boost/interprocess/sync/named_semaphore.hpp>
#include <boost/thread/thread_time.hpp>
#include <iostream>
static bool gStopRequested(false);
int main(char* argc, const char** argv)
{
boost::interprocess::named_semaphore::remove("My_Boost_Interprocess_Test");
boost::interprocess::named_semaphore lMySemaphore(boost::interprocess::open_or_create, "My_Boost_Interprocess_Test", 1);
// The loop
while (!gStopRequested)
{
boost::system_time lDeadlineAbsoluteTime = boost::get_system_time() + boost::posix_time::milliseconds(500);
if (lMySemaphore.timed_wait(lDeadlineAbsoluteTime))
{
std::cout << "acquired !" << std::endl;
}
else
{
std::cout << "tick" << std::endl;
}
}
}
I was actually expecting a better behaviour of boost::interprocess on Mac OS X because of the available Posix primitives, but it is actually not. Any idea for a resolution? Thanks a lot.
I successfully Used Mach semaphores instead of the ones of boost::interprocess... see http://pkaudio.blogspot.com/2010/05/mac-os-x-no-timed-semaphore-waits.html