コード例 #1
0
ファイル: main.cpp プロジェクト: ale3otik/ADS-4
void test_queue() {
    SyncQueue<int> q;
    const int nthreads = 10;
    const int64_t qsize = 100;
    vector<thread> threads;
    for (int i = 1 ; i < qsize + 1; ++i) {
        q.push(i);
    }

    vector<int> qnt(nthreads);
    for(int i = 0; i < nthreads; ++i) {
        qnt[i] = 0;
        int limit = qsize / nthreads;
        threads.push_back(thread(test_queue_thread<int>, &q, &(qnt[i]), limit));
    }
    
    for(auto & x : threads) {
        x.join();
    }
    
    int64_t sum = 0;
    for(int i = 0; i < nthreads; ++i) {
        sum += qnt[i];
    }

    assert(sum == ((qsize * (qsize+1)) / 2));
    cout << "queue works" << endl;
}
コード例 #2
0
ファイル: Tests.cpp プロジェクト: max-dankow/Concurrency
void receiver(std::vector<int> &dest, SyncQueue<C> &taskQueue, size_t offset, size_t size) {
    auto upperBound = std::min(offset + size, dest.size());
    for (size_t i = offset; i < upperBound; ++i) {
        Optional<int> optionalValue = taskQueue.popOrWait();
        optionalValue.some(dest[i]);
    }
}
コード例 #3
0
ファイル: thread_pool.hpp プロジェクト: lsytj0413/liter
    void run_in_thread(){
        while(m_running){
            Task t;

            if (m_tasks.take(t, 100)){
                t();
            }
        }
    };
コード例 #4
0
ファイル: queue.cpp プロジェクト: okalitova/programming_tasks
int main() {
    vector<thread> threads;
    int n, m;
    cin >> n;
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0)
            threads.push_back(thread(pushElem, i));
        else
            threads.push_back(thread(popElem));
    }
    for (int i = 0; i < n; i++) {
        threads[i].join();
    }
    while(!sync_queue.empty()) {
        cout << sync_queue.front() << " ";
        sync_queue.pop();
    }
    return 0;
}
コード例 #5
0
ファイル: Tests.cpp プロジェクト: max-dankow/Concurrency
bool master(size_t senderNumber, size_t receiverNumber) {
    using std::vector;

    const size_t DATA_SIZE = 1000000;
    vector<int> source(DATA_SIZE);
    std::uniform_int_distribution<int> valuesRandom(-1000, 1000);
    for (size_t i = 0; i < DATA_SIZE; ++i) {
        source[i] = valuesRandom(engine);
    }

    SyncQueue<C> syncQueue;

    // Создаем отправителей.
    vector<std::thread> senders;
    size_t readBlockSize = (DATA_SIZE + senderNumber - 1) / senderNumber;
    for (size_t i = 0; i < senderNumber; ++i) {
        senders.push_back(
                std::thread(sender<C>, std::ref(source), std::ref(syncQueue), i * readBlockSize, readBlockSize));
    }

    // Создаем принимающие потоки.
    vector<int> dist(DATA_SIZE, 0);
    vector<std::thread> receivers;
    size_t writeBlockSize = (DATA_SIZE + receiverNumber - 1) / receiverNumber;
    for (size_t i = 0; i < receiverNumber; ++i) {
        receivers.push_back(
                std::thread(receiver<C>, std::ref(dist), std::ref(syncQueue), i * writeBlockSize, writeBlockSize));
    }

    for (size_t i = 0; i < senders.size(); ++i) {
        senders[i].join();
    }

    for (size_t i = 0; i < receivers.size(); ++i) {
        receivers[i].join();
    }

    // Проверяем результат.
    std::sort(source.begin(), source.end());
    std::sort(dist.begin(), dist.end());
    return source == dist && syncQueue.empty();
}
コード例 #6
0
ファイル: test.cpp プロジェクト: egiby/mipt_algo
TEST_P(SyncQueueTest, TestOfPush)
{
    SyncQueue<ui32> queue;
    std::thread threads[10];
    for (ui32 i = 0; i < GetParam(); ++i)
    {
        threads[i % 10] = std::thread(getPush, std::ref(queue), i);
        if (i)
            threads[(i - 1) % 10].join();
    }
    
    threads[(GetParam() - 1) % 10].join();
    
    std::vector<ui32> used(GetParam());
    
    for (ui32 i = 0; i < GetParam(); ++i)
    {
        used[queue.front()] = 1;
        queue.pop();
    }
    
    ASSERT_EQ(std::accumulate(used.begin(), used.end(), 0), GetParam());
}
 std::future< void > Invoke(F&& f, const VoidType& ) {
     auto p = 
         std::make_shared< std::promise< void > >(std::promise< void >());
     auto ft = p->get_future();
     queue_.Push([=]() {
         try {
              f(data_);
              p->set_value();
          } catch(...) {
              p->set_exception(std::current_exception());
          }
     });
     return ft;
 } 
コード例 #8
0
ファイル: queue.cpp プロジェクト: okalitova/programming_tasks
void popElem() {
    sync_queue.pop();
}
コード例 #9
0
ファイル: thread_pool.hpp プロジェクト: lsytj0413/liter
 // @function
 // @brief 添加任务到执行队列
 // @param task: 待执行任务
 // @return
 void add(const Task& task){
     m_tasks.put(task);
 };
コード例 #10
0
ファイル: thread_pool.hpp プロジェクト: lsytj0413/liter
 // @function
 // @brief 添加任务到执行队列
 // @param task: 待执行任务
 // @return
 void add(Task&& task){
     m_tasks.put(std::forward<Task>(task));
 };
コード例 #11
0
ファイル: test.cpp プロジェクト: egiby/mipt_algo
void getPop(SyncQueue<ui32> &queue)
{
    std::lock_guard<SyncQueue<ui32> > l(queue);
    queue.pop();
}
コード例 #12
0
ファイル: test.cpp プロジェクト: egiby/mipt_algo
void getPush(SyncQueue<ui32> &queue, ui32 x)
{
    std::lock_guard<SyncQueue<ui32> > l(queue);
    queue.push(x);
}
コード例 #13
0
ファイル: queue.cpp プロジェクト: okalitova/programming_tasks
void pushElem(int num) {
    sync_queue.push(num);
}
 ~ConcurrentAccess() {
     queue_.Push([=]{done_ = true;});
     f_.wait();
 }
 ConcurrentAccess(T data, Executor& e) : data_(data), 
     f_(e([=]{
             while(!done_) queue_.Pop()();
          }))
 {}
コード例 #16
0
ファイル: Tests.cpp プロジェクト: max-dankow/Concurrency
void sender(std::vector<int> &source, SyncQueue<C> &taskQueue, size_t offset, size_t size) {
    auto upperBound = std::min(offset + size, source.size());
    for (size_t i = offset; i < upperBound; ++i) {
        taskQueue.push(source[i]);
    }
}
コード例 #17
0
ファイル: Tests.cpp プロジェクト: max-dankow/Concurrency
TEST(SyncQueue, ClosedExceptions) {
    SyncQueue<std::queue<int>> syncQueue;
    syncQueue.close();
    ASSERT_THROW(syncQueue.close(), closedQueueException);
    ASSERT_THROW(syncQueue.push(0), closedQueueException);
}