1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "nsXPCOMCIDInternal.h"
11 #include "nsIThreadPool.h"
12 #include "nsComponentManagerUtils.h"
14 #include "nsIRunnable.h"
16 class Task
: public nsIRunnable
19 NS_DECL_THREADSAFE_ISUPPORTS
21 Task(int i
) : mIndex(i
) {}
25 printf("###(%d) running from thread: %p\n", mIndex
, (void *) PR_GetCurrentThread());
26 int r
= (int) ((float) rand() * 200 / RAND_MAX
);
27 PR_Sleep(PR_MillisecondsToInterval(r
));
28 printf("###(%d) exiting from thread: %p\n", mIndex
, (void *) PR_GetCurrentThread());
35 NS_IMPL_ISUPPORTS(Task
, nsIRunnable
)
40 nsCOMPtr
<nsIThreadPool
> pool
= do_CreateInstance(NS_THREADPOOL_CONTRACTID
);
41 NS_ENSURE_STATE(pool
);
43 for (int i
= 0; i
< 100; ++i
) {
44 nsCOMPtr
<nsIRunnable
> task
= new Task(i
);
45 NS_ENSURE_TRUE(task
, NS_ERROR_OUT_OF_MEMORY
);
47 pool
->Dispatch(task
, NS_DISPATCH_NORMAL
);
55 main(int argc
, char **argv
)
57 if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
60 NS_ShutdownXPCOM(nullptr);