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