I'm starting a thread

I'm starting a thread

Starting a thread

When your application creates a new thread, the system initializes a thread object, control structures, and run-time support. These allow the new thread to use language constructs and system services safely. In addition, you may need to initialize the application data and parameters the new thread uses before starting the new thread.

As you create each thread, the system assigns it a unique thread identifier. The thread identifier is an integer value that you can use when you debug, trace, or perform other administrative activities on the thread. You usually do not use this thread identifier to directly manipulate the thread.

Most threads application program interface (API) sets also return a thread object or handle that represents the newly created thread. You can use this thread object to manipulate the new thread or as a synchronization object to wait for the thread to finish its processing.

Use the following as examples for your program:

Example: Starting a thread in a Pthread program

/*
Filename: ATEST11.QCSRC
The output of this example is as follows:
Enter Testcase - LIBRARY/ATEST11
Create/start a thread with parameters
Wait for the thread to complete
Thread ID 0000000c, Parameters: 42 is the answer to "Life, the Universe and Everything"
Main completed
*/
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define checkResults(string, val) { \
if (val) { \
printf("Failed with %d at %s", val, string); \
exit(1); \
} \
}
typedef struct {
int threadParm1;
char threadParm2[124];
} threadParm_t;

void *theThread(void *parm)
{
pthread_id_np_t tid;
threadParm_t *p = (threadParm_t *)parm;
tid = pthread_getthreadid_np();
printf("Thread ID %.8x, Parameters: %d is the answer to \"%s\"\n",
tid.intId.lo, p->threadParm1, p->threadParm2);
return NULL;
}

int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
threadParm_t *threadParm;

printf("Enter Testcase - %s\n", argv[0]);

threadParm = (threadParm_t *)malloc(sizeof(threadParm));
threadParm->threadParm1 = 42;
strcpy(threadParm->threadParm2, "Life, the Universe and Everything");

printf("Create/start a thread with parameters\n");
rc = pthread_create(&thread, NULL, theThread, threadParm);
checkResults("pthread_create()\n", rc);

printf("Wait for the thread to complete\n");
rc = pthread_join(thread, NULL);
checkResults("pthread_join()\n", rc);

printf("Main completed\n");
return 0;

Example: Starting a thread using Java

/*
FileName: ATEST11.java

The output of this example is as follows:
Entered the testcase
Create a thread with parameters
Start the thread
Wait for the thread to complete
Thread Parameters: 42 is the answer to "Life, the Universe and Everything"
Testcase completed
*/
import java.lang.*;


public class ATEST11 {

static class theThread extends Thread {
int threadParm1;
String threadParm2;

public theThread(int i, String s) {
threadParm1 = i;
threadParm2 = s;
}
public void run() {
System.out.print("Thread Parameters: " + String.valueOf(threadParm1) +
" is the answer to \"" + threadParm2 + "\"\n");
}
}

public static void main(String argv[]) {
System.out.print("Entered the testcase\n");

System.out.print("Create a thread with parameters\n");
theThread t = new theThread(42, "Life, the Universe and Everything");

System.out.print("Start the thread\n");
t.start();

System.out.print("Wait for the thread to complete\n");
try {
t.join();
}
catch (InterruptedException e) {
System.out.print("Join interrupted\n");
}

System.out.print("Testcase completed\nj");
System.exit(0);
}

}

You're welcome...
 
Back
Top