一直对多线程比较好奇,后来在移动编程课上学了基本的创建进程,加入进程。

创建3个进程,加入3个进程代码如下:

#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
void *thread_function(int x){
        while(1){
                printf("I am thread %d\n",x);
                sleep(1);
        }
}
int main(){
        int res1,res2,res3;
        pthread_t a_thread,b_thread,c_thread;
        res1 = pthread_create(&a_thread,NULL,thread_function,1);
        res2 = pthread_create(&b_thread,NULL,thread_function,2);
        res3 = pthread_create(&c_thread,NULL,thread_function,3);
        res1 = pthread_join(a_thread,NULL);
        printf("%d\n",res1);
        res2 = pthread_join(b_thread,NULL);
        res3 = pthread_join(c_thread,NULL);
        
        return 0;
}

之后我还将对线程的各个参数及函数进行实验,敬请期待。