What are you trying to do? Perhaps a thread isn't even worth the effort.
Software development isn't easy. If it were, I wouldn't get paid so much :)
Printable View
What are you trying to do? Perhaps a thread isn't even worth the effort.
Software development isn't easy. If it were, I wouldn't get paid so much :)
Well, if you show us what you are doing, it may well be simple to explain what you are (possibly) doing wrong. Without knowing what particular problem you are trying to solve makes it almost impossible to solve.
Multi-trheading makes a lot of things more complicated, because you potentially have two threads trying to use the same piece of data, which leads to interesting challenges in making sure that the data is consistent and correct, just for one example of things that can go wrong.
If you are doing something where the data accessed only from either one thread or the other thread.
--
Mats
Ok.
Look on this code:
Now, what I am traying to do is when SPACE is pressed I call to the function above (thread..) and I'll be able to getch again (I don't want to wait till the function ends for new input)Code:#include <stdio.h>
#include <windows.h>
#define LEFT 75
#define RIGHT 77
#define SPACE 32
void gotoxy(int y, int x)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void ThreadProc(void *param, int x, int y)
{
int i;
int h=*((int*)param);
for (i=1;i<10;i++)
{
gotoxy(x-i,y);
printf(".");
gotoxy(x,i-1);
printf(" ");
}
_endthread();
}
int main()
{
HANDLE handle;
int in,x=20,y=20;
gotoxy(x,y);
printf("O");
while (1)
{
in = getch();
gotoxy(x,y);
printf(" ");
switch (in)
{
case LEFT:
{
y--;
}
break;
case RIGHT:
{
y++;
}
break;
case SPACE:
{
handle = (HANDLE) _beginthread( ThreadProc,0,NULL); // create thread
WaitForSingleObject(handle,INFINITE);
}
break;
}
gotoxy(x,y);
printf("O");
}
return 0;
}
Do not pay attention for bounds or anything similar - it's just an example.
Hope I was clear enough..
-----
rags_to_riches
LOLLLL XDDD
andCode:handle = (HANDLE) _beginthread( ThreadProc,0,NULL); // create thread
Means that you are accessing a NULL pointer - not a good idea.Code:int h=*((int*)param);
You can't arbitrarily change the calling convention of the thread function:
The thread function is supposed to take ONE parameter of void *, not a list of arbitrary parameters.Code:void ThreadProc(void *param, int x, int y)
You could for example pass the x, y coordinates you want as the address of a struct that contains your x and y values.
This waits for the thread to finish. If you want to create a number of threads, and/or keep the thread running and then get back to your getch(), you obviously can't wait for the thread to finish before you continue the loop.Code:WaitForSingleObject(handle,INFINITE);
It's also slightly confusing that you are incrementing/decrementing y on left/right - I would have thought y would be best represented by up/down, and x would be left/right - but maybe you have a rotated monitor?
--
Mats
I prefer using pure CreateThread, CreateMutex, CreateEvent API...
They look nice ^_^
Yep I know I diidnt know what this parameter means so I wrote NULL XD
oh and I didn't write this part I took it from the link I gave you.
Anyway how do I make this thread?
What do you mean "rotated monitor?"
As I said it doesn't matter I wrote it to show you what I am traying to do.
It would help if your comments was referring to quotes in the previous post, rather than just stating your thoughts on the matter in.
So, that's one of the dangers of not fully understanding what you are doing.Quote:
Yep I know I diidnt know what this parameter means so I wrote NULL XD
Just copying and pasting code without understanding it doesn't actually solve anything, when that code doesn't do quite what you wanted it to do. I'm not sure, still, what you want to actually achieve... Do you want to create a thread that draws stuff at the current position each time you type space, and keeps doing so "forever"? In which case, you'd need to make an endless loop in your thread.Quote:
oh and I didn't write this part I took it from the link I gave you.
I was being sarcastic when I said you have rotated monitor - sorry, the joke apparently didn't get through.Quote:
What do you mean "rotated monitor?"
I do realize it doesn't matter, but why not do it "right in the first place" - that makes it less to change later, and you wouldn't have any comments on it being strange [and it's even less to type if you do up/down instead of left right].Quote:
As I said it doesn't matter I wrote it to show you what I am traying to do.
I don't understand your question. Your code creates a thread (albeit short-lived), since it just prints 20 characters in the ten iteration for-loop.Quote:
Anyway how do I make this thread?
--
Mats
First changed what mastp said. Create a struct that has 3 parameters and pass it to ThreadProc.
Then just remove the WaitForSingleObject. There is no logic calling a thread and pausing the main thread. That is just like calling normally a function!
So main will run read characters and each time space is pressed the TreadProc will run at the "same" time. You don't need to wait for the tread to finish, when ThreadProc finishes the thread is done.
I don't know what the program's goal is. But note that the thread will be executed at the same time with the main thread in whatever order. Which means that a gotxy() may be executed from the main thread and right after that a gotoxy() from a thread, which will make the console pointer move twice, causing errors. When two threads share data, in your case the position of the console pointer, you have to synchronize them. In your case again, that will make kind of meaningless using a thread. Except if you are "ok" with this, meaning you want to do something really random (for whatever reason)
Good point about the sharing between threads of the cursor position. Not only does it show the complexities of threading, but also it would illustrate the concept of 'even though you are not visibly sharing data, subsystems that you make use of may do so without your implicit knowledge'. In this case, the console subsystem has only one current cursor position, so two threads would compete to set it to the right place.
It woucl certainly be possible write something that uses a pair of "gotoxy()" and "printf()" (plus fflush(stdout), perhaps?) with a mutex or other way to prevent this problem. But if all we are essentially doing in each thread is to wait to be able to do gotoxy() and a short printf(), then it's pretty pointless to use threads, as you say. It would make more sense to build a data structure to track where on the screen what data should appear, and the process that data structure in one function.
--
Mats
V_V
Again I copy and paste cause I tried to show you what I am traying to do.
Old humor XD
I wont use it at least not this specific code..
So I'll ask it diffrently: "how do I create a thread that starts when I press SPACE and stops when it ends?"
---
I hate struct, and I guess I can use globals.
I didn't knew what the ........ it suposed to do (it's very stupid so why it has been "invented"? ><")
@_@ (again..)
Thanks for the patiens [:
Just don't wait for it to end then and there, and your code does the right thing [in that respect, obviously other comments on things that are wrong are still valid].
Using globals with threads is VERY error prone, if you ever have more than one thread accessing the same global/set of globals. I'd suggest you get used to using structs, as they are often very useful for a huge number of things. You can not possibly have (at least somewhat complex) software that is efficient and never use structs.Quote:
---
I hate struct, and I guess I can use globals.
I didn't knew what the ........ it suposed to do (it's very stupid so why it has been "invented"? ><")
@_@ (again..)
Thanks for the patiens [:
--
Mats
The concept of multi-threading is not difficult. Using multiple threads and getting it right is difficult.
The fact that you seem to have a hard time understanding even basic code that creates a thread suggests that you are not ready for such a leap.
I recommend that you continue studies deeper into the languages to get proper understanding before attempting threads. Structs is one such thing, along with compiler warnings (you should have gotten at least ONE with that code), and proper understanding of pointers, as well as functions.
When you feel the code to start the thread above no longer looks alien to you, then maybe you are ready to start programming with threads.
But it's still not good ("NULL").
You right again I didn't thought about accessing to the memory at the same time.
It's complicated \: