09.07.2012, 19:08
If you want to use threads for queries in a loop (especially with multiple indexes), then it differs a lot from the thread example on the Wiki. I faced this problem today (my queries also had to execute in a pattern) and luckily I solved it. I could not find any additional information about this in this forum so that is why I am sharing it here.
The solution I came up with is recursion. The idea was to keep calling the same function after a query had finished until it matches certain criteria. I will show some snippets below to illustrate what I have done.
This can be improved using mysql_function_query (R7 only).
A loop using the same variable names would look like this:
The solution I came up with is recursion. The idea was to keep calling the same function after a query had finished until it matches certain criteria. I will show some snippets below to illustrate what I have done.
This can be improved using mysql_function_query (R7 only).
Код:
#define MAX_STREETS 5 #define MAX_HOUSES 5 new streetHouses[MAX_STREETS][MAX_HOUSES]; #define S_THREAD_STREETS 1 new houseid = 0; stock CreateStreet(streetid) { if(houseid == 0) { Thread_CreateStreet(streetid); } else { print("[CreateStreet]: Thread is busy."); } } stock Thread_CreateStreet(streetid) { if(houseid == MAX_HOUSES) { houseid = 0; return 1; } else { new query[99]; format(query, sizeof query, "INSERT INTO streets (street_id, house_id, value)\ VALUES ('%i', '%i', %i)", streetid, houseid, streetHouses[streetid][houseid]); houseid++; mysql_query(query, S_THREAD_STREETS, streetid); } return 0; } public OnQueryFinish(query[], resultid, extraid, connectionHandle) { switch(resultid) { case S_THREAD_STREETS: { if(Thread_CreateStreet(extraid)) { print("[Thread_CreateStreet]: "#MAX_HOUSES" houses have been saved"); } } default: { printf("Unknown query: (Resultid %d, Connection ID %d, Query: %s)",resultid, connectionHandle, query); } } return 1; }
Код:
for(new houseid = 0; houseid < MAX_HOUSES; houseid++) { // insert houses }