İçeriğe geç

PCYNLITX MULTITHREADING

Cybernetic thread management technology

1. WHAT IS PCYNLITX MULTITHREADING LIBRARY

Basically, pcynlitx multithreading is a C++ template library providing thread control functions for multithreading applications. However, differently from the classical C++ multithreading applications, in pcynlitx library, the theoretical foundation of the software cybernetics is used. On the pcynlitx applications, the threads are managed by cybernetic control objects receiving feedback from both the operating system and the software developer. From the software developers perspective, pcynlitx library tries to minimize the complexity of the multithreaded software development and provides abstract software management tools.  

2. DETERMINISIM ON THE MULTITHREADING 

It is a well known fact that there are multiple program flows in multithreaded software and the behavior of the software may be unpredictable when the threads are running concurrently on the software. Because, the execution order of the threads is determined by the underlying hardware and/or the operating system and the software developer can not effect the schedulling of the threads.

In multithread computing, the execution order and the priority of the threads reflect the term of scheduling. More specifically, the scheduling is a decision about which thread will run and which thread will be suspended in a particular time interval. Therefore, the procedures and code sections on the multithreaded software applications are executed randomly and the execution order of the code sections are changed in each running of the threads. This situation has been illustrated in below image.

In the above figure, how the scheduling of the particular code segments effects internal determinism has been illustrated. The numbers that are given in the above figure represent the execution order of the code segments on the multi-thread programming applications. In Figure (a), a typical nondeterministic scheduling has been illustrated and the execution order of the code segments changes on each running of the software. However, in the Figure (b), deterministic scheduling of the threads has been illustrated and the execution order of the code segments is constant. In breif, random schedulling of the threads in multithreading leads nondeterminism and nondeterminism is the one of the reasons of the complexity that are faced on the multithreaded software applications.

2. 1 Fixing the execution order of the critical sections

Differently from the classical thread control, the software developer can fix the execution order of the critical sections wtih the help of the cybernetic thread control technology. Determination of the execution sequance of the code sections significantly improves internal determinism and This mechanism provides natural protection againt the ordering violations that are faced multithread software applications. In other words, fixing the execution order of the code sections improves internal determinizm and sipmlifies multithreaded software development.

In software systems, when the determinizm is a design requirement, generally input-output determinizm is taken into account and it reflects obtaining the same output for the same input in each running of the software. However, on the contrary, the internal determinism which reflects determinism of each code section plays crucial role on the input-output determinism and input-output determisim can not be achived without internal determisim. In the figure that is given on above, the code section is executed in an exactly same order by the threads in each running of the software. With the help of the control functions of the pcynlitx multithreading library, the execution order of the critical section can be easily achive. Off course, a similar staged computation can be performed with standart C++ library functions such as std::future-std::promise functions. However, the implementation of this mechanism is highly difficult for average software developer and it has its own drawbacks. 

A coding example which performs a comparations of the classical C++ approach and the pcynlitx aproach is given on the following PDF document.

3. THE THREAD CONTROL MECHANISM IN PCYNLITX MULTITHREADING

In the classical C++ multithreading, the block status of the threads is changed when a particular condition occurs. In this classical approach, the block status of the threads change without recognizing the identity of the threads. However, instead, in pcynlitx multithreading, the threads are controlled by means of their id numbers which are given by the software developers to the threads.  In other words, the threads are blocked using ID numbers which are given by the programmer to the threads on thread creation. Moreover, you can control the threads in therms of the names of the functions which are executed by the threads. This situation has been illustrated on the figure which is given on the right site of this section.

This new approach brings some advantages. For instance, you can block a specific thread ( the thread having particular ID number ) in a particular location. Meanwhile, you can determine the execution order of the threads in critical sections. Especially, in some cases, the threads must perform some dependent operations and this operations must be performed in a particular order. This property significantly improves the internal determinism of the multithreaded software. Beside this, the determination of the order of the messages which are used for inter-thread communication is very difficult task. This approach provides fixed order messaging between the threads.

Finally, the controlling the threads with particular ID numbers reduces the complexity of the multithreaded software development and this property also icreases logical understandability of the software. You can find the theoretical foundation of the apprach which is used in pcynlitx multithreading library on the research paper cited on the home page. In the following sections and on the documentations, the practical usage of the pcynlitx library and the properties of the classes of the pcynlitx multithreading library will be explained.

void function( ){

   mt.lock();

   if(condition){
 
      cv.wait();{
 
   }
 
   mt.unlock();
}

void function( ){

      syn.stop(1);
}

4. INTRODUCTION TO THE BASIC PCYNLITX MULTITHREADING

It has been already mentioned that the main philosopy of the pcynlitx project is simplification and abstraction on software development. Therefore, the classes which are the parts of the pcynlitx library act as abstraction layer between the software developer and the thread control operations. In pcynlitx multithreading library, there are three main classes which are used. The other classes are the variants of these classes. Basically, these classes are responsible from thread creation, control and messaging. These classes are named as “threads”, “synchronizer” and “channel” in pcynlitx library. In pcynlitx project, these classes are named as cybernetic control classes and their instances are named as cybernetic control objects. The control classes of the pcynlitx library are listed in below with pcynlitx namespace.

In pcynlitx multithreading applications, the thread class is the upper-most level wrapper classes and its instances are the compositions of  the other classes on the library. Therefore, the constructors of the thread class must be shown in below list. On the thread construction of the thread class instances, the total number of the thread to be created must be passed as a parameter to the constractor. Similarly, if the threads will be created on the inside of an object member, the address of the object must be passed as an argument to the constructor. 

The list of constructors for threads class

void function(syncronizer & syn){    

     syn.stop(1);                                                   

}

void function(syncronizer & syn){    

     syn.run(1);                                                   

}

In addition, you can block the threads with a particular id number just as seen on the above function. Similarly, you can also start execution of a particular thread with its id numbers.

4.2.2 BLOCKING THE THREADS IN THERMS OF ITS FUNCTION

In pcynlitx multithreading applications, the threads can be blocked in therms of the name of the function which is executed. The function prototypes and typical examples is given below.

Function prototypes

void stop(std::string Function_Name);

void stop(std::string Function_Name, int id);

SAMPLE -3

#include <iostream>
#include <thread>
#include <pcynlitx>

using namespace pcynlitx;


void procedures(syncronizer & syn){    


     syn.stop(“procedures”);   

     // thread-1 and thread-2 cross the function together

     /* Every threads which executes the function named

        as procedures function is stopped  */   

     /* When each thread which executes the function named

        as procedures reach the same point every thread

        start execution again  */

     syn.stop(“procedures”,3);   

     /* thread-1 and thread-2 waits until thread-3 performs a call

         to the run(“procedures”,3)  */                         
}

 


void activator_function(syncronizer & syn){

     std::cout << “\n In order to complate thread executions, press enter”;

     std::cin.get();     

     syn.run(“procedures”,3);                               
}

 

int main ( ){


    threads th(3);                                          

    for(int i=0;i<2;i++){

        th.create(procedures,i,”procedures”);       
    }

    th.create(activator_function,3,”activator_function”);       

    for(int i=0;i<3;i++){

        th.join(i);                  
    }


    return 0;
}

In the example code that is given above, each thread which executes the function named as “procedures” is blocked untill the control function stop(“procedures”) is called by every thread executing the same thread function. In other words, the threads cross the code line together. On the other hand, there is another control function on the same code section which is written as stop(“procedures”,3). In this code line, the threads executing the function which is named as procedures are blocked again when the threads perform a call to the control function stop(“procedures”,3). However, in this case, differently from the other control function, the threads wait an activation command coming from another thread executing a different function. The thread which is numbered 3 activates the other threads from another thread function. 

void procedures(syncronizer & syn){ 

     std::cout << “\n The name of the function to be executed.”;

     << syn.function_name();

}  

4.2.3 THE SERIAL EXECUTION OF THE CODE SECTION ( CRITICAL SECTIONS )

In pcynlitx multithreading applications, the code section can be executed serially. Different from the classical mutual exclution, the code section is executed in an exactly same order in each running of the software. More specifically, in each running of the software, the code sections are executed in an increasing order of the threads { 1, 2 , 3 , 4 . . . . . . }. This propertiy significaly reduces the ordering violations.

Function prototypes

void start_serial( );

void end_serial( );

void procedures(syncronizer & syn){ 

     syn.start_serial();

     // The code section to be executed serially 

     syn.end_serial();

}  

4.2.4 THE USAGE OF PCYNLITX MULTITHREADING INSIDE CLASS MEMBER FUNCTIONS

The constructor of the “threads” class differes from the classical constructor when the it is used in class member functions. The function prototypes of the threads class have been given in previous sections. However, the constructors which are used in class is given below.

template<typename T>
void threads(T * ptr, int t);

t: the number of the threads to be created on the process
T: the type of the objects in which threads are created on its member functions

template<typename T>
void threads(T * ptr, int t, channel<M> * ch);

t: the number of the threads to be created on the process
T: the type of the objects in which threads are created on its member functions
ch: a poiner to a channel class instance

Pcynlitx Software Systems

ABOUT

The pcynlitx project is carried out by Erkam Murat Bozkurt who is control systems engineer with M.Sc degree.

PRODUCTS

Pcynlitx IDE
Pcynlitx Multithreading Library

RESEARCH

The main purpose of the researches performed on pcynlitx project is to simplifiy software development with C++

LICIENCE

The software are distributed with apache software liciense

CONTACT

pcynlitx.help@gmail.com

emb.muratbozkurt@gmail.com

erkam@pcynlitx.com

The source codes that are distributed on pcynlitx projects are copyrited on U.S copyright office. Copyright © 2025. All rights reserved.