Files
Mozilla/mozilla/xpcom/tests/TestThreadPool.cpp
darin%meer.net faa62fcf4b landing support for thread pools
git-svn-id: svn://10.0.0.236/branches/THREADS_20060213_BRANCH@190050 18797224-902f-48f8-a5cc-f745e15eee43
2006-02-15 01:41:49 +00:00

50 lines
935 B
C++

#include <stdio.h>
#include "nsXPCOM.h"
#include "nsIThreadPool.h"
#include "nsComponentManagerUtils.h"
#include "nsCOMPtr.h"
#include "nsIRunnable.h"
class Task : public nsIRunnable
{
public:
NS_DECL_ISUPPORTS
Task(int i) : mIndex(i) {}
NS_IMETHOD Run()
{
printf("###(%d) running from thread: %p\n", mIndex, (void *) PR_GetCurrentThread());
PR_Sleep(PR_MillisecondsToInterval(50));
return NS_OK;
}
private:
int mIndex;
};
NS_IMPL_THREADSAFE_ISUPPORTS1(Task, nsIRunnable)
static nsresult
RunTests()
{
nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID);
NS_ENSURE_STATE(pool);
for (int i = 0; i < 100; ++i) {
nsCOMPtr<nsIRunnable> task = new Task(i);
pool->Dispatch(task, NS_DISPATCH_NORMAL);
}
pool->Shutdown();
return NS_OK;
}
int
main(int argc, char **argv)
{
NS_InitXPCOM2(nsnull, nsnull, nsnull);
RunTests();
NS_ShutdownXPCOM(nsnull);
return 0;
}