1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
/*
* ivykis, an event handling library
* Copyright (C) 2010 Lennert Buytenhek
* Dedicated to Marija Kulikova.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <iv.h>
#include <iv_thread.h>
#include <iv_work.h>
static struct iv_work_pool pool;
static struct iv_work_item item_a;
static struct iv_work_item item_b;
static struct iv_work_item item_c;
static struct iv_work_item item_d;
static int item_count;
static void work(void *cookie)
{
char *task = cookie;
printf("performing work item %s in thread %p\n",
task, (void *)pthread_self());
sleep(1);
}
static void work_complete(void *cookie)
{
char *task = cookie;
printf("notification that work item %s is complete\n", task);
item_count--;
if (item_count == 3) {
printf("putting pool\n");
iv_work_pool_put(&pool);
}
}
int main()
{
iv_init();
iv_thread_set_debug_state(1);
IV_WORK_POOL_INIT(&pool);
pool.max_threads = 8;
iv_work_pool_create(&pool);
IV_WORK_ITEM_INIT(&item_a);
item_a.cookie = "a";
item_a.work = work;
item_a.completion = work_complete;
iv_work_pool_submit_work(&pool, &item_a);
IV_WORK_ITEM_INIT(&item_b);
item_b.cookie = "b";
item_b.work = work;
item_b.completion = work_complete;
iv_work_pool_submit_work(&pool, &item_b);
IV_WORK_ITEM_INIT(&item_c);
item_c.cookie = "c";
item_c.work = work;
item_c.completion = work_complete;
iv_work_pool_submit_work(&pool, &item_c);
IV_WORK_ITEM_INIT(&item_d);
item_d.cookie = "d";
item_d.work = work;
item_d.completion = work_complete;
iv_work_pool_submit_work(&pool, &item_d);
item_count = 4;
iv_main();
iv_deinit();
return 0;
}
|