Many times when we write code, we will encounter multi-threading. Next, I simply thank a multi-threaded program directly on the code.
#include <iostream>
using namespace std;
#include <pthread.h>
#define NUM_THERADS 5
void * say_hello(void * args)
{
cout << "Hello Runoob!" <<*((int *)args) << endl ;
return 0;
}
/*
Prototype: int pthread_create((pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Usage: #include <pthread.h>
Function: Create thread( In fact, it is to determine the entry point of calling the thread function), after the thread is created, it starts to run the related thread function.
Description: thread: thread identifier;
attr: thread attribute setting;
start_routine: starting address of the thread function;
arg : parameter passed to start_routine;
return value: success, return 0; error, return -1.
*/
int main()
{
pthread_t tids[NUM_THERADS];
static int j =0;
for(int i= 0;i<NUM_THERADS; ++i)
{
j++;
int ret = pthread_create(&tids[i],NULL,say_hello,(void *)&(j));
if(ret != 0)
{
cout << "pthread_create error : error_code"<< ret << endl;
}
}
pthread_exit(NULL);
}