[EN] Catch the issues – Multithreading tutorial


Hello..

I wanted to start some short series of entries which can help you realize multithreading potential of your applications.

As my approach in this very first entry is that you have no idea about multithreading, here we go with a very basic example how to code it using C++:


#include <stdio.h>
#include <windows.h>
#include <process.h>
#include <conio.h>

 


void threadA_sample( void * );
void threadB_sample( void * );


void mainThread(void);


int synchro;


int main()
{

printf( "Multithreading tutorial.\n" );
synchro = 0;

_beginthread( threadA_sample, 0, (void*)1 );
_beginthread( threadB_sample, 0, (
void*)2 );

 

mainThread();

Sleep( 300 ); //to be sure main process mainThread finished will not kill all threads dependent


_getch();

}

void mainThread()
{

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

    do {} while (synchro!=2 && synchro!=3);
    printf(
"MAINTHREAD: some message [%d]!\n",i);
   
if (synchro!=3) //synchronize only when all 3 threads are running
      
synchro=0;
}
synchro=3;

}


void threadA_sample(void* args)
{

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

 

    do {} while (synchro!=0 && synchro!=3);
    printf(
"THREAD[A]{%d}: some message [%d]!\n",(INT_PTR)args,i);
   
if (synchro!=3) //synchronize only when all 3 threads are running
      
synchro=1;
}
synchro =3;

}


void threadB_sample(void* args)
{

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

    do {} while (synchro!=1 && synchro!=3);
    printf(
"THREAD[B]{%d}: some message [%d]!\n",(INT_PTR)args,i);
   
if (synchro!=3) //synchronize only when all 3 threads are running
       
synchro = 2;
}
synchro =3;

}


Execution of this looks like:

Multithreading tutorial.
THREAD[A]{1}: some message [0]!
THREAD[B]{2}: some message [0]!
MAINTHREAD: some message [0]!
THREAD[A]{1}: some message [1]!
THREAD[B]{2}: some message [1]!
MAINTHREAD: some message [1]!
THREAD[A]{1}: some message [2]!
THREAD[B]{2}: some message [2]!
MAINTHREAD: some message [2]!
THREAD[A]{1}: some message [3]!
THREAD[B]{2}: some message [3]!
MAINTHREAD: some message [3]!
THREAD[A]{1}: some message [4]!
THREAD[B]{2}: some message [4]!
MAINTHREAD: some message [4]!
THREAD[A]{1}: some message [5]!
THREAD[B]{2}: some message [5]!
MAINTHREAD: some message [5]!
THREAD[A]{1}: some message [6]!
THREAD[B]{2}: some message [6]!
MAINTHREAD: some message [6]!
THREAD[A]{1}: some message [7]!
THREAD[B]{2}: some message [7]!
MAINTHREAD: some message [7]!
THREAD[A]{1}: some message [8]!
THREAD[B]{2}: some message [8]!
MAINTHREAD: some message [8]!
THREAD[A]{1}: some message [9]!
MAINTHREAD: some message [9]!
MAINTHREAD: some message [10]!
MAINTHREAD: some message [11]!
MAINTHREAD: some message [12]!
MAINTHREAD: some message [13]!
MAINTHREAD: some message [14]!
MAINTHREAD: some message [15]!
MAINTHREAD: some message [16]!
MAINTHREAD: some message [17]!
THREAD[B]{2}: some message [9]!
MAINTHREAD: some message [18]!
MAINTHREAD: some message [19]!

Idea was to execute three threads. One in main process and two in threads dependent. As far as 3 threads are running I wanted to synchronize the execution to a single iteration per single thread in a "turn". It should be true as far as all three threads are running. Later all synchronization should be dropped.

This simple example can show you some basic example of multithreading usage and shows up first issues in multithreading (synchronization which is made here in totally manual way)

To make more sense to the title of this entry, can you catch some hidden issue in above code?

Comments (0)

Skip to main content