Programming threads in C ++ / CLR

I am really struggling with thread programming in Visual C ++ / CLR. I searched a lot and found a lot of materials on the Internet, including official resources, however I am still confused. There are very few resources for C ++ / CLR. Most of them are for C # or the old style C ++. I am trying to run this simple code. I select a new project like clr console applicationatioN and post the following code here, but I get errors that I don’t understand.

// 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 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 yet, please direct me to some tutorials or something in this influence. I understand that SO is not a training site, and I am not asking for it, but I would appreciate it if someone could specify resources for the C ++ / CLR stream. C ++ / CLR Winform Threads. Would love it.

Hello

Some errors:

'printer1' uses the undefined class 'MessagePrinter' 'Print': is not a member of System :: Int32 '' :: Threading :: ThreadStart ': a delegate constructor expects 2 arguments (s)' System :: Threading :: Thread :: Thread ': no ​​matching default constructor available' System :: Threading :: Thread :: Thread ': no ​​matching default constructor available' syntax error: 'int' must precede ':' 'cannot declare a managed' random 'in unmanaged 'MessagePrinter'may does not declare a global or static variable, or a member of a native type that references objects in the gc heap' MessagePrint er :: random ': you cannot insert an instance of a type link,' System :: Random ', in the native type' MessagePrinter :: random ': only static constant integral data elements can be initialized within the class' MessagePrinter 'must precede': '' void 'must be preceded by': '

+4
source share
2 answers

I fixed this code for you, but let me first say one thing: you really need to learn the basics first. It was not even C ++ / CLI code, and you did not find or were unable to fix the most egregious syntax errors. Forget about streams for a while, they are complicated. Study the basic materials 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 Start method to place each // thread in Started state thread1->Start(); thread2->Start(); thread3->Start(); Console::WriteLine( "Threads started\n" ); Console::ReadLine(); return 0; } 
+5
source

Remember that you will leave the main function, and therefore all threads will be completed ... therefore put Console.ReadLine() on the last line of your "main" ...

Also: your source code is C #, not C ++ / CLI ...

+2
source

Source: https://habr.com/ru/post/1491060/


All Articles