CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Need help understanding threads

    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 everytime 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

  2. #2
    Join Date
    Nov 2009
    Posts
    2

    Re: Need help understanding threads

    solved
    fixed my problems

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured