C++/CLI 类内多线程编程 多个参数

using namespace System;
using namespace System::Threading;
namespace NNNN
{
	public ref class TTTT
	{
		static Thread^ t1;
	    static Thread^ t2;
		void fun()
		{
			t1 = gcnew Thread(gcnew ParameterizedThreadStart(DoWork1));
			t2 = gcnew Thread(gcnew ParameterizedThreadStart(DoWork2));
			Tuple<int, char, String^>^ tuple1 = Tuple::Create(1, 'a', "111");
			t1->Start(tuple1);
			t2->Start(tuple1);
		}
	}
	
	//传入线程的函数必须是静态的 形参必须是object
	//多个参数使用tuple作为整体传入
	static void DoWork1(System::Object^ i)
	{
		Tuple<int, char, String^>^ primes = (Tuple<int, char, String^>^)i;
		String^ s = Convert::ToString(primes->Item3);
		// do somethings....
	}

	static void DoWork2(System::Object^ i)
	{
		//使用join方法可以保证线程2必须在线程1后面执行
		t1->Join();
		Tuple<int, char, String^>^ primes = (Tuple<int, char, String^>^)i;
		String^ s = Convert::ToString(primes->Item3);
		// do somethings....
	}
}
posted @ 2024-10-26 20:40  小松鼠树懒  阅读(2)  评论(0编辑  收藏  举报