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

Docker installs ClickHouse and initializes data testing

Clickhouse Introduction ClickHouse is a column-or...

Detailed example of creating and deleting tables in MySQL

The table creation command requires: The name of...

SQL function to merge a field together

Recently, I need to query all the fields in a rel...

Install Centos7 using Hyper-v virtual machine

Table of contents introduce Prepare Download syst...

Introduction to the three essential logs for MySQL database interviews

Table of contents 1. redo log (transaction log of...

Summary of five commands to check swap space in Linux

Preface Two types of swap space can be created un...

CSS performance optimization - detailed explanation of will-change usage

will-change tells the browser what changes will h...

Vue implements sample code to disable browser from remembering password function

Find information Some methods found on the Intern...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

SystemC environment configuration method under Linux system

The following is the configuration method under c...

centos7.2 offline installation mysql5.7.18.tar.gz

Because of network isolation, MySQL cannot be ins...

How to implement remote access control in Centos 7.4

1. SSH remote management SSH is a secure channel ...

How to install phabricator using Docker

I am using the Ubuntu 16.04 system here. Installa...

HTML design pattern daily study notes

HTML Design Pattern Study Notes This week I mainl...

Detailed explanation of Angular component life cycle (I)

Table of contents Overview 1. Hook calling order ...