Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample code for asynchronous programming in C

Tags:

asynchronous

I need to program asynchronous ODBC driver,which need to handle user requested ODBC APIs in asynchronous way. I am desperate to know how to write an asynchronous program portable on all platforms. Can you please provide me a basic C code on how to right asynchronous code?

Thanks in advance.

like image 219
Nagasai Sowmya Avatar asked Jan 21 '10 11:01

Nagasai Sowmya


People also ask

What is asynchronous programming in C?

What is asynchronous programming? Asynchronous programming is an effective way to reduce the delay or wait time that is happening in the code. It avoids the following scenario - an activity is blocked in a synchronous process, which will in turn block the entire application by blocking the other tasks from executing.

Does C have async?

With the new C++11 standard, there is std::async . Pretty much anything the machine is capable of can be done in C, you just have to do it yourself.

How do you create asynchronous method?

You can use await Task. Yield(); in an asynchronous method to force the method to complete asynchronously. Insert it at beginning of your method and it will then return immediately to the caller and complete the rest of the method on another thread.

What is C# asynchronous programming model?

C# has a language-level asynchronous programming model, which allows for easily writing asynchronous code without having to juggle callbacks or conform to a library that supports asynchrony. It follows what is known as the Task-based Asynchronous Pattern (TAP).


2 Answers

Take below as example, notice async mostly used in multi-thread,

// FILE NAME: a.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

typedef void (*pcb)(int a);
typedef struct parameter
{
    int a;
    pcb callback;
} parameter;

void *callback_thread(void *p1)
{
    //do something
    parameter *p = (parameter *)p1;
    while (1)
    {
        printf("GetCallBack print! \n");
        sleep(3); //delay 3s
        p->callback(p->a);
    }
}

extern int SetCallBackFun(int a, pcb callback)
{
    printf("SetCallBackFun print! \n");
    parameter *p = malloc(sizeof(parameter));
    p->a = 10;
    p->callback = callback;

    pthread_t thing1;
    pthread_create(&thing1, NULL, callback_thread, (void *)p);
    pthread_join(thing1, NULL);
}
// FILE NAME: b.c
#include "boo.c"
#include <stdio.h>

void fCallBack(int a)
{
    //do something
    printf("a = %d\n",a);
    printf("fCallBack print! \n");
}


int main(void)
{
    SetCallBackFun(4,fCallBack);

    return 0;
}

Output is below,

SetCallBackFun print!
GetCallBack print!
a = 10
fCallBack print!
GetCallBack print!
a = 10
fCallBack print!
GetCallBack print!
a = 10
fCallBack print!
GetCallBack print!
a = 10
fCallBack print!
GetCallBack print!
a = 10
fCallBack print!
GetCallBack print!

...

In terms of calling function, there are three types: sync, back, and async.

The tricky thing is the last two are highly correlated, why is that?

Perhaps a graph would make it clear, i.e.

enter image description here

like image 194
LinconFive Avatar answered Oct 12 '22 20:10

LinconFive


tidy code for asynchronous IO is a good thread to start in.

Portable solutions don't really exist. It also differs for socket streams and files, on all platforms.

libevent is a good abstraction.

Writing ODBC is not for the faint hearted.

like image 24
Will Avatar answered Oct 12 '22 20:10

Will