ThreadTest.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 
  2. #include "../testTools.h"
  3. #include "../../DFPSR/base/threading.h"
  4. #include "../../DFPSR/api/timeAPI.h"
  5. // The dummy tasks are too small to get a benefit from multi-threading. (0.18 ms overhead on 0.04 ms of total work)
  6. START_TEST(Thread)
  7. { // Basic version iterating over lambdas in a dynamic list
  8. const int jobCount = 50;
  9. int results[jobCount] = {}; // TODO: Make a safer example.
  10. List<std::function<void()>> jobs;
  11. for (int i = 0; i < jobCount; i++) {
  12. int* result = &results[i];
  13. jobs.push([result, i](){
  14. // Simulate a heavy workload
  15. time_sleepSeconds(0.01f);
  16. *result = i * 26 + 43;
  17. });
  18. }
  19. double totalStartTime = time_getSeconds();
  20. threadedWorkFromList(jobs);
  21. printText("Completed all jobs in ", (time_getSeconds() - totalStartTime) * 1000.0, " ms\n");
  22. for (int i = 0; i < jobCount; i++) {
  23. ASSERT_EQUAL(results[i], i * 26 + 43);
  24. }
  25. }
  26. { // Threaded split for automatic division of a big number of jobs
  27. List<int> items;
  28. for (int i = 0; i < 100; i++) {
  29. items.push(0);
  30. }
  31. int rowStart = 10; // Inclusive
  32. int rowEnd = 90; // Exclusive
  33. double totalStartTime = time_getSeconds();
  34. int* itemPtr = &items[0];
  35. threadedSplit(rowStart, rowEnd, [itemPtr](int startIndex, int stopIndex) {
  36. for (int i = startIndex; i < stopIndex; i++) {
  37. itemPtr[i] += i * 10;
  38. }
  39. });
  40. printText("Completed all jobs in ", (time_getSeconds() - totalStartTime) * 1000.0, " ms\n");
  41. for (int i = 0; i < rowStart; i++) {
  42. ASSERT_EQUAL(items[i], 0);
  43. }
  44. for (int i = rowStart; i < rowEnd; i++) {
  45. ASSERT_EQUAL(items[i], i * 10);
  46. }
  47. for (int i = rowEnd; i < items.length(); i++) {
  48. ASSERT_EQUAL(items[i], 0);
  49. }
  50. }
  51. END_TEST