libevent多线程

来源:互联网 发布:北京网络推广公司排名 编辑:程序博客网 时间:2024/05/16 01:16
功能:
主线程同时监听定时器事件和IO事件,工作线程简单的监听IO事件。
主线程每隔一秒种向工作线程发送一条字符串信息
工作线程显示该信息并回显给主线程

该示例演示了主线程如何同时监听多个事件。

Makefile

  1. eventtest : eventtest.c
  2. gcc -Wall -g -levent -lpthread -o eventtest eventtest.c
  3. .PHONY : clean
  4. clean :
  5. rm eventtest -f

eventtest.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>

  5. #include <sys/types.h>
  6. #include <sys/socket.h>

  7. #include <event.h>


  8. struct event_base *workerbase;
  9. int fd[2];

  10. static void 
  11. thread_libevent_process(int fd, short which, void *arg)
  12. {
  13.     int ret;
  14.     char buf[128];

  15.     ret = read(fd, buf, 128);
  16.     if (ret > 0) {
  17.         buf[ret] = '\0';
  18.         printf("thread receive message : %s\n", buf);
  19.     }

  20.     sleep(1);

  21.     write(fd, buf, ret);


  22.     return;
  23. }

  24. static void *
  25. worker_thread(void *arg)
  26. {
  27.     int fd = *(int *)arg;

  28.     struct event ev;

  29.     event_set(&ev, fd, EV_READ | EV_PERSIST, thread_libevent_process, NULL);
  30.     event_base_set(workerbase, &ev);
  31.     event_add(&ev, 0);

  32.     event_base_loop(workerbase, 0);


  33.     return NULL;
  34. }

  35. static void
  36. main_event_process(int fd, short which, void *arg)
  37. {
  38.     int ret;
  39.     char buf[128];

  40.     ret = read(fd, buf, 128);
  41.     if (ret > 0) {
  42.         buf[ret]='\0';
  43.         printf("main thread receive echo message : %s\n", buf);
  44.     }
  45.     

  46.     return;
  47. }

  48. static void
  49. timeout_cb(int fdd, short event, void *arg)
  50. {
  51.     struct timeval tv;
  52.     struct event *timeout = arg;

  53.     write(fd[0], "Hello world!", sizeof("Hello world!") - 1);

  54.     evutil_timerclear(&tv);
  55.     tv.tv_sec = 1;
  56.     event_add(timeout, &tv);
  57. }

  58. int
  59. main (int argc, char *argv[])
  60. {
  61.     int ret;

  62.     pthread_t tid;

  63.     struct event ev;
  64.     struct event evtimeout;
  65.     struct timeval tv;

  66.     struct event_base *base;

  67.     base = event_init();
  68.     if (base == NULL) {
  69.         perror("event_init( base )");
  70.         return 1;
  71.     }

  72.     workerbase = event_init();
  73.     if (workerbase == NULL) {
  74.         perror("event_init( workerbase )");
  75.         return 1;
  76.     }



  77.     ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, fd);
  78.     if (ret == -1) {
  79.         perror("socketpair() : ");
  80.         return 1;
  81.     }


  82.     event_set(&ev, fd[0], EV_READ | EV_PERSIST, main_event_process, NULL);
  83.     event_base_set(base, &ev);
  84.     event_add(&ev, 0);

  85.     evtimer_set(&evtimeout, timeout_cb, &evtimeout);
  86.     event_base_set(base, &evtimeout);
  87.     evutil_timerclear(&tv);
  88.     tv.tv_sec = 1;
  89.     event_add(&evtimeout, &tv);

  90.     ret = pthread_create(&tid, NULL, worker_thread, (void *)&fd[1]);
  91.     if (ret != 0) {
  92.         perror("pthread_create()");
  93.         return 1;
  94.     }


  95.     event_base_loop(base, 0);


  96.     return 0;
  97. }

libevent带负载均衡的多线程使用示例 



功能:
主线程根据负载工作线程负载均衡算法,每隔一秒钟向特定的工作线程发送一条字符串信息,工作线程简单的把字符串信息打开出来。

Makefile

  1. eventtest : eventtest.c
  2. gcc -Wall -g -levent -lpthread -o eventtest eventtest.c
  3. .PHONY : clean
  4. clean :
  5. rm eventtest -f

eventtest.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>

  5. #include <sys/types.h>
  6. #include <sys/socket.h>

  7. #include <event.h>


  8. typedef struct {
  9.     pthread_t tid;
  10.     struct event_base *base;
  11.     struct event event;
  12.     int read_fd;
  13.     int write_fd;
  14. }LIBEVENT_THREAD;

  15. typedef struct {
  16.     pthread_t tid;
  17.     struct event_base *base;
  18. }DISPATCHER_THREAD;


  19. const int thread_num = 10;

  20. LIBEVENT_THREAD *threads;
  21. DISPATCHER_THREAD dispatcher_thread;
  22. int last_thread = 0;



  23. static void
  24. thread_libevent_process(int fd, short which, void *arg)
  25. {
  26.     int ret;
  27.     char buf[128];
  28.     LIBEVENT_THREAD *me = arg;

  29.     if (fd != me->read_fd) {
  30.         printf("thread_libevent_process error : fd != me->read_fd\n");
  31.         exit(1);
  32.     }

  33.     ret = read(fd, buf, 128);
  34.     if (ret > 0) {
  35.         buf[ret] = '\0';
  36.         printf("thread %llu receive message : %s\n", (unsigned long long)me->tid, buf);
  37.     }


  38.     return;
  39. }

  40. static void *
  41. worker_thread(void *arg)
  42. {
  43.     LIBEVENT_THREAD *me = arg;
  44.     me->tid = pthread_self();

  45.     event_base_loop(me->base, 0);


  46.     return NULL;
  47. }

  48. static void
  49. timeout_cb(int fd, short event, void *arg)
  50. {
  51.     struct timeval tv;
  52.     struct event *timeout = arg;

  53.     int tid = (last_thread + 1) % thread_num;        //memcached中线程负载均衡算法

  54.     LIBEVENT_THREAD *thread = threads + tid;

  55.     last_thread = tid;

  56.     write(thread->write_fd, "Hello world!", sizeof("Hello world!") - 1);

  57.     evutil_timerclear(&tv);
  58.     tv.tv_sec = 1;
  59.     event_add(timeout, &tv);
  60. }

  61. int
  62. main (int argc, char *argv[])
  63. {
  64.     int ret;
  65.     int i;
  66.     int fd[2];
  67.     struct event timeout;
  68.     struct timeval tv;

  69.     pthread_t tid;

  70.     dispatcher_thread.base = event_init();
  71.     if (dispatcher_thread.base == NULL) {
  72.         perror("event_init( base )");
  73.         return 1;
  74.     }
  75.     dispatcher_thread.tid = pthread_self();


  76.     threads = calloc(thread_num, sizeof(LIBEVENT_THREAD));
  77.     if (threads == NULL) {
  78.         perror("calloc");
  79.         return 1;
  80.     }

  81.     for (= 0; i < thread_num; i++) {
  82.         
  83.         ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, fd);
  84.         if (ret == -1) {
  85.             perror("socketpair()");
  86.             return 1;
  87.         }

  88.         threads[i].read_fd = fd[1];
  89.         threads[i].write_fd = fd[0];

  90.         threads[i].base = event_init();
  91.         if (threads[i].base == NULL) {
  92.             perror("event_init()");
  93.             return 1;
  94.         }


  95.         event_set(&threads[i].event, threads[i].read_fd, EV_READ | EV_PERSIST, thread_libevent_process, &threads[i]);
  96.         event_base_set(threads[i].base, &threads[i].event);
  97.         if (event_add(&threads[i].event, 0) == -1) {
  98.             perror("event_add()");
  99.             return 1;
  100.         }
  101.     }

  102.     for (= 0; i < thread_num; i++) {
  103.         pthread_create(&tid, NULL, worker_thread, &threads[i]);
  104.     }

  105.     evtimer_set(&timeout, timeout_cb, &timeout);
  106.     event_base_set(dispatcher_thread.base, &timeout);
  107.     evutil_timerclear(&tv);
  108.     tv.tv_sec = 1;
  109.     event_add(&timeout, &tv);

  110.     event_base_loop(dispatcher_thread.base, 0);


  111.     return 0;
  112. }


libevent 简单例子,包括一个socket的
http://blog.chinaunix.net/uid-25885064-id-3399135.html


0 0
原创粉丝点击