Tasks 多核查找最大最小值問題

2021-06-20 15:18:46 字數 935 閱讀 7618

先貼下**

_datas.parallelforeach(arg_ndatastartindex, arg_ndatacount, (data) =>

);

max,min的實現如下:

public static t min(this icomparablearg_othis, t arg_oother)

public static t max(this icomparablearg_othis, t arg_oother)

**很簡潔,但是其實問題很大。得到的最大值有時候(一般10次會有2次)是乙個比較大的值,但不是最大值。 最小值得到是比較小的值

主要問題是 dmax = dmax.max(data.highprice) 不是同步的。

但是又不想用lock 

想用 interlocked.compareexchange 這個函式達到取極值的效果

**如下:

_datas.parallelforeach(arg_ndatastartindex, arg_ndatacount, (data) =>

while (m0 != interlocked.compareexchange(ref dmin, m1, m0));

dowhile (m0 != interlocked.compareexchange(ref dmax, m1, m0));

//dmax = dmax.max(data.highprice);

//dmin = dmin.min(data.lowprice);

});

其中的while用於檢測是否發生過衝突,如果是則再次比較

經過檢測4核上1000個資料發生了10次衝突

最大最小值

示例一 maximum lambda x,y x y x x y y 注意 x y 返回的是0或者1 minimum lambda x,y x y y x y x a 10 b 20 print the largar one is d maximum a,b print the lower one ...

分組查詢最大 最小值sql

經典題目 查詢每個班級的最高分,查詢每種日誌的最晚記錄 1.查詢每個班級的最高分 不考慮同一分數的 思路 首先利用max group by取出每組最高的分數,再與表自連線 sql語句 select t1.id,t1.name,t1.calssid,t2.score from t zhb t1 sel...

分組查詢最大 最小值sql

經典題目 查詢每個班級的最高分,查詢每種日誌的最晚記錄 1.查詢每個班級的最高分 不考慮同一分數的 sql語句 select id,name,calssid,max score from select from t zhb order by score desc a group by calssid...