glib学习-- timer使用

如果之让定时器执行一次,回调函数返回false,如果返回true,会定时执行。

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <glib.h>

#define true 1
#define false 0

int id = 0;

gboolean service_send_change(gpointer data)
{
	id = 0;
	printf("signal aaaaaaaaaaaaa%d\n", id);
	return true;
}

gboolean service_send_changed(gpointer data)
{
	id = 0;
	printf("signal iiiiiiiiiiiis %d\n", id);
	return true;
}

int main(int argc, char* argv[])
{
	GMainLoop* loop;
	if(g_thread_supported() == 0)
		g_thread_init(NULL);
	g_print("g_main_loop_new\n");
	loop = g_main_loop_new(NULL, FALSE);
	//Ôö¼ÓÒ»¸ö¶¨Ê±Æ÷£¬100ºÁÃëÔËÐÐÒ»´Îcallback
	id = g_timeout_add(500,service_send_changed,NULL);
	g_timeout_add(100, service_send_change,NULL);
	g_print("g_main_loop_run\n");
	g_main_loop_run(loop);
	g_print("g_main_loop_unref\n");
	g_main_loop_unref(loop);
	return 0;
}
编译:

Makefile:

</pre><pre name="code" class="cpp">CC=gcc
.PHONY:all
all:test timer watch1
test:
	$(CC) test.c  -pthread -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include  -pthread  -lgthread-2.0 -lglib-2.0  -o test
timer:
	$(CC) timer.c  -pthread -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include  -pthread  -lgthread-2.0 -lglib-2.0  -o timer
watch1:
	$(CC) watch1.c  -pthread -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include  -pthread  -lgthread-2.0 -lglib-2.0  -o watch1
	
clean:
	rm test timer watch1



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值