Pthread_join Two-level pointer parameter analysis for thread wait function

Source: Internet
Author: User
Tags function prototype

The function prototype for the thread creation function pthread_create is as follows:

int pthread_create (pthread_t *restrict thread,
Const pthread_attr_t *restrict attr,
void * (*start_routine) (void*), void *restrict arg);

Where thread is the threading to be created;

Attr the properties of the specified thread, the default value is null;

Start_routine is the function that the thread executes;

ARG is a parameter passed to a function executed by a thread;

Note that: Start_routine is a function pointer

The function prototype for the thread wait function pthread_create is as follows:

int Pthread_join (pthread_t thread, void **value_ptr);

Where thread is the name of the threads to wait for;

VALUE_PTR: pointer *value_ptr points to the parameters returned by the thread

Note When using: For VALUE_PTR, you should declare a first-level pointer, and then pass the address to the Pthread_create function, instead of directly defining a level two pointer, the level two pointer is passed directly to Pthread_create.

Such as:

Correct method of delivery:

void *ret;

Pthread_join (thread, &ret);

Error Delivery Method:

void **ret;

Pthread_join (thread, ret);

Cause: There is a sentence of type such as Pthread_join: (* value_ptr) = arg; If you pass the parameter in the correct way, the statement on the left actually completes the operation: ret = arg; However, when parameters are passed in the wrong way, a fatal error occurs: (* value_ptr), which takes a value operation on a pointer that has not yet been initialized, which is not allowed by the system, at which point (* value_ptr) is equivalent to *ret,*ret or a pointer, is a level two pointer, but *ret is a wild pointer!

Watch out! You return is a first-level pointer, then the storage can only be stored with * &ret (hope you understand, is the pointer demotion) =ret;

Then the output is naturally ret.

Here is an example:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

#define N 64

int a = 10;
void * Handler (void *arg)
{
printf ("a=%d,%s\n", A, (char *) arg);
strcat ((char *) arg, "...");
Pthread_exit (ARG);
}

int main ()
{
pthread_t Tid;
Char Buf[n] = {"Welcome"};
void *result;

if (Pthread_create (&tid, NULL, Handler, (void *) buf)! = 0)
Exit (-1);
printf ("*\n");
Pthread_join (Tid, &result);
printf ("ret:%s\n", (char *) result);

return 0;
}

Pthread_join Two-level pointer parameter analysis for thread wait function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.