Before the POSIX standard, each computer vendor would implement its own thread library and the resulting programs were not protable across different computer systems.
#include <pthread.h> |
CC -mt [other flags] C++-program (Solaris) g++ -pthread [other flags] C++-program (Linux) |
|
int pthread_create(pthread_t *threadID, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); |
Input Parameters:
|
Output parameters
|
Return value:
|
|
The following definition will define one such variable; and the name of our variable is "attr" (you can call the variable any name that you like):
pthread_attr_t attr; |
pthread_attr_init(&attr); |
Attribute | Default | Meaning of default |
---|---|---|
contentionscope | PTHREAD_SCOPE_PROCESS | Thread will compete for resources within the process |
detachstate | PTHREAD_CREATE_JOINABLE | Thread is joinable by other threads |
stackaddr | NULL | Workspace (stack) used by thread is allocated (reserved) by the operating system |
stacksize | NULL | Workspace size used by thread is determined by the operating system |
priority | 0 | Priority of the thread (the lower the priority value, the higher the priority...) |
policy | SCHED_OTHER | The scheduling policy is determined by the system |
inheritsched | PTHREAD_EXPLICIT_SCHED | scheduling policy and parameters are not inherited but explicitly defined by the attribute object |
guardsize | PAGESIZE | size of guard area for a thread's created stack (this area will help determine if the thread has exceeded the total amount of space that was allocated for the thread) |
(In fact, my examples always use the default values)
|
The stack is used to store:
|
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize); |
Example:
pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 10000000); // 10000000 bytes stack |
pthread_t TID; /* Thread ID - used for signaling */
pthread_attr_t attr; /* Thread attribute values for creation */
int param;
/* ---------------------------------------------------
Clear out thread attributes (use default values)
--------------------------------------------------- */
if (pthread_attr_init(&attr) != 0)
{ perror("Problem: pthread_attr_init");
exit(1);
}
/* ---------------------------------------------------------
Optionally, set thread attributes if needed (often not)
--------------------------------------------------------- */
if ( pthread_attr_setstacksize(&attr, 10000000) != 0)
{ perror("Problem: pthread_attr_setstacksize");
exit(1);
}
param = some value....;
/* --------------------------------------------------------------
Create thread (once created, thread starts running my_proc()
Note: the C operator & means: address of ...
-------------------------------------------------------------- */
if (pthread_create(& TID, & attr, my_proc, & param) != 0)
{ perror("Problem: pthread_create");
exit(1);
}
|
We must also provide a function for the thread to execute:
void *my_proc((void *) x) <---- new thread will begin
{ execution with this function
....
}
|
#include <pthread.h>
/* ==================================
Thread prints "Hello World"
================================== */
void *worker(void *arg)
{
cout << "Hello World !" << endl;
return(NULL); /* Thread exits (dies) */
}
/* =================================================
MAIN: create a trhead and wait for it to finish
================================================= */
int main(int argc, char *argv[])
{
pthread_t tid;
/* ---------------------
Create threads
(Use default thread attribute values)
--------------------- */
if ( pthread_create(&tid, NULL, worker, NULL) )
{
cout << "Cannot create thread" << endl;
exit(1);
}
cout << "Main waits for thread to finish...." << endl ;
pthread_join(tid, NULL);
exit(0);
}
|
How to run the program:
|