Difference between revisions of "User:Kr/A Thread's Life"
From Apache OpenOffice Wiki
< User:Kr
m (→Notes: Typo.) |
m |
||
Line 27: | Line 27: | ||
* Threads may dynamically switch from one type to the other and vice versa. | * Threads may dynamically switch from one type to the other and vice versa. | ||
* The "main" thread is a holder thread. | * The "main" thread is a holder thread. | ||
+ | |||
+ | <code>[cpp] | ||
+ | #include <stdlib.h> | ||
+ | #include <stdio.h> | ||
+ | #include <pthread.h> | ||
+ | |||
+ | |||
+ | static void * holder(void * gr) { | ||
+ | fprintf(stderr, "%s - tid: %x\n", __PRETTY_FUNCTION__, pthread_self()); | ||
+ | |||
+ | sleep(5); | ||
+ | |||
+ | |||
+ | if (1) // This may only be true if this is the last "holder" thread! | ||
+ | exit(0); | ||
+ | |||
+ | else | ||
+ | pthread_exit(NULL); | ||
+ | } | ||
+ | |||
+ | static void createHolderThread() { | ||
+ | pthread_attr_t attr; | ||
+ | |||
+ | pthread_attr_init(&attr); | ||
+ | pthread_attr_setdetachstate(&attr, 1); | ||
+ | |||
+ | pthread_t bt; | ||
+ | pthread_create(&bt, &attr, holder, NULL); | ||
+ | pthread_attr_destroy(&attr); | ||
+ | } | ||
+ | |||
+ | |||
+ | static void * holded(void * gr) { | ||
+ | fprintf(stderr, "%s - tid: %x\n", __PRETTY_FUNCTION__, pthread_self()); | ||
+ | |||
+ | sleep(10); | ||
+ | |||
+ | pthread_exit(NULL); | ||
+ | } | ||
+ | |||
+ | static pthread_t hd; | ||
+ | |||
+ | static void join_holded(void) { | ||
+ | fprintf(stderr, "%s - tid: %x\n", __PRETTY_FUNCTION__, pthread_self()); | ||
+ | |||
+ | // You may want to cancel a thread actively during termination: | ||
+ | pthread_cancel(hd); | ||
+ | // respectively | ||
+ | //pthread_kill(); | ||
+ | |||
+ | pthread_join(hd, NULL); | ||
+ | } | ||
+ | |||
+ | static void createHoldedThread(void) { | ||
+ | pthread_create(&hd, NULL, holded, NULL); | ||
+ | atexit(join_holded); | ||
+ | } | ||
+ | |||
+ | |||
+ | static void print_something(void) { | ||
+ | fprintf(stderr, "%s - printing ... tid: %x\n", __PRETTY_FUNCTION__, pthread_self()); | ||
+ | } | ||
+ | |||
+ | int main(void) { | ||
+ | atexit(print_something); | ||
+ | |||
+ | createHolderThread(); | ||
+ | |||
+ | createHoldedThread(); | ||
+ | |||
+ | pthread_exit(NULL); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </code> | ||
[[Category:Multi-Threading]] | [[Category:Multi-Threading]] |
Revision as of 09:53, 3 September 2007
Status: draft
Contents
Two types of Threads
- holder threads
- holded threads
Holder Thread
- Holder threads keep the process alife.
- The last holder thread terminates the process.
- Every holder thread must be detached, as no other thread is going to join it.
Examples
- GUI dispatcher thread
Holded Thread
- Holded threads don't keep the process alife.
- Any holded thread needs to be joined (terminated / cancelled) when (indirectly) released by a holder thread.
- Every holded thread must be joinable, as a holder thread is going to join it, latest during termination.
Examples
- cache flushing threads
Notes
- Threads may dynamically switch from one type to the other and vice versa.
- The "main" thread is a holder thread.
[cpp]
- include <stdlib.h>
- include <stdio.h>
- include <pthread.h>
static void * holder(void * gr) {
fprintf(stderr, "%s - tid: %x\n", __PRETTY_FUNCTION__, pthread_self());
sleep(5);
if (1) // This may only be true if this is the last "holder" thread! exit(0);
else pthread_exit(NULL);
}
static void createHolderThread() {
pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, 1); pthread_t bt; pthread_create(&bt, &attr, holder, NULL); pthread_attr_destroy(&attr);
}
static void * holded(void * gr) {
fprintf(stderr, "%s - tid: %x\n", __PRETTY_FUNCTION__, pthread_self());
sleep(10);
pthread_exit(NULL);
}
static pthread_t hd;
static void join_holded(void) {
fprintf(stderr, "%s - tid: %x\n", __PRETTY_FUNCTION__, pthread_self());
// You may want to cancel a thread actively during termination: pthread_cancel(hd); // respectively //pthread_kill();
pthread_join(hd, NULL);
}
static void createHoldedThread(void) {
pthread_create(&hd, NULL, holded, NULL); atexit(join_holded);
}
static void print_something(void) {
fprintf(stderr, "%s - printing ... tid: %x\n", __PRETTY_FUNCTION__, pthread_self());
}
int main(void) {
atexit(print_something);
createHolderThread();
createHoldedThread();
pthread_exit(NULL);
return 0;
}