150 lines
5.8 KiB
HTML
150 lines
5.8 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Example: Thread-specific data in Pthread programs" />
|
||
|
<meta name="abstract" content="This example shows a Pthread program creating thread-specific data." />
|
||
|
<meta name="description" content="This example shows a Pthread program creating thread-specific data." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzahwdatco.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="rzahwe21-e21rx" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Example: Thread-specific data in Pthread programs</title>
|
||
|
</head>
|
||
|
<body id="rzahwe21-e21rx"><a name="rzahwe21-e21rx"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Example: Thread-specific data in Pthread programs</h1>
|
||
|
<div><p>This example shows a Pthread program creating thread-specific data.</p>
|
||
|
<div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms
|
||
|
of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
||
|
</div>
|
||
|
<div class="example"> <pre>/*
|
||
|
Filename: ATEST22.QCSRC
|
||
|
The output of this example is as follows:
|
||
|
Enter Testcase - LIBRARY/ATEST22
|
||
|
Create/start threads
|
||
|
Wait for the threads to complete, and release their resources
|
||
|
Thread 00000000 00000036: Entered
|
||
|
Thread 00000000 00000036: foo(), threadSpecific data=0 2
|
||
|
Thread 00000000 00000036: bar(), threadSpecific data=0 2
|
||
|
Thread 00000000 00000036: Free data
|
||
|
Thread 00000000 00000037: Entered
|
||
|
Thread 00000000 00000037: foo(), threadSpecific data=1 4
|
||
|
Thread 00000000 00000037: bar(), threadSpecific data=1 4
|
||
|
Thread 00000000 00000037: Free data
|
||
|
Main completed
|
||
|
*/
|
||
|
#define _MULTI_THREADED
|
||
|
#include <pthread.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
void foo(void); /* Functions that use the threadSpecific data */
|
||
|
void bar(void);
|
||
|
void dataDestructor(void *data);
|
||
|
|
||
|
#define checkResults(string, val) { \
|
||
|
if (val) { \
|
||
|
printf("Failed with %d at %s", val, string); \
|
||
|
exit(1); \
|
||
|
} \
|
||
|
}
|
||
|
|
||
|
typedef struct {
|
||
|
int threadSpecific1;
|
||
|
int threadSpecific2;
|
||
|
} threadSpecific_data_t;
|
||
|
|
||
|
#define NUMTHREADS 2
|
||
|
pthread_key_t threadSpecificKey;
|
||
|
|
||
|
|
||
|
void *theThread(void *parm)
|
||
|
{
|
||
|
int rc;
|
||
|
threadSpecific_data_t *gData;
|
||
|
printf("Thread %.8x %.8x: Entered\n", pthread_getthreadid_np());
|
||
|
gData = (threadSpecific_data_t *)parm;
|
||
|
rc = pthread_setspecific(threadSpecificKey, gData);
|
||
|
checkResults("pthread_setspecific()\n", rc);
|
||
|
foo();
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
void foo() {
|
||
|
threadSpecific_data_t *gData = pthread_getspecific(threadSpecificKey);
|
||
|
printf("Thread %.8x %.8x: foo(), threadSpecific data=%d %d\n",
|
||
|
pthread_getthreadid_np(), gData->threadSpecific1, gData->threadSpecific2);
|
||
|
bar();
|
||
|
}
|
||
|
|
||
|
void bar() {
|
||
|
threadSpecific_data_t *gData = pthread_getspecific(threadSpecificKey);
|
||
|
printf("Thread %.8x %.8x: bar(), threadSpecific data=%d %d\n",
|
||
|
pthread_getthreadid_np(), gData->threadSpecific1, gData->threadSpecific2);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
void dataDestructor(void *data) {
|
||
|
printf("Thread %.8x %.8x: Free data\n", pthread_getthreadid_np());
|
||
|
pthread_setspecific(threadSpecificKey, NULL);
|
||
|
free(data);
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(int argc, char **argv)
|
||
|
{
|
||
|
pthread_t thread[NUMTHREADS];
|
||
|
int rc=0;
|
||
|
int i;
|
||
|
threadSpecific_data_t *gData;
|
||
|
|
||
|
printf("Enter Testcase - %s\n", argv[0]);
|
||
|
rc = pthread_key_create(&threadSpecificKey, dataDestructor);
|
||
|
checkResults("pthread_key_create()\n", rc);
|
||
|
|
||
|
printf("Create/start threads\n");
|
||
|
for (i=0; i <NUMTHREADS; ++i) {
|
||
|
/* Create per-thread threadSpecific data and pass it to the thread */
|
||
|
gData = (threadSpecific_data_t *)malloc(sizeof(threadSpecific_data_t));
|
||
|
gData->threadSpecific1 = i;
|
||
|
gData->threadSpecific2 = (i+1)*2;
|
||
|
rc = pthread_create(&thread[i], NULL, theThread, gData);
|
||
|
checkResults("pthread_create()\n", rc);
|
||
|
}
|
||
|
|
||
|
printf("Wait for the threads to complete, and release their resources\n");
|
||
|
for (i=0; i <NUMTHREADS; ++i) {
|
||
|
rc = pthread_join(thread[i], NULL);
|
||
|
checkResults("pthread_join()\n", rc);
|
||
|
}
|
||
|
|
||
|
pthread_key_delete(threadSpecificKey);
|
||
|
printf("Main completed\n");
|
||
|
return 0;
|
||
|
}
|
||
|
</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzahwdatco.htm" title="Typical applications that are not threaded use global storage.">Thread-specific data</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|