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

Best way to replace the key in json object

JSON (JavaScript Object Notation, JS Object Notat...

How to use MySQL binlog to restore accidentally deleted databases

Table of contents 1 View the current database con...

MySQL sorting using index scan

Table of contents Install sakila Index Scan Sort ...

mysql 5.7.18 winx64 password change

After MySQL 5.7.18 is successfully installed, sin...

Implementation of k8s deployment of docker container

Environment: (docker, k8s cluster), continue with...

How to handle token expiration in WeChat Mini Programs

Table of contents Conclusion first question Solut...

The One-Hand Rule of WEB2.0

<br />My previous article about CSS was not ...

js realizes the dynamic loading of data by waterfall flow bottoming out

This article shares with you the specific code of...

How to create your own Docker image and upload it to Dockerhub

1. First register your own dockerhub account, reg...

Detailed explanation of docker nginx container startup and mounting to local

First, the structure inside the nginx container: ...

Detailed tutorial for springcloud alibaba nacos linux configuration

First download the compressed package of nacos fr...

Viewing and analyzing MySQL execution status

When you feel that there is a problem with MySQL ...

How to build YUM in Centos7 environment

1. Enter the configuration file of the yum source...

Detailed tutorial on downloading mysql on Windows 10

MySQL versions are divided into Enterprise Editio...

Example code for configuring monitoring items and aggregated graphics in Zabbix

1. Install Zabbix Agent to monitor the local mach...