In the previous chapter we saw how to create a thread and how to join and detach thread.
In this chapter we shall further see, what are the different thread functions available to us.
In this chapter we shall learn about:
1. API to thread ID
2. API to for thread compare
3. API to initialize a routing only once.
4. API to set priority to a thread.
5. API to get the priority of a thread.
6. API to send a signal to a thread.
7. API to terminate a thread.
8. API to cancel a thread.
1. To get the ID of a thread, use below API:
pthread_t pthread_self(void);
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <pthread.h> //for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com void* func(void* p) { printf("The thread id = %d\n", pthread_self()); pthread_exit(NULL); return NULL; } int main() { pthread_t thread; pthread_create(&thread, NULL, func, NULL); printf("From the main function, the thread id = %d\n", thread); pthread_join(thread, NULL); return 0; }
Output;
From the main function, the thread id = 120414208 The thread id = 120414208
2. To compare 2 different threads, use below API:
int pthread_equal(pthread_t tid1, pthread_t tid2);
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <pthread.h> //for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com pthread_t thread_1_id; void* func(void* p) { printf("The thread id = %d\n", pthread_self()); if( pthread_equal(thread_1_id, pthread_self()) ) { printf("\n Both of the threads are equal"); } else { printf("\n Both threads are not equal"); } pthread_exit(NULL); return NULL; } int main() { pthread_t thread; pthread_create(&thread, NULL, func, NULL); printf("\nFrom the main function, the thread id = %d", thread); thread_1_id = thread; pthread_join(thread, NULL); return 0; }
Output:
From the main function, the thread id = 29249536 The thread id = 29249536 Both of the threads are equal
3. To initialize a routing only once, use below API. The subsequent call will not have any effect.
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
4. To set priority to a thread use below API:
int pthread_setschedparam(pthread_t tid, int policy, const struct sched_param *param);
Priority should be set before creating a thread.
5. To get the priority of a thread, use below API:
int pthread_getschedparam(pthread_t tid, int policy, struct schedparam *param);
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <pthread.h> #include <sched.h> //for more tutorials on C, C++, STL, DS, Linux visit www.ProDeveloperTutorial.com void* func(void* p) { pthread_exit(NULL); return NULL; } int main() { pthread_t thread; pthread_attr_t tattr; int ret; struct sched_param param; int policy = 0; printf ("The priority is = %d\n", pthread_getschedparam(thread, &policy, ¶m)); pthread_create(&thread, &tattr, func, NULL); pthread_join(thread, NULL); return 0; }
Output:
The priority is = 3
6. To send a signal to a thread, use below API:
pthread_kill(tid, sig);
7. To terminate a thread, use below API:
pthread_exit(&status); /* exit with status */
8. To cancel a thread, use below API:
pthread_cancel(thread);