Skip to content
Start » Tutorial » Tutorial C (Dev-C) to show time in seconds – Threads demo – example of a clock

Tutorial C (Dev-C) to show time in seconds – Threads demo – example of a clock

Threads, in C, C++, Java, PHP, Python or any other programming language, are independent execution flows of the same program.

Threads are functions that run on “background”, that is, while the program executes the main() function – in C, a thread can be executing at the same time as main() in parallel.

So we can say that programming with threads is the equivalent of programming multitasking.

example context

My students, who are starting in programming techniques using the C language, are challenged with the implementation of a simple game in a text environment, and they are always faced with the desire/need to show the time or the game clock in parallel with the execution of the main logic.

In this short article I present the implementation of an example that executes two loops – loops, in the main routine (main) however, which at the same, displays in a corner of the screen the elapsed time in seconds.

In addition to the demonstration of Programming with Threads in C, it is possible to verify the use of routines such as gotoxy – cursor placement on the screen or console, and sleep – source code execution pause.

About Threads

Let's improve the concept of the introduction, a simplified explanation is to say that they are "channels" of execution of routines in systems, allowing several functions to be processed in parallel in the same application.

Exemplo de execução de rotinas em paralelo - threads em C
Example of programming and execution in threads running in parallel on the time line

In this way, programming with Threads is the equivalent of programming Multitasking orMultithreading.

Multithreading is a specialized form of multitasking, where multitasking is the resource that allows the computer to execute two or more programs or functions – routines, simultaneously.

In general, there are two types of multitasking: process-based and thread-based.

Process-based multitasking deals with the simultaneous execution of programs. Multitasking, the one that will be demonstrated in this article, is thread-based and deals with the simultaneous execution of parts of the same program.

A multithreaded program contains two or more parts that can run concurrently. Each part of this program is called a thread and each thread defines a separate execution path – as exemplified in the figure above.

The C programming language does not contain any built-in support for multithreaded applications. Instead, it relies entirely on the operating system to provide this feature.

In our simple example below, the function _beginthread() from the library process.h is being used to show the execution time in seconds, while two loops of repetition are processed in the main (function) thread.

programming environment

For the following example, the following programming environment was configured and used to create and execute the code:

Clock code using threads in C programming language

Here is the full example code:

#include #include #include #include int running; // global variable that controls if the application is running // it will guarantee the closure of open threads when its value is zero void clock() { // the long type supports non-fractional numbers // with a higher (long) value than the int type long start = time(NULL); long now; while (running != 0) { now = time(NULL); // position clock at column 1 line 1 // screen text is 80 columns by about 23 lines gotoxy(1,1); printf("Time: %d\n", now - start); // this command holds the code execution for the time in milliseconds passed by parameter // in this case we hold it in 1 second _sleep(1000); } printf("Clock stopped!\n\n"); } int main(int argc, char *argv[]) { int x; running = 1; // start the thread that will execute the clock function in parallel with the rest of the application _beginthread(clock, 0, NULL); for (x=0; x<10000; x++) { // position text in column 1 line 5 // text screen has 80 columns for about 23 lines gotoxy(1,5); printf("For do MAIN %d\n", x); } for (x=0; x // position clock at column 1 line 10 // text screen is 80 columns for about 23 lines gotoxy(1,10); printf("New For from MAIN %d\n" , x); } // setting the value of this variable to 0 ensures that the while of the clock stops executing = 0; system("PAUSE"); return 0; }

Compile the code and run it, you will see the seconds count while the system is executing the function main().

To learn more about programming read the articles on this blog with Tutorials or about programming techniques.

keep up to date

Don't forget to follow me on twitter and sign the channel on Youtube to receive updates on new articles, video lessons and free courses in software development and engineering.

Leave a Reply

Your email address will not be published.

%d bloggers like this: