The eight-thread creation experiment of the Linux operating system experiment series

1. The purpose of the experiment
• Understand the concept of threads
• Master the basic methods of creating threads under Linux

2. Experimental content:
POSIX threads (POSIX threads), referred to as Pthreads, is the POSIX standard for threads. Because pthread is not the default library of the Linux system, but the POSIX thread library. It is used as a library in Linux, so add -lpthread (or -pthread) to link the library explicitly. The POSIX standard defines a set of APIs for creating and manipulating threads. In Unix-like operating systems (Unix, Linux, Mac OS X, etc.), Pthreads are used as threads of the operating system.

Three, the experimental environment

Linux operating system

Fourth, the experimental process and operating results

源代码:
#include<stdio.h>
#include<pthread.h>

void* run(void* arg)
{
int i;
for(i=0;i<5;i++)
{
printf(“hello in thread(%d).\n”,(unsigned long)arg);
usleep(1000);
}
}

int main()
{
pthread_t id[5];
int i;
for(i=0;i<5;i++)
pthread_create(&id[i],NULL,run,(void*)(long)i);
for(i=0;i<5;i++)
pthread_join(id[i],NULL);
}

Result graph:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43372169/article/details/110522032