Single-line Chengdo event source processing based on glib

Source: Internet
Author: User
Tags prepare stdin

Traditional implementations command line input generally uses scanf or getchar system calls, which requires a separate thread to detect user input when the program is implemented, which involves a series of problems with multithreaded programming. GLib's mainloop mechanism can provide a single line Chengdo event source injection, so long as we convert user input to Gsource, we can implement a single-threaded concurrency model that can respond to user input while at the same time handle other events in the input gap.
1. Basic Concepts
1.1 Gmaincontext
1.2 Gmainloop
1.3 Gsource
1.4 Poll mechanism
1.5 File Descriptor
2. Realize
2.1 Create a Gmainloop
loop = G_main_loop_new (NULL, FALSE);
Parameter description:
Null refers to the Gmainloop based Gmaincontext,null parameter that represents the Mainloop based on the glib default context.
2.2 Create a Gsource
2.2.1 Provides a callback function structure for Gsource
Static Gboolean Prepare (Gsource * source, Gint * timeout_)
{
* Timeout_ =-1;


return FALSE;
}


Static Gboolean Check (Gsource * source)
{
Gboolean _result = FALSE;




if (fd.revents = = Pollin)
{
_result = TRUE;
}


return _result;
}




Static Gboolean Dispatch (Gsource * source, Gsourcefunc callback, Gpointer User_data)
{
Gchar _cmd[100] = {0};


Read (0, _cmd, sizeof (_cmd));


Switch (_cmd[0])
{
Case ' H ':
Printusage ();
Break
Case ' P ':
Break
Case ' x ':
G_main_loop_quit (loop);
Break
Case ' > ':
Break
Case ' < ':
Break
Default
Break
}




printf (">");
Fflush (stdout);


return g_source_continue;
}
static Gsourcefuncs Funcs =
{
. prepare = Prepare,
. Check = check,
. Dispatch = Dispatch
};
2.2.2 provides stdin FD for Gsource
static GPOLLFD FD =
{
. FD = 0,
. Events = Pollin,
. revents = 0
};
2.2.3 Create Gsource
_source = g_source_new (&funcs, sizeof (Gsource));
G_source_attach (_source, NULL);
2.2.4 Add stdin fd for Gsource
G_source_add_poll (_source, &AMP;FD);
2.2.5 Release Gsource Reference
G_source_unref (_source);/Be sure to do otherwise memory leaks
2.3 Running Gmainloop
G_main_loop_run (loop);


At this point, the command-line interface has been created, and after the program is run, users can enter commands through the console. Developers can then attach additional gsource on the mainloop so that the Mainloop can handle additional concurrent events.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.