Multiple Tasks

This is a discussion on Multiple Tasks within the C++ Programming forums, part of the General Programming Boards category; In DOS, I want to allow someone to be able to enter input while seeing output at the same time, ...

  1. #1
    Joe
    Guest

    Multiple Tasks

    In DOS, I want to allow someone to be able to enter input while seeing output at the same time, is this possible, I heard this is what we call "multi-tasking". Please help.

    Here's an instance of what i mean:

    #include <iostream.h>

    int main ()
    {

    char input;

    while (1)
    {
    cout << "Output!\n"; // output
    }

    while (1)
    {
    cin >> input; // input
    }

    return 0;
    }

    See, I want to be able to do two things at once, can someon help please?

  2. #2
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86

    Multitasking

    Hi,

    have a look at the _beginthread () function.

    Greetings
    cody
    #include "reallife.h"

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    I think this will be tricky in DOS - it's not an operating system which makes multitasking easy (unless you count things like TSRs)

    Or do you mean a win32 console program, and just called it DOS because it has a command line?
    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.

  4. #4
    Joe
    Guest
    Yeah, sorry about that, I mean the Win32 console. Will that "_beginthread" thing work with MSVC++ ?

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Yes, it works like this (you need to link with the multithreaded libraries) -

    Code:
    #include <iostream> 
    #include <process.h>
    #include <windows.h>
    
    using namespace std;
    
    void fn(void* dummy)
    {
    	while(1)
    	{
    	cout << "Output" << flush;
    	Sleep(3000);
    	}
    }
    
    void fn2(void* dummy)
    {
    	char input;
    	while(1)
    	{
    	cin >> input;
    	cout << input << flush;
    	}
    }
    using namespace std;
    
    int main()
    {
    
    	_beginthread(fn,0,0);
    	_beginthread(fn2,0,0);
    	while(1);
    	return 0;
    
    }
    zen

  6. #6
    Joe
    Guest
    I've just tried your example and have used the following libraries:

    LIBCMT.LIB
    MSVCRT.LIB

    Yet it gives me the following error:

    error : '_beginthread' : undeclared identifier

    ???

  7. #7
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    _beginthread() also needs _MT to be defined. You don't need to do all this manually, go to Project/Settings, C/C++ tab, Code Generation category and change the Run-Time library type to one of the Multithreaded types (depending on your build type and preference).
    zen

  8. #8
    Joe
    Guest
    k, that works, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

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