Complete the code to create a new thread using the POSIX pthread library.
pthread_t thread_id;
pthread_create(&thread_id, NULL, [1], NULL);The pthread_create function requires the name of the function that the new thread will execute. This function is passed as the third argument. Here, start_thread is the correct function name to start the thread.
Complete the code to wait for a thread to finish execution.
pthread_t tid;
// thread created earlier
pthread_join([1], NULL);The pthread_join function waits for the thread specified by its thread ID to finish. The thread ID variable here is tid.
Fix the error in the thread function declaration to match pthread requirements.
void* [1](void* arg) { // thread code return NULL; }
The thread function must return void* and accept a single void* argument. The correct declaration is void* thread_func(void* arg).
Fill both blanks to create a thread and wait for it to finish.
pthread_t tid; pthread_create(&tid, NULL, [1], NULL); pthread_[2](tid, NULL);
The thread is created with pthread_create using the function thread_func. To wait for the thread to finish, pthread_join is called with the thread ID.
Fill all three blanks to create a thread, pass an argument, and wait for it to finish.
void* [1](void* arg) { int* num = (int*)[2]; printf("Number: %d\n", *num); return NULL; } pthread_t tid; int value = 5; pthread_create(&tid, NULL, [3], &value); pthread_join(tid, NULL);
The thread function is named thread_func. Inside it, the argument pointer is named arg and cast to int*. The same function name thread_func is passed to pthread_create.