Specific use of pthread_create in linux to create threads

Specific use of pthread_create in linux to create threads

pthread_create function

Function Introduction

pthread_create is a UNIX environment thread creation function

Header Files

#include<pthread.h>

Function declaration

int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

Return Value

If successful, it returns 0, otherwise it returns an error number

parameter

The first parameter is a pointer to the thread identifier.

The second parameter is used to set thread attributes.

The third parameter is the address of the function that the thread runs.

The last argument is the parameters to run the function with.

Notice

When compiling, remember to add the -lpthread parameter to call the static link library. Because pthread is not the default library of the Linux system.

pthread_join Function

Function Introduction

The function pthread_join is used to wait for the end of a thread.

The function prototype is:

extern int pthread_join __P (pthread_t __th, void **__thread_return);

parameter:

The first parameter is the thread identifier being waited for.

The second parameter is a user-defined pointer that can be used to store the return value of the waited thread.

Notice

This function is a thread blocking function. The function that calls it will wait until the waiting thread ends. When the function returns, the resources of the waiting thread are reclaimed. If the execution is successful, it will return 0, if it fails, it will return an error number.

example:

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

/*Declare the structure*/
struct member
{
  int num;
  char *name;
};

/* Define thread pthread */
static void * pthread(void *arg)
{
  struct member *temp;

  /* Thread pthread starts running*/
  printf("pthread start!\n");

  /* Let the main thread continue to execute */
  sleep(2);

  /* Print the passed parameters */
  temp = (struct member *)arg;
  printf("member->num:%d\n",temp->num);
  printf("member->name:%s\n",temp->name);

  return NULL;
}

/* main function */
int main(int agrc,char* argv[])
{
  pthread_t tidp;
  struct member *b;

  /* Assign a value to the structure variable b*/
  b = (struct member *)malloc(sizeof(struct member));
  b->num=1;
  b->name="mlq";

  /* Create a thread pthread */
  if ((pthread_create(&tidp, NULL, pthread, (void*)b)) == -1)
  {
    printf("create error!\n");
    return 1;
  }

  /* Let the thread pthread run first*/
  sleep(1);

  /* Thread pthread sleeps for 2s, then main can be executed first*/
  printf("mian continue!\n");

  /* Waiting for thread pthread to be released*/
  if (pthread_join(tidp, NULL))
  {
    printf("thread is not exit...\n");
    return -2;
  }

  return 0;
}

Compilation and execution results

The compilation and execution results are shown in the figure below. You can see that the main thread main and the thread pthread are executed alternately. That is to say, after we created the thread pthread, both threads were executed, proving that the creation was successful. In addition, you can see that when the thread pthread is created, the passed parameters are printed correctly.


This is the end of this article about the specific use of pthread_create in linux to create threads. For more relevant linux pthread_create content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Study and summary based on pthread_create, readlink, getpid and other functions
  • Solution to undefined reference to ''pthread_create''
  • C language pthread_create() function explanation

<<:  js realizes the function of clicking to switch cards

>>:  The front-end must know how to lazy load images (three methods)

Recommend

Installation method of mysql-8.0.17-winx64 under windows 10

1. Download from the official website and unzip h...

Web designers also need to learn web coding

Often, after a web design is completed, the desig...

The most complete 50 Mysql database query exercises

This database query statement is one of 50 databa...

A brief discussion on the principle of shallow entry and deep exit of MySQL

Table of contents 1. Overview of the page 2. Infi...

js dynamically adds example code for a list of circled numbers

1. Add the ul tag in the body first <!-- Unord...

In-depth analysis of JDBC and MySQL temporary tablespace

background Temporary tablespaces are used to mana...

Using vue3 to implement counting function component encapsulation example

Table of contents Preface 1. The significance of ...

MySQL 8.x msi version installation tutorial with pictures and text

1. Download MySQL Official website download addre...

Four completely different experiences in Apple Watch interaction design revealed

Today is still a case of Watch app design. I love...

Vue two fields joint verification to achieve the password modification function

Table of contents 1. Introduction 2. Solution Imp...