| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Classes - Threads are sharing
I have a issue with muti threading that I dont fully understand.
Both my threads are calling a function called Message(string, x, y, color); void Message(string text, int x, int y, int textc) { gotoxy(x,y); textcolor(textc); std::cout<<text<<endl; } The 1st thread works just fine, But when the second calls it it mostly outputs the stuff from the 1st thread. gotoxy, and textcolor Code:
int textcolor(WORD Color)
{
HANDLE h;
h = GetStdHandle ( STD_OUTPUT_HANDLE );
return SetConsoleTextAttribute ( h, Color);
}
void gotoxy(short x, short y)
{
wHnd=GetStdHandle(STD_OUTPUT_HANDLE);
COORD c;
c.X=x; c.Y=y;
SetConsoleCursorPosition(wHnd,c);
}
Any sugestions, Would i need to make those 2 functions threaded or somthing, or just pause the thread that wants it and then pasue the other one so it can use it |
|
#2
|
|||
|
|||
|
do you want to do those two things at the same time?
|
|
#3
|
||||
|
||||
|
Quote:
You ARE synchronizing calls to that function, right? The whole function is a critical section. All those calls act on the console which you must consider a shared resource. (As such, a global console lock would be appropriate.) In other words, if thread X starts executing that function before thread Y finishes the call, you'll get effects like that.
__________________
Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Classes - Threads are sharing |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|