1. 程式人生 > >c# multi thread programming ,c# 多執行緒程式設計

c# multi thread programming ,c# 多執行緒程式設計

使用函式代表(委託)的BeginInvoke()和EndInvoke()方法。可以在新開的執行緒上執行函式,並且在主執行緒得到函式的返回值。

using System;
using System.Threading;

namespace MultiThread
{
public delegate int Handler (int a);//use of Handler.BeginInvoke() and EndInvoke() methods,
								//first,we need a handler.
class F     	 {
	public static int Accum(int a)//this function should run in child thread.
		{
		Console.WriteLine("this is in child thread");
		Console.WriteLine("begin accumulation in child thread");
		int i,n;
		n = 10;
		//simulate the time consuming,seemingly,the accumulation takes some time.
		for(i=1;i<n;i++)
			{
			a ++;
			Console.WriteLine("now the number reaches {0}",a);
			Thread.Sleep(1000);
			}
		
		return a;
		}
	public static int Add(int a,int b)//the function should run in main thread.
		{
		return a+b;
		}
				 }
class Program{
	public static void Main(string[] args)
		{
		Console.WriteLine("this is in main thread");//main thread begins.
		Handler myHandler = new Handler(F.Accum);
		IAsyncResult result = myHandler.BeginInvoke(2,null,null);//child thread runs F.Accum() beginning.
																//child thread is another thread.
															   //main thread continues following execution.
		Console.WriteLine("main thread executes some implementations that consume several seconds");
		
		int i,n,d;
		n = 10;
		d = 0;//the d
		//some implementations consumes some time.
		for(i=1;i<n;i++)
			{Thread.Sleep(1000);
			 d += i;}//the d
		Console.WriteLine("the implementationin in main thread completed,consuming {0} seconds",i);
		//get the result of child thread and do it an addition.
		int r1 = myHandler.EndInvoke(result);//get the result of thread if it ends.
		Console.WriteLine("the final number returned to main thread is  {0}",r1);
		Console.WriteLine("now the final number will be added a digit  {0} in main thread",d);//the d
		int r2;
		r2 = F.Add(r1,d);
		Console.WriteLine("the result after addition is {0}",r2);
		
		Console.Write("Press any key to continue . . . ");
		Console.ReadKey(true);
		}
			}
}
在這段程式碼中,可以看出,當主執行緒在執行一些耗時的程式碼的時候。子執行緒的工作仍在同時進行。這樣的併發同時執行兩段程式碼,與單執行緒執行完一段程式碼再執行一段程式碼相比,就節約了很多時間。在主執行緒得到了子執行緒執行函式的結果以後,再進行利用,給它加上一個數。演示了主執行緒如何得到子執行緒的執行結果並利用。

下面我用單執行緒的模式執行上面的程式碼,顯然耗時得多。

using System;
using System.Threading;

namespace MultiThread2
{
public delegate int Handler (int a);//use of Handler.Invoke()  methods,
								//first,we need a handler.
class F     	 {
	public static int Accum(int a)
		{
		Console.WriteLine("this is in single main  thread");
		Console.WriteLine("begin accumulation");
		int i,n;
		n = 10;
		//simulate the time consuming,seemingly,the accumulation takes some time.
		for(i=1;i<n;i++)
			{
			a ++;
			Console.WriteLine("now the number reaches {0}",a);
			Thread.Sleep(1000);
			}
		
		return a;
		}
	public static int Add(int a,int b)
		{
		return a+b;
		}
				 }
class Program{
	public static void Main(string[] args)
		{
		Console.WriteLine("this is in main thread");
		Handler myHandler = new Handler(F.Accum);
		int result = myHandler.Invoke(2);
		Console.WriteLine("the final number returned to main thread is  {0}",result);
		Console.WriteLine("main thread executes some implementations that consume several seconds");
		
		int i,n,d;
		n = 10;
		d = 0;//the d
		//some implementations consumes some time.
		for(i=1;i<n;i++)
			{Thread.Sleep(1000);
			 d += i;}//the d
		Console.WriteLine("the implementationin in main thread completed,consuming {0} seconds",i);

		Console.WriteLine("now the final number will be added a digit  {0} in main thread",d);//the d
		int r2;
		r2 = F.Add(result,d);
		Console.WriteLine("the result after addition is {0}",r2);
		
		Console.Write("Press any key to continue . . . ");
		Console.ReadKey(true);
		}
			}
}

下面是雙執行緒和單執行緒執行同樣的程式耗時對比


相關推薦

C++使用thread類進行執行程式設計

C++11中引入了一個用於多執行緒操作的thread類,簡單多執行緒示例: #include <iostream> #include <thread> #include <Windows.h> using namespace std; void thread01(

c# multi thread programming ,c# 執行程式設計

使用函式代表(委託)的BeginInvoke()和EndInvoke()方法。可以在新開的執行緒上執行函式,並且在主執行緒得到函式的返回值。 using System; using System.Threading; namespace MultiThread { pub

java執行同步以及執行間通訊詳解&amp;消費者生產者模式&amp;死鎖&amp;Thread.join()(執行程式設計之二)

從執行結果,我們就可以看出我們4個售票視窗同時賣出了1號票,這顯然是不合邏輯的,其實這個問題就是我們前面所說的執行緒同步問題。不同的執行緒都對同一個資料進了操作這就容易導致資料錯亂的問題,也就是執行緒不同步。那麼這個問題該怎麼解決呢?在給出解決思路之前我們先來分析一下這個問題是怎麼產生的?我們宣告一個執行緒類

c++11特性裡的執行thread的用法

建立和啟動一條C++執行緒就像在C++原始碼中新增執行緒標頭檔案那麼簡便。我們來看看如何建立一個簡單的帶執行緒的HelloWorld: #include <iostream> #include <thread> using namespace std

C++使用thread執行程式設計

C++11中引入了一個用於多執行緒操作的thread類,簡單多執行緒示例: #include <iostream> #include <thread> #include &l

multi-reactor伺服器模型的C++封裝類(libevent+執行實現)

最近在看memcached的原始碼,覺得它那種libevent+多執行緒的伺服器模型(multi-reactor)真的很不錯,我將這個模型封裝成一個C++類,根據我的簡單測試,這個模型的效率真的很不錯,歡迎大家試用。 這個類的使用方法很簡單(缺點是不太靈活),只

執行程式設計 c++ /thread(detach,join)/ _beginthreadex

1.標頭檔案引用 #include <thread> 2.std::thread test(&function, this);test.detach(); join:主執行緒被阻塞 detach:,不會阻塞,會分離,子執行緒自動回收資源 _be

【Linux C/C++】 第08講 執行TCP傳輸檔案/select模型

一、多執行緒    pthread.h    libpthread.so   -lpthread    1.建立多執行緒      1.1 程式碼   &nbs

c++單例模式,執行使用

c++ 11保證了這樣做是執行緒安全的。 一:class Singleton{ static Singleton* GetInstance(){         static Singleton s;     

Java併發(十八):阻塞佇列BlockingQueue BlockingQueue(阻塞佇列)詳解 二叉堆(一)之 圖文解析 和 C語言的實現 多執行緒程式設計:阻塞、併發佇列的使用總結 Java併發程式設計:阻塞佇列 java阻塞佇列 BlockingQueue(阻塞佇列)詳解

阻塞佇列(BlockingQueue)是一個支援兩個附加操作的佇列。 這兩個附加的操作是:在佇列為空時,獲取元素的執行緒會等待佇列變為非空。當佇列滿時,儲存元素的執行緒會等待佇列可用。 阻塞佇列常用於生產者和消費者的場景,生產者是往佇列裡新增元素的執行緒,消費者是從佇列裡拿元素的執行緒。阻塞佇列就是生產者

C/S模式下---執行程式設計

伺服器採用單程序/執行緒程式設計,在同一時刻,伺服器只能與一個客戶端進行互動。只有與當前客戶端的通訊結束後,才能為下一個客戶端進行服務。所以,如果採用執行緒,讓主執行緒連線客戶端,而函式執行緒為每個客戶端進行服務,這樣就可以保證伺服器可以同時為多個客戶端提供服務,實現併發。 採用多執

C++ 在類裡面使用執行技術

前言 有很多時候,我們希望可以在C++類裡面對那些比較耗時的函式使用多執行緒技術,但是熟悉C++物件語法的人應該知道,C++類的成員函式的函式指標不能直接

理解c++執行程式設計

多執行緒程式設計 本篇博文不是主要介紹互斥鎖之類的,是理解執行緒的執行,以便以後有把握的寫多執行緒程式。 #include<thread> #include<iostream&g

Linux c執行程式設計的4個例項

在主流的作業系統中,多工一般都提供了程序和執行緒兩種實現方式,程序享有獨立的程序空間,而執行緒相對於程序來說是一種更加輕量級的多工並行,多執行緒之間一般都是共享所在程序的記憶體空間的。   Linux也不例外,雖然從核心的角度來看,執行緒體現為一種對程序的"克隆"(clon

C++11執行程式設計 緒論及總結

C++11多執行緒程式設計 這一系列文章是從 https://thispointer.com/c11-multithreading-tutorial-series/ 轉過來的, 本來想翻譯一下, 但看了些內容, 用詞都不難, 讀英文沒有太大難度, 翻譯過來反而怕用詞不準畫蛇添

C++11執行程式設計 第十章: 使用packaged_task優雅的讓同步函式非同步執行

C++11 Multithreading – Part 10: packaged_task<> Example and Tutorial Varun July 2, 2017 C++11 Multithreading – Part 10: packaged_tas

C++11執行程式設計 第九章: std::async 更更優雅的寫執行

C++11 Multithreading – Part 9: std::async Tutorial & Example Varun May 5, 2017 C++11 Multithreading – Part 9: std::async Tutorial &

C++11執行程式設計 第八章: 使用 std::future std::promise 更優雅的獲取執行返回值

C++11 Multithreading – Part 8: std::future , std::promise and Returning values from Thread Varun June 20, 2015 C++11 Multithreading – Part

C++11執行程式設計 第七章: 條件變數及其使用方法

C++11 Multithreading – Part 7: Condition Variables Explained Varun June 2, 2015 C++11 Multithreading – Part 7: Condition Variables Explain

C++11執行程式設計 第五章: 使用鎖來解決竟態條件

C++11 Multithreading – Part 5: Using mutex to fix Race Conditions Varun February 22, 2015 C++11 Multithreading – Part 5: Using mutex to fi