#include #include #include #include pthread_cond_t condReady; pthread_cond_t condRun; pthread_mutex_t lock; int running; void *runme(void *arg) { virConnectPtr conn; pthread_mutex_lock(&lock); running++; fprintf(stderr, "Notifying we are ready\n"); pthread_cond_signal(&condReady); pthread_cond_wait(&condRun, &lock); pthread_mutex_unlock(&lock); fprintf(stderr, "Opening libvirt\n"); conn = virConnectOpenAuth("qemu:///system", virConnectAuthPtrDefault, 0); fprintf(stderr, "Open %s\n", conn ? virConnectGetURI(conn) : "failed"); if (conn) virConnectClose(conn); } int main(int argc, char **argv) { int i; pthread_t th[20]; pthread_mutex_init(&lock, NULL); pthread_cond_init(&condRun, NULL); pthread_cond_init(&condReady, NULL); for (i = 0; i < 20; i++) { pthread_create(&th[i], NULL, runme, NULL); } fprintf(stderr, "Waiting for threads to initialize\n"); pthread_mutex_lock(&lock); while (running != 20) pthread_cond_wait(&condReady, &lock); fprintf(stderr, "Telling threads to proceed\n"); pthread_cond_broadcast(&condRun); pthread_mutex_unlock(&lock); for (i = 0; i < 20; i++) { pthread_join(th[i], NULL); } return 0; }