g_main_loop 간단 한 loop 생 성 및 사용
1493 단어 C 문법
GMainLoop -> GMainContext -> {GSource1, GSource2, GSource3......}
#include
#include
#include
GMainLoop* loop;
gboolean callback(GIOChannel *channel)
{
gchar* str;
gsize len;
g_io_channel_read_line(channel, &str, &len, NULL, NULL);
while(len > 0 && (str[len-1] == '\r' || str[len-1] == '
'))
str[--len]='\0';
for(;len;len--)
g_print("%c",str[len-1]);
g_print("
");
if(strcasecmp(str, "quit") == 0){
g_main_loop_quit(loop);
}
g_free(str);
}
void add_source(GMainContext *context)
{
GIOChannel* channel;
GSource* source;
channel = g_io_channel_unix_new(1);
source = g_io_create_watch(channel, G_IO_IN);
g_io_channel_unref(channel);
g_source_set_callback(source, (GSourceFunc)callback, channel, NULL);
g_source_attach(source, context);
g_source_unref(source);
}
int main(int argc, char* argv[])
{
GMainContext *context;
if(g_thread_supported() == 0)
g_thread_init(NULL);
context = g_main_context_new();
add_source(context);
loop = g_main_loop_new(context, FALSE);
g_print("input string('q' to quit)
");
g_main_loop_run(loop);
g_main_loop_unref(loop);
g_main_context_unref(context);
return 0;
}