Thread: Threads

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    I am traying to do something very simple..
    no pausing and no resuming.
    Why is that so hard?!
    I'll try this ........ later, you may add simple examples..
    thanks anyway.
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Ok.
    Look on this code:
    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;
    }
    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)
    Do not pay attention for bounds or anything similar - it's just an example.
    Hope I was clear enough..

    -----
    rags_to_riches
    LOLLLL XDDD
    gavra.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
                                   handle = (HANDLE) _beginthread( ThreadProc,0,NULL); // create thread
    and
    Code:
        int h=*((int*)param);
    Means that you are accessing a NULL pointer - not a good idea.

    You can't arbitrarily change the calling convention of the thread function:
    Code:
    void ThreadProc(void *param, int x, int y)
    The thread function is supposed to take ONE parameter of void *, not a list of arbitrary parameters.

    You could for example pass the x, y coordinates you want as the address of a struct that contains your x and y values.

    Code:
                                   WaitForSingleObject(handle,INFINITE);
    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.

    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    I prefer using pure CreateThread, CreateMutex, CreateEvent API...
    They look nice ^_^

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by audinue View Post
    I prefer using pure CreateThread, CreateMutex, CreateEvent API...
    They look nice ^_^
    But will just complicate matters in this particular example - if nothing else because there are even more parameters to get confused about.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    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.
    gavra.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help if your comments was referring to quotes in the previous post, rather than just stating your thoughts on the matter in.

    Yep I know I diidnt know what this parameter means so I wrote NULL XD
    So, that's one of the dangers of not fully understanding what you are doing.

    oh and I didn't write this part I took it from the link I gave you.
    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.

    What do you mean "rotated monitor?"
    I was being sarcastic when I said you have rotated monitor - sorry, the joke apparently didn't get through.

    As I said it doesn't matter I wrote it to show you what I am traying to do.
    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].


    Anyway how do I make this thread?
    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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)

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    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 [:
    gavra.

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    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?"
    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].
    ---

    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 [:
    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    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 \:
    gavra.

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    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 \:
    Well, if you make a
    Code:
    struct coord 
    { 
        int x; 
        int y; 
    };
    ...
    void ThreadProc(void *param)
    {
    int i;
        struct coord xy =*((struct coord*)param);
    ...
    int main()
    {
       struct coord xy;
    ...
       xy.x = x;
       xy.y = y;
       _beginthread(..., &xy); 
    ... 
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM