|
I'm not sure if i should put this question in this forum or the Graphics forum, as the description in graphics forum only says DirectX,OpenGL or GDI.
So I was trying this graphics.h library that can crate simple graphic for console app. Everything works just fine until I multithreaded? it.
Ok, here is the code:
#include<graphics.h>
void drawrect(int l,int t,int r,int d,int col){
if(col==0)setcolor(RED);
else setcolor(BLUE);
rectangle(l,t,r,d);
}
void drawpiece(int l,int t,int r,int d,int col){
for(int i=1;i<5;i++)
drawrect(i*16+l,t,i*16+r,d,col);
}
DWORD WINAPI Thread2(void* lparam){
int px2=161,py2=17;
for(int i=0;i<10;i++,py2+=16,delay(100))
drawpiece(px2,py2,px2+16,py2+14,1);
}
int main()
{
initwindow(600,600,"TEST");
CreateThread(0,0,&Thread2,NULL,0,0);
int px1=17,py1=17;
for(int i=0;i<10;i++,py1+=16,delay(100))
drawpiece(px1,py1,px1+14,py1+14,0);
getch();
return EXIT_SUCCESS;
}
What i'm trying to do is to make two set of blockline. where first set is red and second set is blue.
this code makes the color mixed up.
So that's it. Any help would be greatly appreciated.
|
|
|
|
|
I have not checked your arithmetic, but it may be that the start points and limits of your rectangles are not correct.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I have checked the arithmetic and it works just fine if i don't multithread it.
This works just fine:
#include<graphics.h>
void drawrect(int l,int t,int r,int d,int col){
if(col==0)setcolor(RED);
else setcolor(BLUE);
rectangle(l,t,r,d);
}
void drawpiece(int l,int t,int r,int d,int col){
for(int i=1;i<5;i++)
drawrect(i*16+l,t,i*16+r,d,col);
}
int main()
{
initwindow(600,600,"TEST");
int px1=17,py1=17;
for(int i=0;i<10;i++,py1+=16,delay(100))
drawpiece(px1,py1,px1+14,py1+14,0);
int px2=161,py2=17;
for(int i=0;i<10;i++,py2+=16,delay(100))
drawpiece(px2,py2,px2+16,py2+14,1);
getch();
return EXIT_SUCCESS;
}
I'm really starting to think that this might be a problem from graphics.h
|
|
|
|
|
It may well be a problem with the graphics library (not the header) which is rather old and was probably not designed with multi-threading in mind. I would suggest you switch to one of the more current graphics libraries available in the Windows API.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Yes that's what i thought. I've switched to SDL and it works.
Anyway, thanks for replying to my question. Really appreciate it.
|
|
|
|
|
Hi,
This is very basic computer science problem. Look at your code cafefully.
First read all about context switching[^] and once you completely understand look over your code again.
Now ask yourself what would happen if ThreadA context switches in the middle of drawrect() and ThreadB continues execution of drawrect().
Best Wishes,
-David Delaune
|
|
|
|
|
David,
My reading of that is that it should, in theory, work. The variables used by each function are all stack variables and each thread has its own stack, so context switching should not affect it. I can only assume that there is some code in the actual library that is not thread safe.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
David,
Thanks for the reply, I've read about context switching and now I have so many question going on my head.
First thing first, what actually happen when you multithread? Does the processor run two or more processes independently at the same time , or does it still need to switch between processes?
because if it does run the two separately then why would it need to switch context?
another one is if it does run the two separately, then i'm guessing the function drawrect() can't be used at the same time and thus needs to be switched between processes. by logic, will it work if i apply semaphores to it?
So there you go. Can you help me with this? or did i confuse you with my questions?
|
|
|
|
|
Hi,
yudhistira dewanata wrote: First thing first, what actually happen when you multithread? Does the processor run two or more processes independently at the same time , or does it still need to switch between processes?
because if it does run the two separately then why would it need to switch context?
You would obviously need more physical cpu registers to execute processes completely independently if we define 'independently' as 'single task and never subject to context switches on the physical hardware'. Each operating system internally implements this in its own way[^].
I like to visualize engineering problems. As you are looking at my forum post... you can visualize your web browser application as being a node on a tree structure. The node at the very top is the probably the kernel and where the scheduling occurs. Multitasking [^] can be implemented in many ways.
yudhistira dewanata wrote: i'm guessing the function drawrect() can't be used at the same time and thus needs to be switched between processes. by logic, will it work if i apply semaphores to it?
No, because the function setcolor() is changing a global variable.
In fact I read your code before I read your question and I immediately saw the error. It is because I actually used these libraries many years ago.
Best Wishes,
-David Delaune
|
|
|
|
|
Ohh... I see...
Now i finally get it! Thanks a lot for the very good reply.
|
|
|
|
|