libev / multiple threads / multiple loops
Jamie Doran
jamie.doran81 at gmail.com
Mon Nov 3 13:39:05 CET 2014
Hi,
I am looking at using libev in a multi-threading process and am trying to
understand how callbacks are invoked on individual threads.
My test program (see below) creates a number of worker threads, each with
its own dynamic loop. As a test, I create a periodic timer on each thread
and when it expires do some work.
Because each worker thread has its own loop instance and is polling that
i.e. loop.run(0) then it should operate independent of the main thread
where the default loop is ? i.e. the invocation of the callback would be
done on the "worker" thread. Instead I see that the callbacks for all
workers are in fact executed on the main thread where the default loop is
running.
Perhaps I am misunderstanding something fundamental about libev and my
apologies if I am but I would be grateful if you could point this out to me
and why it works this way.
The attached example is only just that to illustrate the point but I would
like something where a specific worker with its own thread could do some
socket io independent of the main thread.
Does this make sense?
Thanks,
Jamie
#include <stdio.h>
#include <stdlib.h>
#include <ev.h>
#include <ev++.h>
#include<pthread.h>
pthread_barrier_t b;
void do_some_work(int id)
{
printf("%s Id= %d thr_id= %lu\n\n", __FUNCTION__, id, pthread_self());
long data = 230000;
for (int i=0;i<2000;i++) {
for (int j = 0; j < 1000; j++) {
for (long z = 0; z < 300; z++) {
data *= j;
data <<8>>8;
}
}
}
}
class Worker {
int m_id;
ev::dynamic_loop m_loop;
ev::timer m_timer;
void timeout_cb(ev::timer &watcher, int revents) {
printf("Worker: %s : Id= %d <%lu>\n", __FUNCTION__, m_id,
pthread_self());
do_some_work(m_id);
}
public:
Worker(int id) : m_id(id) {
m_timer.set<Worker,&Worker::timeout_cb>(this);
m_timer.start(0., 1);
}
void run_event_loop() {
printf("Worker: %s : id= %d thr_id= %lu\n", __FUNCTION__, m_id,
pthread_self());
pthread_barrier_wait(&b);
m_loop.run(0);
}
};
void* start_worker(void* data)
{
long id = (long)data;
Worker *worker_module =
new Worker(id);
worker_module->run_event_loop();
}
int main(int argc, char **argv)
{
ev::default_loop loop;
int err, numThreads = 1;
if (argc>1)
numThreads = strtol(&argv[1][0], NULL, 10);
pthread_barrier_init(&b, 0, numThreads+1);
pthread_t *thread_ids = (pthread_t
*)malloc(numThreads*sizeof(pthread_t));
for (int i=0;i<numThreads;i++) {
pthread_create(&thread_ids[i], NULL, start_worker, (void *)i);
}
printf("%s thr_id = %lu\n", __FUNCTION__, pthread_self());
pthread_barrier_wait(&b);
loop.run(0);
for (int i=0;i<numThreads;i++) {
pthread_join(thread_ids[i],NULL);
}
return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20141103/82ee0fe8/attachment.html>
More information about the libev
mailing list