Creating a list with Multi-Threading

In order to create a comma-separated list of symbols while exploring across every symbol in the watchlist, I am using the below piece of code.

Symbol = Name();
if(StaticVarCompareExchange("semaphore", 1, 0)==0)
{
StaticVarSetText("Symbol", StaticVarGetText("Symbol") + Symbol + ",");
StaticVarSet("semaphore", 0 );
}
else
{
_trace("Can not obtain semaphore");
}

To prevent any symbol being missed due to multi-threading, I am using the function StaticVarCompareExchange. I cannot use StaticVarAdd function here as it does not support strings. I would want to refrain from using #pragma maxthreads 1 as well as it would slow down the exploration when dealing with many symbols.

However, is there any way I can create a scenario where without using StaticVarCompareExchange function, I could generate an error where a symbol does not get added to the list due to multithreading? I understand this is a very rare scenario, but it would be of great help if we are able to recreate it to test the code thoroughly.

@karan, probably I'm missing something, but I do not understand what your goal is.

The easiest way to retrieve a "comma separated list of symbols" is via the function CategoryGetSymbols( category, index, mode = 0 ) passing to it the category categoryWatchlist and the corresponding index.

What is the purpose of doing it in the way you described if the resulting list is identical to the content of the watchlist that you include in your exploration/scan?
AFAIK, AmiBroker processes ALL the included symbols, whether running in multithreaded mode or a (forced) single-thread mode.

1 Like

@beppe I cannot use CategoryGetSymbols as I want only the list of symbols that meet a certain criteria. Apologies I missed mentioning it earlier.

I agree with you that Amibroker does process all the symbols. But during multithreading, it reads(processes) and writes(adding the processed symbol in the list) many symbols at the same time. However, if two write operations for different symbols occur at the exact same moment, meaning there can be a case where one thread will write that list in the middle of other thread updating it, an overwrite will occur and I will only get the later one in my list. To prevent this from happening and writing to occur in an atomic manner, StaticVarCompareExchange function is used.

So I am trying to create a scenario where a symbol does not get added to the list due to multiple writing happening at the exact same time to test the StaticVarCompareExchange function. The sole purpose of this is to know if the above piece of code is fulfilling the purpose of atomic writing and understanding how it works. I understand this is a very rare scenario, but it would be of great help if we are able to recreate it to test the code thoroughly.

Yes you could use StaticVarCompareExchange. You could also create a simple scan/exploration and CategoryAddSymbol to add symbols matching your criteria to watch list.

http://www.amibroker.com/guide/afl/categoryaddsymbol.html

This is thread safe just out of the box and does not require any extra protection.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.