I wonder whats the most efficient way for all data generation threads to pass data to the socket thread ? Currently using "lock" and "list", all threads sharing a public variable, when any of the threads need to pass data, they will have to lock the variable, add/remove data, then unlock the variable, so other threads can access the same variable, if i have soo many threads, i think the variable will almost always in the "lock" status, are there any better way to do it ?
CODE
void main(){
for (int i = 0; i < 100; i++) {
Thread tAdd = new Thread(threadAdd);
tAdd.Start();
}
Thread tRemove = new Thread();
tRemove.Start();
}
Object lckData = new Object(); // for lock
List <string> lstData = new List<string>();
void threadAdd() {
while (true) {
lock(lckData) { <--------------------------------- problem
lstData.Add("New Data");
}
sleep(16); // 16 mili second = 1 frame
}
}
void threadRemove() {
while (true) {
lock(lckData) { <--------------------------------- problem
while(lstData.Count > 0) {
string strData = lstData[0];
lstData.RemoveAt(0);
fctDeliverData(strData);
}
}
sleep(1);
}
}
void fctDeliverData(string strData){
// send data to clients, no issue
}
This post has been edited by narf03: Dec 4 2023, 03:41 PM
Dec 4 2023, 03:59 AM, updated 3y ago
Quote
0.0128sec
0.25
6 queries
GZIP Disabled