8000 Bug report: When sub-task are performed quickly, the results are inaccurate · Issue #39 · mtrebi/thread-pool · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Bug report: When sub-task are performed quickly, the results are inaccurate #39
Open
@muyuuuu

Description

@muyuuuu
#include <iostream>

#include "../include/ThreadPool.h"

class TEST {
private:
  std::mutex mtx;
  std::vector<int> v1[2];
  std::priority_queue<int> q;
public:
  void init() {
    v1[0].push_back(1);
    v1[0].push_back(2);
    v1[0].push_back(3);

    v1[1].push_back(1);
    v1[1].push_back(3);
    v1[1].push_back(2);
  }
  
  void push(const int& index) {
    for (auto& i : v1[index]) {
      std::lock_guard<std::mutex> m{mtx};
      q.push(i);
    }
  }

  void run() {
    ThreadPool pool(2);
    pool.init();
    // https://github.com/mtrebi/thread-pool/issues/14
    // https://github.com/mtrebi/thread-pool/issues/7
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 2; j++) {
        auto func = std::bind(&TEST::push, this, j);
        pool.submit(func);
      }
    }
    pool.shutdown();

    int tmp{-2};
    std::cout << q.size() << std::endl;
  }
};

int main(int argc, char *argv[]) {
  TEST t;
  t.init();
  t.run();
  return 0;
}

The output of the above code is not accurate.

315 // sometimes
291 // sometimes
69 // sometimes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0