Home > c# > C++ Multithreading program example using multiple terminals in the same computer

C++ Multithreading program example using multiple terminals in the same computer

January 22Hits:1
Advertisement

I am new to multithreaded programming. I am trying to run a program using two terminals in one laptop. Wherein, in these two terminals it should be registered as user1 and user2 and before the 2 programs start running, one of them should wait for the other using "thread" or "pthread"(I don't know the difference). If the user1 will enter any message to the terminal1, it should also be displayed to user2 in terminal2. And I have no idea how to start the code even after researching about threading for so many times. Please help. Thank you in advance.

#include <string> #include <iostream> #include <thread>  using namespace std;  void msg1(string msg) {     cout << "user1 says: " << msg << endl; } void msg2(string msg) {     cout << "user2 says: " << msg << endl; } int main() {     string message1, message2;     cin >> message1;     thread t1(msg1, message1);     t1.join();     cin >> message2;     thread t2(msg2, message2);     t2.join();     return 0; } 

Answers

You're trying to achieve too much at once, using threads and using multiple terminals.

The latter is easy, if you only need output: Just write the content to a file and run tailf filename in a terminale, which follow the contents of the file. You can use e.g. an std::fstream to keep the familiar interface of std::cout.

If you need input, things get a bit more complicated. In that case, you need to open a two-directional connection between a terminal window and the program. The communication channel would be e.g. TCP or some local transport. Using TCP, you could simply use telnet to connect to the program.

Concerning threads, just read any random C++ 11 tutorial on them. You could also check out the documentation of Boost.Thread, where the standard threads originated from. In any case, you will have to do some research on the topics here yourself, I'm only trying to point you in the right direction.

Related Articles

  • C++ Multithreading program example using multiple terminals in the same computerJanuary 22

    I am new to multithreaded programming. I am trying to run a program using two terminals in one laptop. Wherein, in these two terminals it should be registered as user1 and user2 and before the 2 programs start running, one of them should wait for the

  • How do I open multiple terminals?

    How do I open multiple terminals?May 15

    I just downloaded Ubuntu 11.04 and as far as I can remember, there was the option of opening multiple terminals in tabs in Ubuntu. I cannot discover this feature in 11.04. Also, as a general query, suppose I open emacs from the terminal, then I would

  • Multithreaded Programming? September 21

    Recently I've finished learning about multithreaded programming on single shared objects, but was curious about how different things would be in order to successfully program on multiple shared objects? --------------Solutions------------- This is a

  • How to run multiple terminals on remote ubuntu server through ultravnc?July 29

    I am trying to simultaneously run multiple instances of my application from terminal on remote ubuntu server. Single terminal is required for running a single instance of application. I am accessing remote server through putty and ultravnc viewer. Ev

  • On a linux server how do you use multiple terminals over a single ssh connection?May 1

    I often find myself opening several ssh connections in order to view several log files at a time with tail -f. This isn't a problem when I'm at home because I use public key encryption for password-less login. However, I will often use computer at my

  • Utility to send a command to multiple terminals?September 15

    A while ago when I did a lot more system admining, there was a little utility that would send a command to multiple terminals that I had open. So essentially I could ssh into three different servers, then type tail -f /var/log/some_error.log into the

  • How to create multiple "terminals" for a linux workstation?November 3

    I wanted to figure out how to have multiple terminals (monitor, keyboard, and mouse) use a single centralized system. I guess at this point I can get computers for around $150, and use them as dumb terminals, but I would really like to centralize abo

  • Multiple Terminals displayed as a grid in one window

    Multiple Terminals displayed as a grid in one windowNovember 16

    Is there a way to have one window display more than one terminal at once? (Not in the typical tabbed configuration, more like a grid of arbitrary number of terminal screens) Ideally I'd like to have a 3 column by 2 row window with 6 terminal instance

  • Open multiple terminals in a for loop in bashSeptember 19

    Recently i changed to lxde on Ubuntu. And after switching it changed my x-terminal-emulator to lxterm. My script was doing the following: Go through a for loop and open a new terminal window and then run a script in that terminal. It would open multi

  • How to work with a master database and multiple terminals

    How to work with a master database and multiple terminalsSeptember 18

    We have a main database in postgres used to assemble updates using flyway and modified from a single terminal. Now we want to know what the best way to work with multiple terminals making changes to the same database structure. For this we can think

  • How to execute a command in multiple Terminals by executing a command in only one?

    How to execute a command in multiple Terminals by executing a command in only one? December 1

    This question already has an answer here: Is there an SSH client which duplicates commands to multiple terminals? 2 answers I am going to have multiple machines that I am going to connect to via SSH so I am going to have multiple Terminal windows up,

  • Technology to Prevent Multiple logins to the Same ComputerAugust 17

    OK, this is a similar question to this. Actually I am trying to prevent people from multiple login into one single computer and use my application simultaneously. This is because I want to stop them from buying one license, install it on a machine, a

  • How can I run multiple Skype accounts in one computer?March 30

    I would like to run multiple Skype accounts in one computer at the same time. Is there an option to do that? It is just to separate personal Skype from business Skype. Thanks in advance --------------Solutions------------- Run your first instance as

  • Multiple Logins to the same computerAugust 17

    Is there any specific technology that allows me to multiple login to the same computer? This is useful when I have only one license for an application, and I need multiple users to access it simultaneously. The fact that I have only once license, one

  • Cloud Computing - Multiple Physical Computers, One Logical ComputerApril 29

    I know that you can set up multiple virtual machines per physical computer. I'm wondering if it's possible to make multiple physical computers behave as one logical unit? Fundamentally the way I imagine it working is that you can throw 10 computers i

  • How to integrate the Flash Player installation with multiple browsers on a given computer?May 14

    After installing Flash10 player; the installer integrated it into IE (which was where I'd started from). I assumed it would also have been integrated into my Seamonkey, and Chrome browsers. Not only is this assumption false; attempts to install it ex

  • Multiple Instances Of The Same Computer Under Network

    Multiple Instances Of The Same Computer Under NetworkApril 11

    Can anyone tell me why we have multiple instances of the same computer (SALLY) under network in the open file dialog. Please see the image below. This is not an issue in itself, however I am wondering if it is related to some file corruption issues w

  • How to use multiple SkyDrive accounts on one computer?June 9

    Is there any way how to use multiple SkyDrive accounts on one computer running MAC OS X or Windows 8? I would like to sync data from different accounts to different folder and not to merge these accounts to one. The reason is that every SkyDrive has

  • Can I login to multiple terminals with the same user safely on Linux?April 20

    By terminals I mean the ones you can switch to using Ctrl+Alt+F1-F6. I know I'm able to login multiple times but does this have any side-effects? --------------Solutions------------- *NIX is by design a multi-user system, so there is not going to be

  • Why does my computer show multiple terminals open Ubuntu 13.10November 25

    I am running a c program that records the number of terminals open and then lists them but when I run it I am shown multiple terminal windows open when only the one is, why? --------------Solutions------------- The most likely reason is that there ar

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.708 s.