Exemplo n.º 1
0
void work(int serial, int &value, condition_variable &c, mutex &m, int &turn)
{
    while (1) {
        {
            unique_lock w{m};
            // ожидаем наступления события turn == serial
            c.wait(w, [&turn, &serial]() { return turn == serial; });
        }

        if (value >= 1000) {
            lock_guard w{m};
            turn = (turn + 1) % 2;
            c.notify_all();
            break;
        }
        cout << serial <<  " " << value << endl;
        ++value;

        {
            lock_guard w{m};
            turn = (turn + 1) % 2;
            c.notify_all();
        }
    }
}
Exemplo n.º 2
0
 // assume the _files_lock is held
 void add(file_entry_t fe) {
     fe_get(fe);
     cur_lock.lock();
     cur_set->insert(fe);
     cur_lock.unlock();
     cond.notify_all();
 }
Exemplo n.º 3
0
    void WriteUnLock() {
        unique_lock<mutex> lk(mtx);
        is_writing = false;
        readerQ.notify_all();

        lk.unlock();
    }
Exemplo n.º 4
0
	static void filter()
	{
		size_t filterCount = 0;
	
		for (;;) {
			unique_lock<mutex> filterLock(sync);

			step2Condition.wait(filterLock, []() {return !nums.empty() || count == NUM_COUNT; });
			if (nums.empty()) {
				break;
			}

			int front = nums.front();
			if (front % 3 != 0 && front % 13 != 0) {
				filteredNums.push_back(front);
				filterCount++;
			}
			nums.erase(nums.begin(), nums.begin() + 1);

			filterLock.unlock();
		}
		step3Condition.notify_all();

		// Print trace of consumption
		//lock_guard<mutex> out(print);
		//cout << "Step 2 thread done -- filtered: " << filterCount << endl;
	}
Exemplo n.º 5
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;
	}
Exemplo n.º 6
0
void fun_wrap(bool& done, condition_variable& e, mutex& m, boost::function<void(void)> f)
{
	f();
	mutex::scoped_lock l(m);
	done = true;
	e.notify_all();
}
Exemplo n.º 7
0
 void push(T& entry)
 {
     unique_lock<mutex> uniqueLock(mtx);
     tsQueue.push(entry);
     uniqueLock.unlock();
     cv.notify_all();
 }
Exemplo n.º 8
0
 void release_write() const
 {
     mutex::guard g( mutex_ );
     has_writer_ = writer_waiting_ = false;
     writer_cv_.notify_one();
     reader_cv_.notify_all();
 }
Exemplo n.º 9
0
    void ReadUnLock() {
        unique_lock<mutex> lk(mtx);
        readers--;
        if(readers == 0)
            writerQ.notify_all();

        lk.unlock();
    }
Exemplo n.º 10
0
 void push(int data) {
     {
         unique_lock<mutex> lk(m);
         cv.wait(lk,[this](){ return data_queue.size()<max_size; });
         data_queue.push(data);
     }
     cv.notify_all();
 }
Exemplo n.º 11
0
 void decide_read() const
 {
     mutex::guard g( mutex_ );
     upgratable_     = false;
     writer_waiting_ = false;
     writer_cv_.notify_one();
     reader_cv_.notify_all();
 }
Exemplo n.º 12
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();
 }
Exemplo n.º 13
0
 void pop(int& data) {
     {
         unique_lock<mutex> lk(m);
         cv.wait(lk,[this](){ return data_queue.size()>0; });
         data = data_queue.front();
         data_queue.pop();
     }
     cv.notify_all();
 }
Exemplo n.º 14
0
bool BPCallback (void *baton,
                 SBProcess &process,
                 SBThread &thread,
                 SBBreakpointLocation &location) {
  lock_guard<mutex> lock(g_mutex);
  g_breakpoint_hit_count += 1;
  g_condition.notify_all();
  return true;
}
void movePeople(vector<Human> &v) {
	unique_lock<mutex> lck(mtx);
	while (index == v.size()) cv.wait(lck);
	current_floor = v[index].current_floor;
	cout << " Elevator is picking up person:  " << v[index].getID() << " at floor " << current_floor << endl;
	current_floor = v[index].next_floor;
	cout << " Elevator has dropped off person: " << v[index].getID() << " at floor " << current_floor << endl;
	index++;
	cv.notify_all();
}
Exemplo n.º 16
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();
 }
Exemplo n.º 17
0
    void push_back( shared_ptr< runnable > task )
    {
        mutex::guard g( mutex_ );

        tasks_.template push_back< Tag >( task );

        if ( state_ == RUNNING && idle_workers_ > 0 )
        {
            workers_cv_.notify_all();
        }
    }
Exemplo n.º 18
0
    void push_front( shared_ptr< runnable > task )
    {
        mutex::guard g( mutex_ );

        tasks_.push_front( task );

        if ( state_ == RUNNING && idle_workers_ > 0 )
        {
            workers_cv_.notify_all();
        }
    }
Exemplo n.º 19
0
Arquivo: main.cpp Projeto: CCJY/coliru
    void signal(int id)
    {
        unique_lock<mutex> lk(mx);

        std::cout << "signaling " << id << "\n";

        signaled_id = id;
        cv.notify_all();

        cv.wait(lk, [&] { return signaled_id == -1; });
    }
Exemplo n.º 20
0
    void release_undecided() const
    {
        mutex::guard g( mutex_ );
        upgratable_ = false;

        if ( !--reader_count_ )
        {
            writer_waiting_ = false;
            writer_cv_.notify_one();
            reader_cv_.notify_all();
        }
    }
void producer(int id) {
    unique_lock<mutex> locker(mu, defer_lock); 
    for(int i = 0; i < LIMIT + 1; ++i) {
        locker.lock();
        cout << "producer: " << id << endl;
        d.push_front(i);
        if(i == LIMIT)
            ++_is_done;
        locker.unlock();
        std::this_thread::sleep_for(std::chrono::milliseconds(5));    
        cond.notify_all();
    }
}
Exemplo n.º 22
0
void put(int x) {
    for(auto i=0;i<x;i++) {
        unique_lock<mutex> locker(m_mutex);
        while(Q.full())
            empty.wait(locker);
        assert(!Q.full());

        Q.push_back(i);
        cout << "@ "<< i <<endl;
        full.notify_all();
    }
    flag = false;
}
Exemplo n.º 23
0
void take() {
    while(flag) {
        unique_lock<mutex> locker(m_mutex);
        while(Q.empty())
            full.wait(locker);
        if(flag) {
            assert(!Q.empty());
            cout << "# " << Q.front() <<endl;
            Q.pop_front();
            empty.notify_all();
        }
    }
}
void cloud_cb(const sensor_msgs::PointCloud2ConstPtr &input) {
    //convert to PCL format
    {
        lock_guard<mutex> guard(cloud_lock);
        pcl::fromROSMsg(*input, *cloud);
    }

    {
        lock_guard<mutex> guard(cloud_count_lock);
        cloud_count++;
    }

    cloud_count_cv.notify_all();
}
Exemplo n.º 25
0
    void kill_workers_nl( std::size_t count )
    {
        if ( count <= 0 || active_workers_ <= 0 )
        {
            return;
        }

        active_workers_ = ( count > active_workers_ ) ? 0 : active_workers_ - count;

        workers_cv_.notify_all();

        while ( worker_count_ != active_workers_ )
        {
            manager_cv_.wait( mutex_ );
        }
    }
Exemplo n.º 26
0
void handleLoginoutCB(const HeaderOptions &,
                      const OCRepresentation &rep, const int ecode)
{
    cout << "Auth response received code: " << ecode << endl;

    if (rep.getPayload() != NULL)
    {
        printRepresentation(rep);
    }

    if (ecode == 4)
    {
        g_accesstoken = rep.getValueToString("accesstoken");

        g_uid = rep.getValueToString("uid");
    }

    g_callbackLock.notify_all();
}
Exemplo n.º 27
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();
        }
    }
Exemplo n.º 28
0
    void handler_(const zcm_recv_buf_t *rbuf, const char *channel)
    {
        if (args.invert_channels) {
            cmatch match;
            regex_match(channel, match, invert_regex);
            if (match.size() > 0)
                return;
        }

        bool stillRoom;
        {
            unique_lock<mutex> lock{lk};
            stillRoom = (args.max_target_memory == 0) ? true :
                (totalMemoryUsage + rbuf->data_size < args.max_target_memory);
        }

        zcm_eventlog_event_t *le;
        if (stillRoom) {
            le = new zcm_eventlog_event_t();
            le->timestamp = rbuf->recv_utime;
            le->channellen = strlen(channel);
            le->datalen = rbuf->data_size;
            le->channel = new char[le->channellen + 1];
            le->channel[le->channellen] = '\0'; // terminate the cstr with null char
            memcpy(le->channel, channel, sizeof(char) * le->channellen);
            le->data = new char[rbuf->data_size];
            memcpy(le->data, rbuf->data, sizeof(char) * rbuf->data_size);
        }

        {
            unique_lock<mutex> lock{lk};
            if (stillRoom) {
                q.push(le);
                totalMemoryUsage += le->datalen + le->channellen + sizeof(zcm_eventlog_event_t);
            } else {
                ZCM_DEBUG("Dropping message due to enforced memory constraints");
                ZCM_DEBUG("Current memory estimations are at %" PRId64 " bytes", totalMemoryUsage);
            }
        }
        newEventCond.notify_all();
    }
  void smoker( int index )
  {
    dummy_worker dummy(1e4);

    while(true)
    {
      {
        unique_lock<mutex> lock(m_mux);

        while(m_smoker_index != index)
          m_condition.wait(lock);

        m_smoker_index = -1;
        m_condition.notify_all();
      }

      PRINT("Smoker " << index << " smoking.");
      dummy.work();

      int total_smoke_count = ++g_total_smoke_count;

      if (total_smoke_count == 1)
        g_start_time = high_resolution_clock::now();

      if (total_smoke_count == g_max_smoke_count)
      {
        g_end_time = high_resolution_clock::now();
        unique_lock<mutex> lock(m_mux);
        m_done = true;
        m_done_signal.notify_all();
      }

      if (total_smoke_count >= g_max_smoke_count)
        break;

      ++g_smoke_count[index];
    }
  }
Exemplo n.º 30
0
void paralleGet(const vector<string>& sqlVec, vector<string>& resVec)
{
    unique_lock<mutex> sqlLock(sqlMutex);
    int size = sqlVec.size();
    for (auto it : sqlVec) {
        sqlQueue.emplace(it);
    }
    sqlLock.unlock();
    sqlCond.notify_all();

    unique_lock<mutex> resultLock(resultMutex);
    // while (resultQueue.size() != size) {
    //     resultCond.wait(resultLock);
    // }
    resultCond.wait(resultLock, [&] { return resultQueue.size() == size; } ); // 需要使用 & 才可以把局部变量 size 传入

    while (resultQueue.size()) {
        resVec.emplace_back(resultQueue.front());
        resultQueue.pop();
    }
    resultLock.unlock();

}