I have a simple time tables that goes from 1 - 100. It takes like 6000 seconds to run. I figured I could break this down into a a multi thread program
main thread
for(int c = 1; c < 101; c++)
{
cout << c << "| ";
for(int i = 1; i < 101; i++)
{
cout << i * c << '\t';
}
cout << endl << endl;
}

second thread
for(int c = 51; c < 101; c++)
{
cout << c << "| ";
for(int i = 1; i < 101; i++)
{
cout << i * c << '\t';
}
cout << endl << endl;
}

I get garbage every time I run my code, since both threads work at the same time. I have no idea I tried incrementing by two and I still can't figure out what to do.

As well when you get to the end it only runs one thread.

// multi-threading.cpp : main project file.

#include "stdafx.h"
#include<iostream>
#using <mscorlib.dll>
using namespace std;
using namespace System;
using namespace System::Threading;
using namespace System::Timers;

// Simple threading scenario: Start a Shared method running
// on a second thread.
public class ThreadExample
{
public:
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
static void ThreadProc()
{
for(int c = 51; c < 101; c++)
{
cout << c << "| ";
for(int i = 1; i < 101; i++)
{
cout << i * c << '\t';
}
cout << endl << endl;
Thread::Sleep(0);
}
}
};

int main()
{
Console::WriteLine("Main thread: Start a second thread.");
// Create the thread, passing a ThreadStart delegate that
// represents the ThreadExample::ThreadProc method. For a
// delegate representing a static method, no object is
// required.
Thread ^oThread = gcnew Thread(gcnew ThreadStart(&ThreadExample::ThreadProc));

// Start the thread. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
oThread->Start();
//Thread::Sleep(0);

for(int c = 1; c < 101; c++)
{
cout << c << "| ";
for(int i = 1; i < 101; i++)
{
cout << i * c << '\t';
}
cout << endl << endl;
Thread::Sleep(0);
}

Console::WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
oThread->Join();
//Console::WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
//Console::ReadLine();
return 0;
}

As well the thread code came from msdn

I just need to code to use more than one thread and show some sign of speed increase.

Thanks

I'd guess that what's holding up the program is printing to the console output, which is a slow process. Try writing to a file instead and it'll probably help with speed. Then see if you need to thread the application. If so come back to us.

The assignment states that I must use threads. The book we use doesn't even show code as it is an AI class. I have never used them before, but I understand C++ a little bit. I just thought if the program had two threads running at the same time it would be faster, it really doesn't have to be.

My main focus is using threads and getting the desired output when as it would look without using threads.

Correction 6000ms

I have somewhat created this program in C# from another layout how do I go about changing C# to C++

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

public class Printer
{
    public void Printnumbers()
    {
        Console.WriteLine("-> {0} is executing PrintNumbers()",
            Thread.CurrentThread.Name);

        Console.WriteLine("Your Numbers: ");
        for(int i = 1; i < 11; i++)
        {
            for (int c = 1; c < 11; c++)
            {
                Console.Write(i * c + ",");
                Thread.Sleep(2000);
            }
        }
        Console.WriteLine();
    }
}

namespace CSharptest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("****** 1 - 100 ******\n");
            Console.Write("Do you want [1] or [2] threads? ");
            string threadCount = Console.ReadLine();

            Thread primaryThread = Thread.CurrentThread;
            primaryThread.Name = "Primary";

            Console.WriteLine("-> {0} is executing Main()",
            Thread.CurrentThread.Name);

            Printer p = new Printer();

            switch (threadCount)
            {
                case "2":
                    Thread backgroundThread =
                        new Thread(new ThreadStart(p.Printnumbers));
                    backgroundThread.Name = "Secondary";
                    backgroundThread.Start();
                    break;

                case "1":
                    p.Printnumbers();
                    break;

                default:
                    Console.WriteLine("I don't know what you want...you get 1 thread.");
                    goto case "1";
            }

          
        }
    }
}

I have done most of the code to C++ left with 4 errors, that I don't know how to translate.

// test.cpp : main project file.

#include "stdafx.h"
#using <mscorlib.dll>
#include <cstring>
#include <string>
#include <string.h>
#include <iostream>

using namespace std;
using namespace System;
using namespace System::Diagnostics;
//using System::Collections;
//using System::Text;
using namespace System::Threading;
using namespace System::Timers;


public class Multiply
{
	public:
		static void PrintCalculations()
		{
			Console::WriteLine("-> {0} is executing PrintNumbers()", Thread::CurrentThread);

			Console::WriteLine("Your Numbers: ");
			for(int i = 1; i < 11; i++)
			{
				for (int c = 1; c < 11; c++)
				{ 
					Console::Write(i * c + ",");
					Thread::Sleep(2000);
				}
			}
			Console::WriteLine();
		}
};
int main (array<System::String ^> ^args)
{
	int threadCount ;
	Console::WriteLine("****** 1 - 100 ******\n");
	Console::Write("Do you want [1] or [2] threads? ");
	cin>>threadCount;
	

	Thread primaryThread = Thread::CurrentThread;
	primaryThread.Name = "Primary";

	Console::WriteLine("-> {0} is executing Main()",
		Thread::CurrentThread);

	Multiply p = new Multiply();
	switch (threadCount)
	{
	case '1':
		p.PrintCalculations();
		break;

	case '2':
		Thread ^backgroundThread =
			gcnew Thread(gcnew ThreadStart(&Multiply::PrintCalculations));
		backgroundThread->Name = "Secondary" ;
		backgroundThread->Start();
		break;

	default:
		Console::WriteLine("I don't know what you want...you get 1 thread.");
		goto case '1';
	}

	return 0;
}

Thread primaryThread = Thread::CurrentThread;
This is creating the thread and seems to be appointing priority.

Multiply p = new Multiply();
Create a new instance.

default:
What gives that looks correct

goto case '1'
goto '1'
how do i jump back to a case within a case.

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.