threading ?

im trying to implement a help function that can be called if i type"/help" in the console. this function can be called anytime while the main function is running.
at first i thought of creating another thread to call that function but im not sure how to use threading in c++ without using a thread class like boost or something but im trying to not to use outside libs
closed account (S6k9GNh0)
There's no reason not to use outside libs imo. Boost::thread can be statically linked.

Regardless, threading is provided through various things, the main one being your OS. For Windows, this would be Win32 Threads. For Linux, this would probably be pthreads (although I believe there's one other used internally for low-level purposes). Not sure about Mac.

C++ also provides a built-in threading class as of C++11 via std::thread which some compilers (VC++ 2012, GCC, Clang) already support.
Last edited on
So how should i create this thread that will continuously listen for the string "/help" ?
closed account (S6k9GNh0)
Well, think about how it would be provided as input. Are they providing it through the console? If so, aren't you waiting for input to do anything anyways? If not, throw the waiting off into a separate thread and do the dirty in the main thread.
well, i want the main thread call a bunch of processes meanwhile this function continously listens for "/help" from the console im not sure how to create that function.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <time.h>
#include <Windows.h>
#include <process.h>
using namespace std;

unsigned __stdcall help(void*)
{
	string temp;
	cin>>temp;
	if(temp =="/help")
	Misc::debug("help function thread");
	return 0;
}

int main()
{

	HANDLE hThread;
	unsigned threadID;

	hThread = (HANDLE)_beginthreadex(NULL,0,&help,NULL,0,&threadID);
        int temp;
	cout<<"enter a number"<<endl;
        cin>>temp;
        cout<<temp;


	WaitForSingleObject(hThread, INFINITE);
	
	system("pause");
	_endthreadex(0);

	return 0;
}


apparently this is as far as i got before noticing in order to process the function normally i have to still input for the help function first then input for the question that was asked.

im trying to enter input once in the console and it will either answer the question being asked or use help function if "/help" is typed
Last edited on
closed account (S6k9GNh0)
... I officially don't know what you're talking about.
okay okay let me start over

lets say i have the main function calling a function that is asking this "pick a number" then i wait for the input to enter which is an int.
at the same time i want another function also listening to everything i input to check for this string "/help" if the input does not equal "/help" if ignore the function and continues on what it was doing
closed account (S6k9GNh0)
You can only give one input at a time so it doesn't make sense.
alright throwing the threading idea out of the picture,
let me try to draw a picture to describe what i am talking about

ask user question expecting string input->
enter input in console ->
checks the string for certain word ->
if string passes ignore previous function ->
enter into function that originally asked the question

lets say the function that is asking the question only wants a single string to be taken in

im trying to avoid placing a conditional statement around all the input locations in my entire program just to look for one word
You could replace cin with your own class and make a function for this. Now you can avoid writing so many lines of error checking.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class MyCin
{
public:
    MyCin& operator>> (std::string rhs)
    {
        while(true)
        {
            std::cin >> rhs;
            if (rhs == "/help")
                std::cout << "Help message";
            else
                break;
        }
        return *this;
    }
}myCin;
Last edited on
actually that might work overloading the operator>> , it will save me alot of time ,unnecessary effort and error checking. thanks
Topic archived. No new replies allowed.