Hello,

I have a command line program which opens other programs. I want to make it multi-threaded so multiple programs can be opened at one time.

Following is my code with using thread. I tried to incorporate threads using this online tutorial, but it did not help. [http://www.computersciencelab.com/MultithreadingTut1.htm]

#include <iostream>
using namespace std;
#include <windows.h>
#include <process.h>	// needed for _beginthread()


/**
 *
 * @return user_input 
 */

int user_interface()
{
	int user_input = 5;

	cout << "What would you like to do?" << endl;
	cout << "1) Open Internet Explorer" << endl;
	cout << "2) Open Microsoft Word" << endl;
	cout << "3) Open Calculator" << endl;
	cout << "4) Enter a command" << endl;
	cout << "5) Quit" << endl << endl;

	cout << "Enter an option number of your choice to perform a task." << endl;
	cout << "Invalid option numbers will terminate the program." << endl << endl;

	cin >> user_input;

	cout << endl;

	return user_input;
}

/**
 * 
 * @return 0 - exit program
 *         1 - continue
 */

int exec_cmd(int user_input)
{
	string cmd;
	switch(user_input)
	{
	case 1:

		return 1;
	case 2:

		return 1;
	case 3:
		cmd = "calc";
		system(cmd.c_str());
		return 1;
	case 4:

		return 1;
	case 5:

		return 1;
	}
	return 0;
}

int main (void) 
{
	
	int user_input = 0;
	int continue_flag = 1;

	user_input = user_interface();

	// this instruction is for single thread program
	exec_cmd(user_input);

//	_beginthread( exec_cmd, 0, (int)12 );

//	exec_cmd( (int)-5 );
	
//	Sleep(100);

	cin.get();
	return 0;
}

I have commented out the code that I wrote for multi-threading.

I wouldn't use multi-threading in this type of application.

You might look at using something from the _spawn family of functions using the _P_NOWAIT setting in place of your system() call. They start child programs, but do not wait for them to complete.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.