24.05.2014 Views

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example<br />

The control structure has the following layout and would be coded as such in C:<br />

typedef struct {<br />

pthread_key_t key;<br />

int flags;<br />

void *unused_1;<br />

int unused_2;<br />

} FORT_LOCAL_COMMON;<br />

extern FORT_LOCAL_COMMON myblock;<br />

The ″key″ field is a unique identifier that describes a threadlocal data area. Every<br />

threadlocal common block has its own key. The ″flags″ field indicates whether a<br />

key has been obtained <strong>for</strong> the common block. Within a C function, you should use<br />

the ″key″ in the control block in a call to pthread_getspecific to obtain the<br />

thread-specific address of the threadlocal common area.<br />

! Example 1: "<strong>for</strong>t_sub" is invoked by multiple threads. This is an invalid example<br />

! because "<strong>for</strong>t_sub" and "another_sub" both declare /block/ to be THREADLOCAL.<br />

! They intend to share the common block, but they are executed by different threads.<br />

SUBROUTINE <strong>for</strong>t_sub()<br />

COMMON /block/ j<br />

INTEGER :: j<br />

!<strong>IBM</strong>* THREADLOCAL /block/<br />

INTEGER a(10)<br />

! Each thread executing <strong>for</strong>t_sub<br />

! obtains its own copy of /block/.<br />

...<br />

!<strong>IBM</strong>* INDEPENDENT<br />

DO index = 1,10<br />

CALL another_sub(a(i))<br />

END DO<br />

...<br />

END SUBROUTINE <strong>for</strong>t_sub<br />

SUBROUTINE another_sub(aa) ! Multiple threads are used to execute another_sub.<br />

INTEGER aa<br />

COMMON /block/ j<br />

! Each thread obtains a new copy of the<br />

INTEGER :: j ! common block: /block/.<br />

!<strong>IBM</strong>* THREADLOCAL /block/<br />

...<br />

aa = j<br />

! The value of ’j’ is undefined.<br />

END SUBROUTINE another_sub<br />

For more in<strong>for</strong>mation, see the THREADLOCAL directive, in the <strong>XL</strong> <strong>Fortran</strong><br />

<strong>Enterprise</strong> <strong>Edition</strong> <strong>for</strong> <strong>AIX</strong> Language Reference.<br />

368 <strong>XL</strong> <strong>Fortran</strong> <strong>Enterprise</strong> <strong>Edition</strong> <strong>for</strong> <strong>AIX</strong> : User’s <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!