[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Program halt after add event



Hiï

Thanks!

Attached please find the test code(3 files):

start.c will create 2 threads: restart1 and restart2, which will create 1 timer each.

time1.c for restart1 and

time2.c for restart2.

while compile and execute the program, restart1 can't execute to line 101 to print the debug message, and restart2 also can't execute to line 101 to print the debug message .

If you don't rewrite the timer in timeout_cb()(comment out line58-line63), it won't execute to line 101 until it executed the timeout_cb()

Michael

On Tue, Sep 11, 2012 at 11:08 AM, Nick Mathewson <nickm@xxxxxxxxxxxxx> wrote:
On Thu, Sep 6, 2012 at 6:24 PM, Michael <michaelxmail@xxxxxxxxx> wrote:
> Hi,
>
> I want use libevent as timer management tool in our program. Our program is
> multi-thread , and each thread will use some timer.
>
> I can enter into the call back function after I called the following
> functions: Â event_assign(), Âevent_add(), Âevent_base_dispatch() , it works
> fine.
>
> But I noticed that after I called the event_add(), the thread will halt,
> after executed the time-out callback function, it will execute the following
> instructions.
>
> for example: Âthe program will not execute to 'instruction 1' until it has
> executed the timer callback function.
>
> event_add(); Â//set the timer value
>
> instruntion 1;
>
>
> Is there some functions I need to call to change to non-halt ?

I think I need to see some more detailed code here to understand
what's happening. What you're describing should work, and should not
halt at all.

--
Nick
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users  Âin the body.

/* Copyright (c) 1999 by Dynatrol Systems Inc.  All rights reserved
 *
 * File Name: Restart.c
 * Author:
 * Date:
 * Description:
 *
 * Restart Procedure:
 * - Obtain Message exchange ID from DBM (Message Exchange should have been
 *   Created by DBM).
 * - Allocate Memory
 * - Create one-shot timer
 * - Trigger the Main Task
 *
 *   Parameters:
 *
 *   Return:
 *
 * 
 * Revision     By             Date           Reason
 * --------     ----------     ---------      --------------------------------
 *    2                        Nov 12, 2001   Cmd/Scan list per Port basis
 */

#include "stdio.h"
#include "pthread.h"
#include "Dynatrol.h"
#include "b01.h"
#include "b02.h"

extern void restart1(void);
extern void restart2(void);

int main(int argc, char * argv[])
{
    pthread_t id[100];
    int i,ret[100];

    printf("main thread \n");

    i = 0;

    printf("thread %d \n", i);
    ret[i]=pthread_create(&id[i],NULL,(void *)restart1,NULL);
    if(ret[i]!=0){
        printf ("Create pthread %d error!\n", i);
        exit (1);
    }

    i++;
    printf("thread %d \n", i);
    ret[i]=pthread_create(&id[i],NULL,(void *)restart2,NULL);
    if(ret[i]!=0){
        printf ("Create pthread %d error!\n", i);
        exit (1);
    }

    pthread_join(id[0],NULL);
    pthread_join(id[1],NULL);

    return (0);

}
/*
 * XXX This sample code was once meant to show how to use the basic Libevent
 * interfaces, but it never worked on non-Unix platforms, and some of the
 * interfaces have changed since it was first written.  It should probably
 * be removed or replaced with something better.
 *
 * Compile with:
 * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
 */

#include <sys/types.h>

#include <event2/event-config.h>

#include <sys/stat.h>
#ifndef WIN32
#include <sys/queue.h>
#include <unistd.h>
#endif
#include <time.h>
#ifdef _EVENT_HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>

#ifdef WIN32
#include <winsock2.h>
#endif

struct timeval lasttime, lasttime2, lasttime3, lasttime4, lasttime5;

int event_is_persistent;

static void
timeout_cb(evutil_socket_t fd, short event, void *arg)
{
	struct timeval newtime, difference;
	struct event *timeout = arg;
	double elapsed;

	evutil_gettimeofday(&newtime, NULL);
	evutil_timersub(&newtime, &lasttime, &difference);
	elapsed = difference.tv_sec +
	    (difference.tv_usec / 1.0e6);

	printf("11111timeout_cb called at %d: %.3f seconds elapsed.\n",
	    (int)newtime.tv_sec, elapsed);
	lasttime = newtime;

	if (! event_is_persistent) {
		struct timeval tv;
		evutil_timerclear(&tv);
		tv.tv_usec = 2000000;
		event_add(timeout, &tv);
	}
}

void restart1()
{
	struct event timeout;
	struct timeval tv;
	struct event_base *base;
	
	int flags;

#ifdef WIN32
	WORD wVersionRequested;
	WSADATA wsaData;
	int	err;

	wVersionRequested = MAKEWORD(2, 2);

	err = WSAStartup(wVersionRequested, &wsaData);
#endif


		event_is_persistent = 0;
		flags = 0;

	/* Initalize the event library */
	base = event_base_new();

	/* Initalize one event */
	event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);

	evutil_timerclear(&tv);
	tv.tv_usec = 2000000;
	event_add(&timeout, &tv);
	
	evutil_gettimeofday(&lasttime, NULL);
	event_base_dispatch(base);

    printf("TTTT1 \n");
	
	return (0);
}

/*
 * XXX This sample code was once meant to show how to use the basic Libevent
 * interfaces, but it never worked on non-Unix platforms, and some of the
 * interfaces have changed since it was first written.  It should probably
 * be removed or replaced with something better.
 *
 * Compile with:
 * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
 */

#include <sys/types.h>

#include <event2/event-config.h>

#include <sys/stat.h>
#ifndef WIN32
#include <sys/queue.h>
#include <unistd.h>
#endif
#include <time.h>
#ifdef _EVENT_HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>

#ifdef WIN32
#include <winsock2.h>
#endif

struct timeval lasttime, lasttime2, lasttime3, lasttime4, lasttime5;

int event_is_persistent;

static void
timeout_cb(evutil_socket_t fd, short event, void *arg)
{
	struct timeval newtime, difference;
	struct event *timeout = arg;
	double elapsed;

	evutil_gettimeofday(&newtime, NULL);
	evutil_timersub(&newtime, &lasttime, &difference);
	elapsed = difference.tv_sec +
	    (difference.tv_usec / 1.0e6);

	printf("22222timeout_cb called at %d: %.3f seconds elapsed.\n",
	    (int)newtime.tv_sec, elapsed);
	lasttime = newtime;

	if (! event_is_persistent) {
		struct timeval tv;
		evutil_timerclear(&tv);
		tv.tv_usec = 2000000;
		event_add(timeout, &tv);
	}
}

void restart2()
{
	struct event timeout;
	struct timeval tv;
	struct event_base *base;
	
	int flags;

#ifdef WIN32
	WORD wVersionRequested;
	WSADATA wsaData;
	int	err;

	wVersionRequested = MAKEWORD(2, 2);

	err = WSAStartup(wVersionRequested, &wsaData);
#endif


		event_is_persistent = 0;
		flags = 0;

	/* Initalize the event library */
	base = event_base_new();

	/* Initalize one event */
	event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);

	evutil_timerclear(&tv);
	tv.tv_usec = 2000000;
	event_add(&timeout, &tv);
	
	evutil_gettimeofday(&lasttime, NULL);
	event_base_dispatch(base);

    printf("TTTT2 \n");
	
	return (0);
}