bool CondVarImpl::init(unsigned int maxThreadCount) { int rc = 0; //! Initialize count and maxThreads _count = 0xFFFFFFFF; _maxThreads = maxThreadCount; #ifdef _WIN32 _nLockCount = 0; // Initialize the critical section that protects access to // the wait set. There is no way to check for errors here. InitializeCriticalSection(&_critsecWaitSetProtection); // Initialize the critical section that provides synchronization // to the client. There is no way to check for errors here. InitializeCriticalSection(&_condVarLock); #else pthread_mutex_init(&_condVarLock, NULL); PRINT_ERROR_MSG(rc, "Failed to initialize condition variable lock"); rc = pthread_cond_init(&_condVar, NULL); PRINT_ERROR_MSG(rc, "Failed to Initialize condition variable"); #endif if(rc != 0) return false; return true; }
bool CondVarImpl::destroy() { int rc = 0; #ifdef _WIN32 // Uninitialize critical sections. Win32 allows no error checking // here. DeleteCriticalSection(&_critsecWaitSetProtection); DeleteCriticalSection(&_condVarLock); // Destroying this thing while threads are waiting is a client // programmer mistake. assert( _deqWaitSet.empty() ); #else //! Destroy condition variable lock pthread_mutex_destroy(&_condVarLock); PRINT_ERROR_MSG(rc, "Failed to destroy condition variable lock"); //! Destroy condition variable rc = pthread_cond_destroy(&_condVar); PRINT_ERROR_MSG(rc, "Failed to destroy condition variable"); #endif if(rc != 0) return false; return true; }
void CondVarImpl::syncThreads() { //! Lock condition variable lock beginSynchronized(); int rc = 0; //! count threads if(_count == 0xFFFFFFFF) _count = 0; else { _count++; } if(_count >= _maxThreads - 1) { //! Set to highest value before broadcasting _count = 0xFFFFFFFF; //! Unblock all waiting threads #ifdef _WIN32 rc = broadcast(); if(rc == 0) printf("Problem while calling broadcast\n"); #else rc = pthread_cond_broadcast(&_condVar); PRINT_ERROR_MSG(rc, "Problem while calling pthread_cond_broadcast()"); #endif // _WIN32 } else { //! Block on a condition variable #ifdef _WIN32 wait(); #else if(_count < _maxThreads - 1) { rc = pthread_cond_wait(&_condVar, &_condVarLock); PRINT_ERROR_MSG(rc, "Problem while calling pthread_cond_wait()"); } #endif // _WIN32 } //! Unlock condition variable lock endSynchronized(); }
//! \brief Connect cameras and return camera objects //! //! \return unsigned int &numCameras //! \return FlyCapture2::Camera** &ppCameras //! //! void prepareCameras(unsigned int &numCameras, FlyCapture2::Camera** &ppCameras) { FlyCapture2::Error error; //!< //!< Get camera bus //!< FlyCapture2::BusManager busMgr; error = busMgr.GetNumOfCameras( &numCameras ); PRINT_ERROR( error ); std::cout << "Number of cameras detected: " << numCameras << std::endl; if ( numCameras < 1 ) { std::cerr << "Insufficient number of cameras... press Enter to exit." << std::endl; getchar(); exit(-1); } ppCameras = new FlyCapture2::Camera* [ numCameras ]; //!< //!< Connect to all detected cameras and attempt to set them to a common video mode and frame rate //!< for ( unsigned int i = 0; i < numCameras; i++) { ppCameras[i] = new FlyCapture2::Camera(); std::cout << "setting camera " << i << std::endl; FlyCapture2::PGRGuid guid; error = busMgr.GetCameraFromIndex( i, &guid ); PRINT_ERROR( error ); //!< Connect to a camera error = ppCameras[i]->Connect( &guid ); PRINT_ERROR( error ); PrintCameraInfo( ppCameras[i] ); PrintCameraPropertyInfo( ppCameras[i] ); //!< Set all cameras to a specific mode and frame rate so they can be synchronized error = ppCameras[i]->SetVideoModeAndFrameRate(FlyCapture2::VIDEOMODE_640x480Y8, //!< size of 640x480, format Mono8bit (Y8) FlyCapture2::FRAMERATE_30 ); PRINT_ERROR_MSG( error, "Error starting cameras. \n" "This example requires cameras to be able to set to 640x480 Y8 at 30fps. \n" "If your camera does not support this mode, please edit the source code and recompile the application. \n" "Press Enter to exit. \n"); } std::cout << "all camera set to specific mode." << std::endl; error = FlyCapture2::Camera::StartSyncCapture( numCameras, (const FlyCapture2::Camera**)ppCameras ); PRINT_ERROR_MSG( error, "Error starting cameras. \n" "This example requires cameras to be able to set to 640x480 Y8 at 30fps. \n" "If your camera does not support this mode, please edit the source code and recompile the application. \n" "Press Enter to exit. \n"); std::cout << "StartSyncCapture." << std::endl; }