xxxxxxxxxx
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
Compile and link with -pthread.
xxxxxxxxxx
pthread_create(&IDthread ,&attribute , Functionexecute ,(void *)struct );
xxxxxxxxxx
// thread0.c
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *worker_thread(void *arg)
{
printf("This is worker_thread()\n");
pthread_exit(NULL);
}
int main()
{
pthread_t my_thread;
int ret;
printf("In main: creating thread\n");
ret = pthread_create(&my;_thread, NULL, &worker;_thread, NULL);
if(ret != 0) {
printf("Error: pthread_create() failed\n");
exit(EXIT_FAILURE);
}
pthread_exit(NULL);
}
xxxxxxxxxx
pthread_t tid; //declaration of thread id
pthread_create(&tid, NULL, &routine, NULL);
//routine is a function that takes in void pointer and returns a void pointer
pthread_join(tid)
xxxxxxxxxx
// pthread_create() used to creat a new thread passing the attributes if needed using
// argument attr the thread is executing the function pass in the void *(*start_routine)(void*)
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void*), void *arg);