Example #1
0
    void run()
    {

        {
            mutex::guard g( mutex_ );
            ++worker_count_;

            if ( worker_count_ == active_workers_ )
            {
                manager_cv_.notify_one();
            }
        }

        shared_ptr< runnable > task;

        for ( bool active = true; active; )
        {
            {
                mutex::guard g( mutex_ );

                while ( worker_count_ <= active_workers_ && tasks_.empty() )
                {
                    ++idle_workers_;
                    workers_cv_.wait( g );
                    --idle_workers_;
                }

                if ( worker_count_ <= active_workers_ ||
                     ( state_ == STOPPING && tasks_.size() ) )
                {
                    if ( tasks_.size() )
                    {
                        task = tasks_.front();
                        tasks_.pop_front();
                    }
                }
                else
                {
                    --worker_count_;
                    if ( worker_count_ == active_workers_ )
                    {
                        manager_cv_.notify_one();
                    }
                    return;
                }
            }

            if ( task )
            {
                task->execute();
                task.reset();
            }
        }
    }
Example #2
0
 void release_write() const
 {
     mutex::guard g( mutex_ );
     has_writer_ = writer_waiting_ = false;
     writer_cv_.notify_one();
     reader_cv_.notify_all();
 }
 //! Wakes a thread possibly blocked in the \c dequeue method
 void interrupt_dequeue()
 {
     lock_guard< mutex_type > lock(m_mutex);
     m_interruption_requested = true;
     overflow_strategy::interrupt();
     m_cond.notify_one();
 }
Example #4
0
int main() {
	
		thread t1(erzeuger1);
		thread t2(erzeuger2);
		thread t3(verbraucher);
		thread t4(watcher);
	
	unique_lock<mutex> sperre2(m);
		cout << "\n";
		condivar.notify_one();
		condivar.wait(sperre2);

		

		//t1.detach();		mit detach wird der thread asugeführt ohen berücksichtigung der anderer Threads


		t1.join();
		t2.join();		//mit join() wird gewartet
		t3.join();
		t4.join();
		
		system("PAUSE");
	
	
}
    void run() {
        while (1) {

            unique_lock<mutex> sqlLock(sqlMutex);
            cout << this_thread::get_id() << ": waiting until sqlQueue is NOT empty " << sqlQueue.size() << endl;
            sqlCond.wait(sqlLock, []{ return !sqlQueue.empty(); } );
            string sql = sqlQueue.front();
            sqlQueue.pop();
            sqlLock.unlock();

            cout << this_thread::get_id() << ": working on sql=" << sql << endl;
            string res = "";
            if (0 != exec(sql, res)) {
                cout << this_thread::get_id() << ": failed with error=" << m_error << endl;
                res = "failed with error="+m_error;
            }
            
            unique_lock<mutex> resultLock(resultMutex);
            cout << this_thread::get_id() << ": setting result to resultQueue, now size=" << resultQueue.size() << endl;
            resultQueue.emplace(res);
            resultLock.unlock();
            resultCond.notify_one();

        }
    }
 //! Enqueues a log record
 void enqueue_unlocked(record_view const& rec)
 {
     const bool was_empty = m_queue.empty();
     m_queue.push(enqueued_record(rec));
     if (was_empty)
         m_cond.notify_one();
 }
 channel_op_status push_and_notify_( ptr_t new_node,
                                     std::unique_lock< mutex > & lk) noexcept {
     push_tail_( new_node);
     lk.unlock();
     not_empty_cond_.notify_one();
     return channel_op_status::success;
 }
Example #8
0
	static void producer()
	{
		// Set Seed
		srand(time(nullptr));
		size_t addCount = 0;

		// Get X random numbers
		for (;;) {

			unique_lock<mutex> addLock(sync);
			if (count >= NUM_COUNT) break;


			int n = rand();

			// Push int
			nums.push_back(n);
			addCount++;		
			count++;
			addLock.unlock();
			step2Condition.notify_one();


		}
		step2Condition.notify_all();

		//lock_guard<mutex> out(print);
		//cout << "Step 1 thread done -- added: " << addCount << endl;
	}
Example #9
0
File: main.cpp Project: CCJY/coliru
    void signal()
    {
        lock_guard<mutex> lk(mx);

        std::cout << "signaling one\n";
        cv.notify_one();
    }
Example #10
0
		void triggerResponseForAnomaly(bool anomalyStillOngoing) {
			clock.lock();
			anomaliesTriggered++;
			cond.notify_one();
			clock.unlock();
			cout << "Triggered" << anomaliesTriggered << endl;
		}
Example #11
0
 void decide_read() const
 {
     mutex::guard g( mutex_ );
     upgratable_     = false;
     writer_waiting_ = false;
     writer_cv_.notify_one();
     reader_cv_.notify_all();
 }
Example #12
0
 void RestartWith(TFn const && fn)
 {
   {
     unique_lock<mutex> l(m_mutex);
     m_fn = fn;
   }
   m_cv.notify_one();
 }
			void disarm()
			{
				if (!sync)
					return;
				sync 	= false;
				rdy 	= true;
				unique_lock<mutex> lk{m};
				cv.notify_one();
			}
Example #14
0
 void write_to_read() const
 {
     mutex::guard g( mutex_ );
     ++reader_count_;
     has_writer_     = false;
     writer_waiting_ = false;
     writer_cv_.notify_one();
     reader_cv_.notify_all();
 }
Example #15
0
  // utility functions; require a lock to already be taken
  // and the container to already be full/empty as applicable.
  // Use these functions everywhere that modifies contents_.
  void preconditions_hold_put_(value_type const& v) {
    if(LASERCAKE_NO_THREADS) {
      caller_error_if(contents_, "'put' into a full m_var in single-threaded mode blocks.");
    }
    contents_.reset(v);
#if !LASERCAKE_NO_THREADS
    readers_wait_on_.notify_one();
#endif
  }
Example #16
0
void thread_func1(mutex & mtx, condition_variable & cv)
{
    std::cout << "thread_func1: begin" << std::endl;
    this_thread::sleep(posix_time::seconds(3));
    mutex::scoped_lock lc(mtx);
    cv.notify_one();
    std::cout << "thread_func1: notify_one" << std::endl;
    std::cout << "thread_func1: end" << std::endl;
}
Example #17
0
void producer_thread() 
{ 
    while (more_data_to_produce()) { 
        auto x = get_next_data(); 
        lock_guard<mutex> lk(mut); 
        q.push(x); 
        cond.notify_one();              // notify the other thread that data is available. 
    }
}
Example #18
0
bool CPU_NextState(CPUThreadState from, CPUThreadState to) {
	if (cpuThreadState == from) {
		cpuThreadState = to;
		cpuThreadCond.notify_one();
		return true;
	} else {
		return false;
	}
}
Example #19
0
 void write_to_undecided() const
 {
     mutex::guard g( mutex_ );
     ++reader_count_;
     upgratable_     = true ;
     has_writer_     = false;
     writer_waiting_ = false;
     writer_cv_.notify_one();
     reader_cv_.notify_all();
 }
Example #20
0
void data_preparation_thread()
{
    while(true)
    {
        lock_guard<mutex> lk(mut);
        data_queue.push(count++);
        data_cond.notify_one();
        this_thread::sleep_for(chrono::milliseconds(1000));
    }
}
void print_number_worker(int num) {
	while(counter < 50) {

		unique_lock<mutex> lk(m);
		cv.wait(lk, [] { return can_print; });

		cout << num << endl;
		counter++;
		cv.notify_one();
	}
}
Example #22
0
  value_type preconditions_hold_take_() {
    if(LASERCAKE_NO_THREADS) {
      caller_error_if(!contents_, "'take' from an empty m_var in single-threaded mode blocks.");
    }
    value_type result = contents_.get();
    contents_.reset();
#if !LASERCAKE_NO_THREADS
    writers_wait_on_.notify_one();
#endif
    return result;
  }
Example #23
0
    size_t push (const T& t) {
      size_t retval;
      {
	lock lock (list_mutex);
	list.push_back (t);
	retval = list.size ();
      }
      // Only one thread should be calling pop.
      m_condition.notify_one ();
      return retval;
    }
Example #24
0
void producer()
{
  int i = 0;
  while (true) {
    Message m(i++);
    unique_lock<mutex> lck{mmutex};
    
    mqueue.push(m);
    mcond.notify_one();
  } // Implicitly release lock.
}
Example #25
0
void even_thread() 
{ 
    while (ctrEven < MAX_ELEMS) { 
        unique_lock<mutex> lk(mut); 
        cond.wait(lk, [] { return ((ctrOdd - ctrEven) == 3); }); 
        ctrEven += 2; 
        cout << ctrEven << endl; 
        cond.notify_one(); 
        lk.unlock(); 
    } 
}
Example #26
0
    void release_read() const
    {
        mutex::guard g( mutex_ );

        if ( !--reader_count_ )
        {
            if ( upgratable_ )
            {
                upgratable_     = false;
                has_writer_     = true ;
                upgrade_cv_.notify_one();
            }
            else
            {
                writer_waiting_ = false;
            }
            writer_cv_.notify_one();
            reader_cv_.notify_all();
        }
    }
void SwitcherData::Stop()
{
	if (th.joinable()) {
		{
			lock_guard<mutex> lock(m);
			stop = true;
		}
		cv.notify_one();
		th.join();
	}
}
Example #28
0
/*
erzeuger2:
es werden 50 verschiedene Zufallszahlen erstellt, anschließend
ausgegeben und zuletzt in einem Vector gespeichert.
*/
void erzeuger2() {
	srand(time(NULL));
	for (int i = 0; i < 50; i++) {
		unique_lock<mutex> sperre2{ m };	//Die Mutex wird hier gesperrt
		condivar.wait(sperre2);				//nach dem "wait", also dem Warten wird sie erneut gesperrt
		int randomZahl = (rand() % 300) - 1;	//Randomzahlen werden erstellt
		cout << randomZahl << endl;				//Randomzahlen werden ausgegeben
		myvector.push_back(randomZahl);			//mit push_back() wird eine Element hinten an den Vector dazugefügt 
		condivar.notify_one();					//"deblockiert" den erzeuger2 Thread

	}
}
Example #29
0
File: main.cpp Project: CCJY/coliru
	void consumer(unsigned int thread_id) {
		do {
            {
			    unique_lock<mutex> l(m);
			    cv.wait(l, [this](){ return produced; });
            }
            
			cout << "Consumer thread " << thread_id << "...now is in control: " << value << endl;
			produced = false;
			cv.notify_one();
		} while (value);
	}
Example #30
0
/*
 * @param char* path
 * @param int   filetype	SG_FILETYPE_SOURCE: .c/.cpp/.m/.mm/etc
 * 							SG_FILETYPE_HEADER: .h/.hpp/etc
 * @param int   wordtype	
 * @param char* target_word
 */
void parse_directory_win( char* path, FILE_TYPE_INFO* p_info, int wordtype, const char* target_word )
{
	char path_name[512];
	strcpy( path_name, path );
	strcat( path_name, "\\*.*" );
	
	HANDLE h_find;
	WIN32_FIND_DATA find_data;
	h_find = FindFirstFile( path_name, &find_data );
	if( h_find == INVALID_HANDLE_VALUE ){
		printf( "directory read error! [%s]\n", path );
		return;
	}
	while( true ){
		if( strcmp( find_data.cFileName, "." ) == 0 ||
			strcmp( find_data.cFileName, ".." ) == 0 ){
			// do nothing
			;
		}
		else if( (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) &&
		         find_data.cFileName[0] != '.' ){
			// not hidden directory
            if( p_info->foldernamelist.has_foldername( find_data.cFileName ) ){
                // ignore the folder
            } else {
                char path_name_r[512];
                strcpy( path_name_r, path );
                strcat( path_name_r, "\\" );
                strcat( path_name_r, find_data.cFileName );
                parse_directory_win( path_name_r, p_info, wordtype, target_word );
            }
		} else if( ( (p_info->filetype & SG_FILETYPE_SOURCE ) && is_source_file( p_info, find_data.cFileName ) ) ||
				   ( (p_info->filetype & SG_FILETYPE_HEADER ) && is_header_file( find_data.cFileName ) ) ){
            // file
			char file_name_r[512];
			strcpy( file_name_r, path );
			strcat( file_name_r, "\\" );
			strcat( file_name_r, find_data.cFileName );
            {
                lock_guard<mutex> lk(m_queue_mtx);
                m_queue.push(string(file_name_r));
                m_files_ready_cond.notify_one();
            }
		}
		BOOL ret = FindNextFile( h_find, &find_data );
		if( !ret ){
			DWORD dwError = GetLastError();
			assert( dwError == ERROR_NO_MORE_FILES );
			break;
		}
	}
	FindClose( h_find );
}