#include #include #include #include static int quit = 0; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static void *threadevent(void *arg) { pthread_mutex_lock(&lock); while (!quit) { pthread_mutex_unlock(&lock); virEventRunDefaultImpl(); pthread_mutex_lock(&lock); } pthread_mutex_unlock(&lock); return NULL; } static void *threadmain(void *arg) { virConnectPtr conn = arg; virDomainPtr *doms; int *domids; int i,j; int n = virConnectNumOfDomains(conn); doms = malloc(sizeof(virDomainPtr) *n); domids = malloc(sizeof(int) *n); virConnectListDomains(conn, domids, n); fprintf(stderr, "n%d\n", n); for (i = 0 ; i < n ; i++) doms[i] = virDomainLookupByID(conn, domids[i]); for (j = 0 ; j < 10 ; j++) { for (i = 0 ; i < n ; i++) { virDomainInfo info; virDomainGetInfo(doms[i], &info); fprintf(stderr, "%s %llu\n", virDomainGetName(doms[i]), info.cpuTime); } } for (i = 0 ; i < n ; i++) virDomainFree(doms[i]); free(doms); free(domids); virConnectClose(conn); return NULL; } #define NUM_THREADS 10 int main(void) { virConnectPtr conn; int i; pthread_t threads[NUM_THREADS]; pthread_t event; virEventRegisterDefaultImpl(); conn = virConnectOpen("qemu:///session"); pthread_create(&event, NULL, threadevent, NULL); for (i = 0 ; i < NUM_THREADS ; i++) { virConnectRef(conn); pthread_create(&threads[i], NULL, threadmain, conn); } for (i = 0 ; i < NUM_THREADS ; i++) { pthread_join(threads[i], NULL); } pthread_mutex_lock(&lock); quit = 1; pthread_mutex_unlock(&lock); virConnectClose(conn); pthread_join(event, NULL); return 0; }