Jump to content

Multithread and automatic variable


Parth

Recommended Posts

Hi , 

For below code , for case-1 and case-2 output is different ,

 

module my_module; 
    int a=0; 
    initial 
    begin 
      for(int i=0;i<5;i++) 
      begin     
         fork 
                // Check differance here , between 2 statements 
                // ##case 1 (declaration and assignment). automatic int j= i;
                // and 
                automatic int j;  // ##case 2 declare 
                j=i;                  //  then assign
                begin
                    $display("Chk auto %d",j);
                end    
         join_none  
      end
    end 
endmodule : my_module

 

For case-1 output is  0 1 2 3 4 

 

For case-2 output is 5 5 5 5 5 

 

Can anyone explain me how??

Thanks , 
Parth

Link to comment
Share on other sites

bhunter, that is incorrect; only case 1 will work. The initialization of automatic variables happens as the scope they declared in come into existance(activated).

 

Each iteration of the for loop creates a new fork/join_none block with a unique value of i.  That unique value of i is used to initialize the local copy of j created for each fork/join_none block. If you put the variable assigment into procedural code of the child threads, the ordering of the execution of those child threads are indetermiate, and at best, deferred until the parent thread blocks or terminates.

Link to comment
Share on other sites

hi,

 

what you essentially wrote in case#2 is 

 

module my_module; 
    int a=0; 
    initial 
    begin 
      for(int i=0;i<5;i++) 
      begin     
         fork 
                // Check differance here , between 2 statements 
                // ##case 1 (declaration and assignment). automatic int j= i;
                // and 
                automatic int j;  // ##case 2 declare 
begin
                 j=i;                  //  then assign
end
                begin
                    $display("Chk auto %d",j);
                end    
         join_none  
      end
    end 
endmodule : my_module

note that the $display and the "j=i" are actually parallel threads (a race). if you switch the order of the two  blocks your result might be different (if your simulator chooses this)

 

                begin
                    $display("Chk auto %d",j);
              end    

begin
                 j=i;                  //  then assign
end
 
so for what you want to accomplish case#1 is the right path.
 
/uwe
Link to comment
Share on other sites

Thanks bhunter , dave and uwe . 

 

What I conclude is , automatic int j= i; does not create process in fork_join but always executed before $display , so case-1 works and not case-2. In case-2 also if we remove automatic type then result would be same as case-1.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...