main.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2010 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. /// ThreadingDemo shows how to use the cross platform thread support interface.
  14. /// You can start threads and perform a blocking wait for completion
  15. /// Under Windows it uses Win32 Threads. On Mac and Linux it uses pthreads. On PlayStation 3 Cell SPU it uses SPURS.
  16. /// June 2010
  17. /// New: critical section/barriers and non-blocking polling for completion
  18. void SampleThreadFunc(void* userPtr,void* lsMemory);
  19. void* SamplelsMemoryFunc();
  20. #include <stdio.h>
  21. //#include "BulletMultiThreaded/PlatformDefinitions.h"
  22. #ifndef _WIN32
  23. #include "b3PosixThreadSupport.h"
  24. b3ThreadSupportInterface* createThreadSupport(int numThreads)
  25. {
  26. b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",
  27. SampleThreadFunc,
  28. SamplelsMemoryFunc,
  29. numThreads);
  30. b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo);
  31. return threadSupport;
  32. }
  33. #elif defined( _WIN32)
  34. #include "b3Win32ThreadSupport.h"
  35. b3ThreadSupportInterface* createThreadSupport(int numThreads)
  36. {
  37. b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,numThreads);
  38. b3Win32ThreadSupport* threadSupport = new b3Win32ThreadSupport(threadConstructionInfo);
  39. return threadSupport;
  40. }
  41. #endif
  42. struct SampleArgs
  43. {
  44. SampleArgs()
  45. :m_fakeWork(1)
  46. {
  47. }
  48. b3CriticalSection* m_cs;
  49. float m_fakeWork;
  50. };
  51. struct SampleThreadLocalStorage
  52. {
  53. int threadId;
  54. };
  55. void SampleThreadFunc(void* userPtr,void* lsMemory)
  56. {
  57. printf("thread started\n");
  58. SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*) lsMemory;
  59. SampleArgs* args = (SampleArgs*) userPtr;
  60. int workLeft = true;
  61. while (workLeft)
  62. {
  63. args->m_cs->lock();
  64. int count = args->m_cs->getSharedParam(0);
  65. args->m_cs->setSharedParam(0,count-1);
  66. args->m_cs->unlock();
  67. if (count>0)
  68. {
  69. printf("thread %d processed number %d\n",localStorage->threadId, count);
  70. }
  71. //do some fake work
  72. for (int i=0;i<1000000;i++)
  73. args->m_fakeWork = b3Scalar(1.21)*args->m_fakeWork;
  74. workLeft = count>0;
  75. }
  76. printf("finished\n");
  77. //do nothing
  78. }
  79. void* SamplelsMemoryFunc()
  80. {
  81. //don't create local store memory, just return 0
  82. return new SampleThreadLocalStorage;
  83. }
  84. int main(int argc,char** argv)
  85. {
  86. int numThreads = 8;
  87. b3ThreadSupportInterface* threadSupport = createThreadSupport(numThreads);
  88. for (int i=0;i<threadSupport->getNumTasks();i++)
  89. {
  90. SampleThreadLocalStorage* storage = (SampleThreadLocalStorage*)threadSupport->getThreadLocalMemory(i);
  91. b3Assert(storage);
  92. storage->threadId = i;
  93. }
  94. SampleArgs args;
  95. args.m_cs = threadSupport->createCriticalSection();
  96. args.m_cs->setSharedParam(0,100);
  97. int arg0,arg1;
  98. int i;
  99. for (i=0;i<numThreads;i++)
  100. {
  101. threadSupport->runTask(B3_THREAD_SCHEDULE_TASK, (void*) &args, i);
  102. }
  103. bool blockingWait =false;
  104. if (blockingWait)
  105. {
  106. for (i=0;i<numThreads;i++)
  107. {
  108. threadSupport->waitForResponse(&arg0,&arg1);
  109. printf("finished waiting for response: %d %d\n", arg0,arg1);
  110. }
  111. } else
  112. {
  113. int numActiveThreads = numThreads;
  114. while (numActiveThreads)
  115. {
  116. if (threadSupport->isTaskCompleted(&arg0,&arg1,0))
  117. {
  118. numActiveThreads--;
  119. printf("numActiveThreads = %d\n",numActiveThreads);
  120. } else
  121. {
  122. // printf("polling..");
  123. }
  124. };
  125. }
  126. printf("stopping threads\n");
  127. delete threadSupport;
  128. printf("Press ENTER to quit\n");
  129. //getchar();
  130. return 0;
  131. }