嵌入式 解决线程使用sleep或usleep等函数导致整个进程睡眠的问题

解决线程使用sleep或usleep等函数导致整个进程睡眠的问题:

获取当前时间的秒数

/*
	author : kj
	time : 2013-12-06 21:36
	instruction:
	get the num of the current seconds
	
*/

int get_current_time_second(void)
{
	struct tm *tmnow;
	struct timeval tv;
	gettimeofday(&tv,NULL);
	tmnow = localtime(&tv.tv_sec);
	return tv.tv_sec;
}


 

使用下面的函数接口完成替换线程中sleep计时

/*
	author : kj
	time : 2013-12-06 21:42
	function :
	done the function of time interval in separate thread
	
*/
int pthread_count_second(int time_interval)
{
	int current_seconds = 0;
	int execl_seconds = 0;
	int set_time_interval = time_interval;
    current_seconds = get_current_time_second();
	execl_seconds = current_seconds + set_time_interval;

	while(1)
	{
		current_seconds = get_current_time_second();
		if((execl_seconds - current_seconds) == 0)
		{
			break;
		}
	}

	return 0;
}


 

call the time interval api example

/*
	author : kj
	time : 2013-12-01
	instrucion:
		glitter :
		0 - direct set to board
		1 - speed slow
		2 - speed quickly
*/
int set_led_status(char *led_status,int glitter_type)
{
    //int i_temp = 0;//the times need global variable
    char set_led_status_buf[4] = {0};
	joseph_ipnc_param.joseph_ipnc_network_attr.joseph_ipnc_led_glitter_close = read_user_file("joseph_ipnc_led_glitter_close");
    if(glitter_type == 0)
    {
    	strcpy(set_led_status_buf,led_status);
		set_status_to_board(set_led_status_buf);
    }
    if(glitter_type == 1)
    {
    	strcpy(set_led_status_buf,led_status);
		while(joseph_ipnc_param.joseph_ipnc_network_attr.joseph_ipnc_led_glitter_close == 1)
		{
		    set_status_to_board(set_led_status_buf);
		    pthread_count_second(2);
		    set_status_to_board("00");//close the led
		    pthread_count_second(2);
		    joseph_ipnc_param.joseph_ipnc_network_attr.joseph_ipnc_led_glitter_close = read_user_file("joseph_ipnc_led_glitter_close");
		    //i_temp++;
		}
		//i_temp = 0;
    }

    if(glitter_type == 2)
    {
    	strcpy(set_led_status_buf,led_status);
		while(joseph_ipnc_param.joseph_ipnc_network_attr.joseph_ipnc_led_glitter_close == 1)
		{
		    set_status_to_board(set_led_status_buf);
		    pthread_count_second(1);
		    set_status_to_board("00");//close the led
		    pthread_count_second(1);
		    //i_temp++;
		    joseph_ipnc_param.joseph_ipnc_network_attr.joseph_ipnc_led_glitter_close = read_user_file("joseph_ipnc_led_glitter_close");
		}
		//i_temp = 0;
    }

    set_status_to_board(set_led_status_buf);
	alter_ipnc_param("joseph_ipnc_led_glitter_close","0");
    return 0;
}


 

你可能感兴趣的:(嵌入式 解决线程使用sleep或usleep等函数导致整个进程睡眠的问题)