Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread Programming in C++/CLR

I am really struggling with Thread programming in Visual C++/CLR. I have searched a lot and found lots of material on the internet including the official resources however i am still confused. There are very few resources for C++/CLR. Most of them are for C# or old style C++. I try to run this simple code. I choose a new project of type clr console applicatioN and place the following code there but i am getting errors which i am not understading.

// thread_clr.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System;
using namespace System::Threading;
class MessagePrinter;

// class ThreadTester demonstrates basic threading concepts
class ThreadTester
{
    static int Main()
    {
        // Create and name each thread. Use MessagePrinter's
        // Print method as argument to ThreadStart delegate.
        MessagePrinter printer1 = gcnew MessagePrinter();
        Thread thread1 = gcnew Thread ( gcnew ThreadStart( printer1.Print ) );
        thread1.Name = "thread1";

        MessagePrinter printer2 = gcnew MessagePrinter();
        Thread thread2 = gcnew Thread ( gcnew ThreadStart( printer2.Print ) );
        thread2.Name = "thread2";

        MessagePrinter printer3 = gcnew MessagePrinter();
        Thread thread3 = gcnew Thread ( gcnew ThreadStart( printer3.Print ) );
        thread3.Name = "thread3";

        Console.WriteLine( "Starting threads" );
        // call each thread's Start method to place each
        // thread in Started state
        thread1.Start();
        thread2.Start();
        thread3.Start();

        Console.WriteLine( "Threads started\n" );
    } // end method Main
}; // end class ThreadTester

class MessagePrinter
{
    private int sleepTime;
    private static Random random = gcnew Random();

    // constructor to initialize a MessagePrinter object
    public MessagePrinter()
    {
        // pick random sleep time between 0 and 5 seconds
        sleepTime = random.Next( 5001 );
    }

    //controls Thread that prints message
    public void Print()
    {
        // obtain reference to currently executing thread
        Thread current = Thread.CurrentThread;

        // put thread to sleep for sleepTime amount of time
        Console.WriteLine(current.Name + " going to sleep for " + sleepTime );

        Thread.Sleep ( sleepTime );

        // print thread name
        Console.WriteLine( current.Name + " done sleeping" );

    } // end method Print
} // end class MessagePrinter

Please help. Or better still please guide me to some tutorials or something to that affect. I understand SO is not a tutorial site and i am not asking one but i would appreciate if some one could atleast point out the resources for C++/CLR thread. C++/CLR Winform Threads. Would really appreciate it

Regards

Some errors are:

'printer1' uses undefined class 'MessagePrinter' 'Print' : is not a member of 'System::Int32' 'System::Threading::ThreadStart' : a delegate constructor expects 2 argument(s) 'System::Threading::Thread::Thread' : no appropriate default constructor available 'System::Threading::Thread::Thread' : no appropriate default constructor available 'syntax error : 'int' should be preceded by ':' 'cannot declare a managed 'random' in an unmanaged 'MessagePrinter'may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap 'MessagePrinter::random' : you cannot embed an instance of a reference type, 'System::Random', in a native type 'MessagePrinter::random' : only static const integral data members can be initialized within a class 'MessagePrinter' should be preceded by ':' 'void' should be preceded by ':'

like image 521
user2572521 Avatar asked Jul 12 '13 06:07

user2572521


Video Answer


2 Answers

I fixed that code for you, but let me say one thing first: you really need to learn the basics first. This was not even C++/CLI code and you did not find or could not fix the most glaring syntax errors. Forget threads for a while, they are difficult. Learn basic stuff first.

#include "stdafx.h"

using namespace System;
using namespace System::Threading;

ref class MessagePrinter
{
private:
    int sleepTime;
    static Random^ random = gcnew Random();

// constructor to initialize a MessagePrinter object
public:
    MessagePrinter()
    {
        // pick random sleep time between 0 and 5 seconds
        sleepTime = random->Next( 5001 );
    }

    //controls Thread that prints message
    void Print()
    {
        // obtain reference to currently executing thread
        Thread^ current = Thread::CurrentThread;

        // put thread to sleep for sleepTime amount of time
        Console::WriteLine(current->Name + " going to sleep for " + sleepTime );

        Thread::Sleep ( sleepTime );

        // print thread name
        Console::WriteLine( current->Name + " done sleeping" );

    } // end method Print
}; // end class MessagePrinter  

int main()
{
    // Create and name each thread. Use MessagePrinter's
    // Print method as argument to ThreadStart delegate.
    MessagePrinter^ printer1 = gcnew MessagePrinter();
    Thread^ thread1 = gcnew Thread(gcnew ThreadStart( printer1, &MessagePrinter::Print ) );
    thread1->Name = "thread1";

    MessagePrinter^ printer2 = gcnew MessagePrinter();
    Thread^ thread2 = gcnew Thread ( gcnew ThreadStart( printer2, &MessagePrinter::Print ) );
    thread2->Name = "thread2";

    MessagePrinter^ printer3 = gcnew MessagePrinter();
    Thread^ thread3 = gcnew Thread ( gcnew ThreadStart( printer3, &MessagePrinter::Print ) );
    thread3->Name = "thread3";

    Console::WriteLine( "Starting threads" );
    // call each thread's Start method to place each
    // thread in Started state
    thread1->Start();
    thread2->Start();
    thread3->Start();

    Console::WriteLine( "Threads started\n" );
    Console::ReadLine();

    return 0;
}
like image 177
nvoigt Avatar answered Sep 19 '22 06:09

nvoigt


Be Aware that you leave the Main function and therefor all threads will be terminated... so place an Console.ReadLine() in the last line of your "main"...

Also: Your source code is C# and not C++/CLI...

like image 20
Jochen Kalmbach Avatar answered Sep 20 '22 06:09

Jochen Kalmbach