"Fossies" - the Fresh Open Source Software Archive

Member "muscle/test/testthreadpool.cpp" (28 Nov 2019, 1638 Bytes) of package /linux/privat/muscle7.52.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* This file is Copyright 2000-2013 Meyer Sound Laboratories Inc.  See the included LICENSE.txt file for details. */  
    2 
    3 #include <stdio.h>
    4 #include "system/ThreadPool.h"
    5 #include "system/SetupSystem.h"
    6 
    7 using namespace muscle;
    8 
    9 class TestClient : public IThreadPoolClient
   10 {
   11 public:
   12    TestClient()
   13       : IThreadPoolClient(NULL) 
   14    {
   15       // empty
   16    }
   17 
   18    virtual void MessageReceivedFromThreadPool(const MessageRef & msgRef, uint32 numLeft)
   19    {
   20       char buf[20];
   21       printf("MessageFromOwner called in thread %s, msgRef=%p (what=" UINT32_FORMAT_SPEC "), numLeft=" UINT32_FORMAT_SPEC "\n", muscle_thread_id::GetCurrentThreadID().ToString(buf), msgRef(), msgRef()?msgRef()->what:666, numLeft);
   22       Snooze64(200000);
   23    }
   24 };
   25 
   26 // This program exercises the Thread class.
   27 int main(void) 
   28 {
   29    CompleteSetupSystem css;
   30 
   31    printf("Creating pool...\n"); fflush(stdout);
   32 
   33    ThreadPool pool;
   34    {
   35       printf("Sending TestClient Messages to pool...\n"); fflush(stdout);
   36       TestClient tcs[10];
   37       for (uint32 i=0; i<ARRAYITEMS(tcs); i++) tcs[i].SetThreadPool(&pool);
   38       for (uint32 i=0; i<10; i++) 
   39       {
   40          for (uint32 j=0; j<ARRAYITEMS(tcs); j++) 
   41          {
   42             MessageRef msg = GetMessageFromPool((j*100)+i);
   43             msg()->AddString("hey", "dude");
   44             tcs[j].SendMessageToThreadPool(msg);
   45          }
   46       }
   47 
   48       printf("Waiting for Messages to complete...\n");
   49       for (uint32 i=0; i<ARRAYITEMS(tcs); i++) tcs[i].SetThreadPool(NULL);  // this will block until all tc's Messages have been handled
   50       printf("Messages completed!\n");
   51    }
   52 
   53    printf("Exiting, bye!\n");
   54    return 0;
   55 }