ThreadPool.cpp 915 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "tests/framework/Framework.h"
  2. #include "anki/core/ThreadPool.h"
  3. #include "anki/core/Logger.h"
  4. #include "anki/util/StdTypes.h"
  5. namespace anki {
  6. /// Struct for our tests
  7. struct TestJobTP: ThreadJob
  8. {
  9. U32 in = 0;
  10. U32 iterations = 0;
  11. void operator()(U /*threadId*/, U /*threadsCount*/)
  12. {
  13. for(U32 i = 0; i < iterations; i++)
  14. {
  15. ++in;
  16. }
  17. }
  18. };
  19. } // end namespace anki
  20. ANKI_TEST(Core, ThreadPool)
  21. {
  22. const U32 threadsCount = 4;
  23. const U32 repeat = 500;
  24. ThreadPool* tp = new ThreadPool;
  25. tp->init(threadsCount);
  26. TestJobTP jobs[threadsCount];
  27. for(U32 i = 1; i < repeat; i++)
  28. {
  29. U32 iterations = rand() % 100000;
  30. for(U32 j = 0; j < threadsCount; j++)
  31. {
  32. jobs[j].in = i;
  33. jobs[j].iterations = iterations;
  34. tp->assignNewJob(j, &jobs[j]);
  35. }
  36. tp->waitForAllJobsToFinish();
  37. for(U32 j = 0; j < threadsCount; j++)
  38. {
  39. ANKI_TEST_EXPECT_EQ(jobs[j].in, i + iterations);
  40. }
  41. }
  42. }