c++ - Multi Threading gives some weird results -
i used multi threading. works gives weird results , every time run program results different.can tell me doing wrong
now in output i'm supposed have 2 blocks continuously move , down in actual output other blocks appear out of can see output in link
(i.e excluded global char array main code adding code section can run code , me )
main code
#include<iostream> //libraries #include<stdio.h> #include<conio.h> #include<windows.h> #include<thread> using namespace std; void print(); void gotoxy(int,int); int race(); void block1(); void block2(); coord coord={0,0};//global variable char road[22][80]={//haven't mentioned array }; //global char array void main() { race(); } int race() { char swap; //printing array once cout<<endl; for(int i=0 ; i<22 ; i++) { for(int j=0 ; j<80 ; j++) { cout<<road[i][j]; } } thread t1 (block2); thread t2 (block1); t1.join(); t2.join(); getch(); return 0; } void block1() //definition of block1 { char swap; int i=1 , j=4; while(!kbhit()) { while(road[i+1][j]!='\xdb') { swap=road[i][j]; road[i][j]=road[i+1][j]; road[i+1][j]=swap; gotoxy(4,i+1); cout<<road[i][j]; gotoxy(4,i+2); cout<<road[i+1][j]; sleep(200); i++; } if(road[i+1][j]=='\xdb') while(road[i-1][j]!='\xdb') { swap=road[i][j]; road[i][j]=road[i-1][j]; road[i-1][j]=swap; gotoxy(4,i); cout<<road[i-1][j]; gotoxy(4,i+1); cout<<road[i][j]; sleep(500); i--; } } } void block2() //definition of function block2 { char swap; int i=1 , j=7; while(!kbhit()) { while(road[i+1][j]!='\xdb') { swap=road[i][j]; road[i][j]=road[i+1][j]; road[i+1][j]=swap; gotoxy(7,i+1); cout<<road[i][j]; gotoxy(7,i+2); cout<<road[i+1][j]; sleep(50); i++; } if(road[i+1][j]=='\xdb') while(road[i-1][j]!='\xdb') { swap=road[i][j]; road[i][j]=road[i-1][j]; road[i-1][j]=swap; gotoxy(7,i); cout<<road[i-1][j]; gotoxy(7,i+1); cout<<road[i][j]; sleep(50); i--; } } }
global char array
char road[22][80]={ //global char array '\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb', '\xdb','.','.','.','\xdb','.','.','\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb',1,'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\xdb', '\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb','\xdb', };
you have race condition when outputting screen. example, if thread 1 running
gotoxy(4,i+1); cout<<road[i][j];
and execution switches thread 2 after gotoxy(4,i+1);
. thread 2 own
gotoxy(7,i+1); cout<<road[i][j];
the location of output cursor (8, i+1). when thread 1 gets run again, use cout<<road[i][j];
output current cursor, in incorrect place.
to fix this, have use race protection mechanisms, πάντα ῥεῖ said. mutex work.
Comments
Post a Comment