{"id":1247,"date":"2016-01-27T06:47:56","date_gmt":"2016-01-27T11:47:56","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=1247"},"modified":"2016-01-28T07:06:35","modified_gmt":"2016-01-28T12:06:35","slug":"how-to-write-to-single-shared-file-in-multi-threaded-scenario","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2016\/01\/27\/how-to-write-to-single-shared-file-in-multi-threaded-scenario\/","title":{"rendered":"How to write to single shared file in multi-threaded scenario"},"content":{"rendered":"
The problem is as follows: during multiple-symbol Scan (or any other multi-threaded Analysis operation) we want to create a single, shared file and append content generated from multiple symbols to it.<\/p>
There are two things that we must consider if we are running in multiple treaded scenario.
1. If we want to get just single-run results, before appending content to the file, we need first to delete file generated in previous runs.<\/p>
2. We have to take care to open the file in share-aware mode so multiple threads do not write at the same time (preventing corruption).<\/p>
A sample formula is presented below.<\/p> One important thing to remember is that in multi-threaded environment threads execute independently and there is no guarantee they will all execute sequentially, so the order of items (symbols) in the file may not be alphabetical.<\/p> If we want strictly sequential execution, then we must limit ourselves to just running in single-thread. A single-thread execution in New Analysis window can be achieved by placing the following pragma call at the top of the formula.<\/p> #pragma maxthreads limits the number of parallel threads used by New Analysis window. This command is available in AmiBroker version 6 or higher.<\/p>","protected":false},"excerpt":{"rendered":" The problem is as follows: during multiple-symbol Scan (or any other multi-threaded Analysis operation) we want to create a single, shared file and append content generated from multiple symbols to it.There are two things that we must consider if we are running in multiple treaded scenario.1. If we want to get just single-run results, before […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[57],"tags":[14,31,66,79],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1247"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=1247"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1247\/revisions"}],"predecessor-version":[{"id":1250,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/1247\/revisions\/1250"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=1247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=1247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=1247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/span>\/\/ our scanning code
<\/span>Buy <\/span>= <\/span>Cross<\/span>( <\/span>MACD<\/span>(), <\/span>Signal<\/span>() );
<\/span>filepath <\/span>= <\/span>"C:\\\\ScanExport.txt"<\/span>;
if( <\/span>Status<\/span>(<\/span>"stocknum"<\/span>) == <\/span>0 <\/span>)
{
<\/span>\/\/ delete previous file before anything else
<\/span>fdelete<\/span>( <\/span>filepath <\/span>);
}
<\/span>\/\/ open file in \"share-aware" append mode
<\/span>fh <\/span>= <\/span>fopen<\/span>( <\/span>filepath<\/span>, <\/span>"a"<\/span>, <\/span>True <\/span>);
<\/span>\/\/ proceed if file handle is correct
<\/span>if ( <\/span>fh <\/span>)
{
<\/span>lastbuyDT <\/span>= <\/span>LastValue<\/span>( <\/span>ValueWhen<\/span>( <\/span>Buy<\/span>, <\/span>DateTime<\/span>() ) ) ;
<\/span>\/\/ write to file
<\/span>fputs<\/span>( <\/span>Name<\/span>() +<\/span>", Last Buy: " <\/span>+ <\/span>DateTimeToStr<\/span>( <\/span>lastBuyDT <\/span>) +<\/span>"\\n"<\/span>, <\/span>fh <\/span>);
<\/span>\/\/ close file handle
<\/span>fclose<\/span>( <\/span>fh <\/span>);
}
else
{
<\/span>_TRACE<\/span>(<\/span>"Failed to open the file"<\/span>);
<\/code><\/span>#pragma maxthreads <\/code>