valid uses of shared
Steven Schveighoffer
schveiguy at yahoo.com
Mon Jun 11 03:26:56 PDT 2012
On Sat, 09 Jun 2012 08:31:01 -0400, Mike Wey <mike-wey at example.com> wrote:
> On 06/09/2012 04:01 AM, mta`chrono wrote:
>> Would this be legal?
>>
>> class A
>> {
>> private static shared int counter; // shared across all instances
>>
>> this()
>> {
>> auto i = ++counter;
>> pragma(msg, typeof(i)); // prints int
>> }
>> }
>>
>
> Would it also be legal if the variable wasn't static?
No.
> int opApply(int delegate(ref Pixels) dg)
> {
> shared(int) progress;
>
> foreach ( row; taskPool.parallel(iota(extent.y, extent.y +
> extent.height)) )
> {
> int result = dg(Pixels(image, extent.x, row, extent.width, 1));
>
> if ( result )
> return result;
>
> if ( image.monitor !is null )
> {
> atomicOp!"+="(progress, 1);
> image.monitor()("ImageView/" ~ image.filename, progress,
> extent.height);
> }
> }
> return 0;
> }
AFAIK, if you removed shared from progress, it would work. I don't think
std.parallel is as strict as std.concurrency (and for pretty good reason).
I think a better way to mark progress is to make it an atomic integer type
(like Artur has developed).
-Steve
More information about the Digitalmars-d
mailing list