简体   繁体   中英

Simulated Concurrency in C

I am aware that there are available functions and libraries that allow concurrency in c.

(pthread.h, fork() etc.)

However, I was wondering if there is any way to simulate concurrency in C, without actually having to use more than one thread?

Example scenario:

There is a main program loop running and outputting calculations. Somehow another function notifies the main function that the time is now 12:00 pm and the program should stop outputting calculations as the user is going off to lunch. At 12:30 this function notifies the main function to start outputting calculations again.

Can someone point me in the right direction on how to do this?

EDIT:

In essence I believe there should be 2 ways to do this:

The first would be the main program constantly checking the alternate function so it knows when the clock hits 12:00. (this is very simple and I know how to do this)

The second would be having the alternate function contact the main program whenever required (ie at 12:00 and 12:30).

It seems to me like you just need a simple timer you could use alarm() for that:

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

volatile unsigned stop_working = 0;

void alarm_handler(int signum)
{
    stop_working = 1;
    //alarm(1); //reschedule alarm
}

int main()
{        
    signal(SIGALRM, alarm_handler);
    alarm(1); //schedule alarm in seconds

    while (!stop_working) {
      //do some work
    }
}

If however, you want to have something like coroutines you should look into user-level threads (or fibers ) using functions like makecontext() and swapcontext()

Note: this example is UNIX/Linux specific, for MinGW you will need to use winapi, using functions like CreateWaitableTimer() , SetWaitableTimer() , and so on. See MSDN for details.

A concurrency model (simulated or not) would make sense if you had multiple tasks that you need to run at the same time. In this case it appears you only have one task to run.

If the only requirement that you have is that your calculations stop at 12:00 and restart at 12:30 then all you need is to make sure your calculations can be divided into small steps, so that you have a chance to check the conditions for starting and stopping in between steps. Here is some pseudo-code:

while (true) {
    current_time = get_current_time();
    if (current_time >= 12:00 and current_time < 12:30)
        sleep(12:30 - current_time);
    else
        perform_calculation_step();
}

If the starting and stopping conditions are more complex than what you indicated in your example you could abstract the start/stop logic into a separate function. Example:

while (true) {
    wait_if_necessary(); // this blocks when the app is not allowed to run
    perform_calculation_step()
}

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM