---
name: "PAPI_attach"
section: "3"
platform: "linux"
package: "libpapi-dev"
version: "7.1.0-5+b1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/PAPI_attach"
---

# PAPI\_attach(3)

Attach PAPI event set to the specified thread id.

## NAME

PAPI\_attach - Attach PAPI event set to the specified thread id.

## SYNOPSIS

## Detailed Description

**C Interface:**

\#include \<**papi.h**\> \
 int **PAPI\_attach( int EventSet, unsigned long tid )**;

**PAPI\_attach** is a wrapper function that calls **PAPI\_set\_opt** to allow PAPI to monitor performance counts on a thread other than the one currently executing. This is sometimes referred to as third party monitoring. **PAPI\_attach** connects the specified EventSet to the specified thread; **PAPI\_detach** breaks that connection and restores the EventSet to the original executing thread.

**Parameters**

*EventSet* An integer handle for a PAPI EventSet as created by **PAPI\_create\_eventset**. \
 *tid* A thread id as obtained from, for example, **PAPI\_list\_threads** or **PAPI\_thread\_id**.

**Return values**

*PAPI\_ECMP* This feature is unsupported on this component. \
 *PAPI\_EINVAL* One or more of the arguments is invalid. \
 *PAPI\_ENOEVST* The event set specified does not exist. \
 *PAPI\_EISRUN* The event set is currently counting events.

**Examples:**

```
int EventSet = PAPI_NULL;
unsigned long pid;
pid = fork( );
if ( pid <= 0 )
exit( 1 );
if ( PAPI_create_eventset( &EventSet ) != PAPI_OK )
exit( 1 );
// Add Total Instructions Executed to our EventSet
if ( PAPI_add_event( EventSet, PAPI_TOT_INS ) != PAPI_OK )
exit( 1 );
// Attach this EventSet to the forked process
if ( PAPI_attach( EventSet, pid ) != PAPI_OK )
exit( 1 );
```

**See also**

**PAPI\_set\_opt**

**PAPI\_list\_threads**

**PAPI\_thread\_id**

**PAPI\_thread\_init**

## Author

Generated automatically by Doxygen for PAPI from the source code.
