c# - Executing a function that accesses global array on multiple threads -
i have function 'graph1' calls function shown below. when call
graph1threader(copy, date);
i answer want, takes upwards of 30 seconds, tried using multithreading. when use
thread[copy] = new thread(() => graph1threader(copy, date));//pass date,copy thread[copy].start();
i no result i.e. global variables hold 0. why this? , how may rectify this?
void graph1() { chart1.chartareas[0].axisx.majorgrid.enabled = false; chart1.chartareas[0].axisy.majorgrid.enabled = false; (int k = 0; k < 5; k++) { int copy = k; datetime date = g.adddays(copy); refvalues[copy] = 0; threshvalues[copy] = 0; y_values[copy] = 0; y__values[copy] = 0; yvalues[copy] = 0; //task[copy] = new task(() => graph1threader(copy, date));//pass date,copy //ask[copy].start(); graph1threader(copy, date); } (int j = 0; j < 5; j++) { datetime temp = g.adddays(j); string temper = temp.toshortdatestring(); y__values[j] = y__values[j] - y_values[j]; xnames[j] = temper; } chart1.series[1].points.databindxy(xnames, y_values); chart1.series[2].points.databindxy(xnames, y__values); chart1.series[3].points.databindxy(xnames, refvalues); chart1.series[4].points.databindxy(xnames, threshvalues); chart1.series[5].points.databindxy(xnames, yvalues); } void graph1threader(int x, datetime date) { dbconnect = new dbconnect(); list<string>[] list = new list<string>[4]; list = a.mandetselect(); int numberofmen = a.countmandet(); string[] man_name = list[0].toarray(); string[] trade = list[1].toarray(); string[] license = list[2].toarray(); string[] training = list[3].toarray(); string[] display_status = list[4].toarray(); //string abc; list<string>[] lista = new list<string>[5]; list<string>[] listc = new list<string>[14]; (int j = 0; j < numberofmen; j++) { int flag = 0; if (!display_status[j].equals("no") && (selection == "all" || (selection == "lae" && license[j] != "") || (selection == "non lae" && license[j] == "") || (selection == "all avionics" && trade[j] == "avionics") || (selection == "non lae avionics" && trade[j] == "avionics" && license[j] == "") || (selection == "lae avionics" && trade[j] == "avionics" && license[j] != "") || (selection == "all airframes" && trade[j] == "airframes") || (selection == "non lae airframes" && trade[j] == "airframes" && license[j] == "") || (selection == "lae airframes" && trade[j] == "airframes" && license[j] != ""))) { refvalues[x]++; threshvalues[x] = 0.8 * refvalues[x]; string abc = man_name[j].replace(" ", "_"); int no_of_proj = a.countproj(abc);//required lista = a.manprojselect(abc);//required string[] projname = lista[0].toarray(); string[] country = lista[2].toarray(); string[] startofproj = lista[3].toarray(); string[] endofproj = lista[4].toarray(); string status = ""; listc = a.select(); int numberc = a.count();//number of projects, not required string[] nameofproj = listc[0].toarray(); string[] status = listc[13].toarray(); (int l = 0; l < a.countproj(abc); l++) { (int m = 0; m < numberc; m++) { if (nameofproj[m] == projname[l]) { status = status[m]; } } datetime shuru = datetime.parseexact(startofproj[l], "dd-mm-yyyy hh:mm:ss", cultureinfo.invariantculture); datetime anth = datetime.parseexact(endofproj[l], "dd-mm-yyyy hh:mm:ss", cultureinfo.invariantculture); if (date >= shuru && date <= anth) { if (status != "planned" && status != "lo" && flag == 0) { y_values[x]++;//basic utilisation flag = 1; } if (status == "ip" || status == "otd") y__values[x]++;//excess if (status == "planned") { yvalues[x]++;//unutilised } } } } } }
i came across multi-threading recently. excuse me if code doesn't good. threshvalue[]
,refvalues[]
,y_values[]
, y__values[]
, yvalues[]
global variables
multithreading not automagically make program run faster, , thread.join
waits thread complete.
basically if main thread cannot continue until thread has completed, should not use multithreading.
examples of multithreading fit:
- when trying download information several locations, can let multiple threads wait data 1 location each
- when want perform time-consuming task while keeping user interface responsive
the second example may case here, thread.join
blocking operation: still stops user interface being updated while thread working. in case, you'd have let thread inform main thread of completion.
i don't know on platform you're working here, instance on windows forms, form
class has invoke
method lets call method on form's thread:
class theform { // method running in separate thread void mythread() { // time consuming work byte[] mydata = processdata(); // send data main thread invoke(new action<byte[]>(threadcompleted), mydata); } // executed in main thread void threadcompleted(byte[] data) { // process data } }
about global variables , multithreading: thread can access them, there's no telling which thread accesses them when. if can, should avoid letting multiple threads access them, or use lock
mechanism protect them.
Comments
Post a Comment