首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多线程使用libev

多线程使用libev
EN

Stack Overflow用户
提问于 2013-01-31 08:19:19
回答 2查看 15.6K关注 1票数 6

我想使用libev和多个线程来处理tcp连接。我想要的是:

  1. 主线程侦听传入的连接,接受连接并将连接转发给工作线程。
  2. 我有一堆工作线程。线程的数量取决于cpu的数量.每个工作线程都有一个事件循环.工作线程侦听我是否可以在tcp套接字上写入,或者是否有什么东西可供阅读。

我查看了libev的文档,我知道这可以用libev完成,但是我找不到任何例子,我必须这样做。

有人有例子吗?我认为我必须使用ev_loop_new() api,对于工作线程,对于主线程,我必须使用ev_default_loop()?

问候

EN

回答 2

Stack Overflow用户

发布于 2013-02-08 19:27:09

下面的代码可以扩展到多个线程

代码语言:javascript
复制
//This program is demo for using pthreads with libev.
//Try using Timeout values as large as 1.0 and as small as 0.000001
//and notice the difference in the output

//(c) 2009 debuguo
//(c) 2013 enthusiasticgeek for stack overflow
//Free to distribute and improve the code. Leave credits intact

#include <ev.h>
#include <stdio.h> // for puts
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t lock;
double timeout = 0.00001;
ev_timer timeout_watcher;
int timeout_count = 0;

ev_async async_watcher;
int async_count = 0;

struct ev_loop* loop2;

void* loop2thread(void* args)
{
    printf("Inside loop 2"); // Here one could initiate another timeout watcher
    ev_loop(loop2, 0);       // similar to the main loop - call it say timeout_cb1
    return NULL;
}

static void async_cb (EV_P_ ev_async *w, int revents)
{
    //puts ("async ready");
    pthread_mutex_lock(&lock);     //Don't forget locking
    ++async_count;
    printf("async = %d, timeout = %d \n", async_count, timeout_count);
    pthread_mutex_unlock(&lock);   //Don't forget unlocking
}

static void timeout_cb (EV_P_ ev_timer *w, int revents) // Timer callback function
{
    //puts ("timeout");
    if (ev_async_pending(&async_watcher)==false) { //the event has not yet been processed (or even noted) by the event loop? (i.e. Is it serviced? If yes then proceed to)
        ev_async_send(loop2, &async_watcher); //Sends/signals/activates the given ev_async watcher, that is, feeds an EV_ASYNC event on the watcher into the event loop.
    }

    pthread_mutex_lock(&lock);     //Don't forget locking
    ++timeout_count;
    pthread_mutex_unlock(&lock);   //Don't forget unlocking
    w->repeat = timeout;
    ev_timer_again(loop, &timeout_watcher); //Start the timer again.
}

int main (int argc, char** argv)
{
    if (argc < 2) {
        puts("Timeout value missing.\n./demo <timeout>");
        return -1;
    }
    timeout = atof(argv[1]);

    struct ev_loop *loop = EV_DEFAULT;  //or ev_default_loop (0);

    //Initialize pthread
    pthread_mutex_init(&lock, NULL);
    pthread_t thread;

    // This loop sits in the pthread
    loop2 = ev_loop_new(0);

    //This block is specifically used pre-empting thread (i.e. temporary interruption and suspension of a task, without asking for its cooperation, with the intention to resume that task later.)
    //This takes into account thread safety
    ev_async_init(&async_watcher, async_cb);
    ev_async_start(loop2, &async_watcher);
    pthread_create(&thread, NULL, loop2thread, NULL);

    ev_timer_init (&timeout_watcher, timeout_cb, timeout, 0.); // Non repeating timer. The timer starts repeating in the timeout callback function
    ev_timer_start (loop, &timeout_watcher);

    // now wait for events to arrive
    ev_loop(loop, 0);
    //Wait on threads for execution
    pthread_join(thread, NULL);

    pthread_mutex_destroy(&lock);
    return 0;
}
票数 8
EN

Stack Overflow用户

发布于 2013-01-31 09:00:42

只要每个线程运行自己的loop1,在不同线程中同时使用libev就可以了。libev ( c++ )中的c++包装器(ev++.h)总是使用默认循环,而不是让您指定要使用的循环。您应该使用C头(ev.h),它允许您指定使用哪个循环(例如,ev_io_start接受指向ev_loop的指针,而ev::io::start没有)。

您可以通过ev_loop安全地发送另一个线程的ev_async信号。

1

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14621261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档