Multithreading?

This is a discussion on Multithreading? within the C++ Programming forums, part of the General Programming Boards category; I've been fooling around with some things and have come to a situation I'm not sure how to deal with. ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    7

    Multithreading?

    I've been fooling around with some things and have come to a situation I'm not sure how to deal with.

    I'm using code::blocks in xp with the curses library.

    I have a function:

    Code:
    void type(int row, int col, char mesg[256], int delay)
    {
        for(int i=0;i<strlen(mesg);++i)
        {
            mvprintw(row, col+i, "%c", mesg[i]);
            refresh();
            Sleep(delay);
        }
    }
    It prints your message at row,col on the console one letter at a time, waiting a little bit between each letter.

    This works fine, but what I want to do is type out two messages simultaneously.
    I've looked around google and these forums but everything I've seen on multithreading is way over my head. So I suppose what I'm looking for is an easy to understand method of running two instances of a function simultaneously, or a multithreading lesson aimed at semi-new programmers.

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Try
    mvprintw(row1, col+i, "&#37;c", mesg1[i]);
    mvprintw(row2, col+i, "%c", mesg2[i]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    7
    That would just print mesg1, then print mesg2. I'm looking to do both at the same time.

    Erm. Actually, I see what you meant. With my function adding another argument to allow a second mesg would print both of them simultaneously. But that would mean any time I needed 2 or more messages printed at the same time I'd have to add another line to the function. And if I wanted to use only one message, I would no longer be able to because the function demands additional arguments.
    Last edited by John Gaden; 08-22-2008 at 01:47 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    There is no such thing "at the same time", even with threads.
    Even if you have many physical processors, making it happen "at the same time" down to the nearest clock tick will be impossible.

    It's all an illusion.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    7
    I understand that. I'm just looking for the closest way to accomplish semi-simultaneousness.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Then use threads. They are not terribly difficult to use for something so simple. There is however a certain minimum overhead in creating them, which pretty much exceeds the complexity of your existing code, which means a lot of work for something you are probably better off doing some other way.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    Then use threads. They are not terribly difficult to use for something so simple. There is however a certain minimum overhead in creating them, which pretty much exceeds the complexity of your existing code, which means a lot of work for something you are probably better off doing some other way.
    Also bear in mind that a function like printf MAY NOT execute in two threads at the same time anyways, because it serializes the calls, to avoid complex issues that happen when you have to process two calls to the same piece of code at once.

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

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Are you sure the curses library is multithread safe?

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by matsp View Post
    Also bear in mind that a function like printf MAY NOT execute in two threads at the same time anyways, because it serializes the calls, to avoid complex issues that happen when you have to process two calls to the same piece of code at once.

    --
    Mats
    are you sur eit serializes them? I recall it executign them in parallel last time i checked, although th output was of course a garbage mix of the two strings. A critical section woudl of course fix that, which is a topic the OP woudl need to learn for threads anyway.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    are you sur eit serializes them? I recall it executign them in parallel last time i checked, although th output was of course a garbage mix of the two strings. A critical section woudl of course fix that, which is a topic the OP woudl need to learn for threads anyway.
    It obviously depends on whether the printf is in a thread-safe library or not - a non-thread safe library may well mix different output messages. Thread-safe libraries produces serialized output - because that's the only way to guarantee that it's not bungling the two up.

    --
    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. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. multithreading in C++
    By manzoor in forum C++ Programming
    Replies: 19
    Last Post: 11-28-2008, 11:20 AM
  3. Question on Multithreading
    By ronan_40060 in forum C Programming
    Replies: 1
    Last Post: 08-23-2006, 07:58 AM
  4. Client/Server and Multithreading
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 03:53 AM
  5. Multithreading
    By JaWiB in forum Game Programming
    Replies: 7
    Last Post: 08-24-2003, 09:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21