C语言多线程
文章目录
- 创建线程
- 实战
创建线程
在C语言中创建线程方式如下
#include <pthread.h>
pthread_create (thread, attr, start_routine, arg)
其中,thread为线程指针;attr为线程属性,默认NULL;start_routine为线程运行函数起始地址;arg为运行函数的参数。无参数时使用NULL。
如果线程成功创建,函数返回0,否则说明创建失败。
当线程完成工作后,若无需继续存在,则会调用pthread_exit函数。也就是说,这个函数可以显式地退出一个线程。
pthread_exit (status)
实战
下面做一个简单的示例,通过调用多线程来打印数字
//test1.c
#include<stdio.h>
#include<pthread.h>
#define NUM_THREADS 5void *PrintTh(void *th){int i = *((int*)th);printf("Hello, I'm thread %d\n", i);return 0;
}int main(){int i,ret;pthread_t p;for(i=0; i<NUM_THREADS; i++){printf("create th %d\n",i);ret = pthread_create(&p,NULL, PrintTh, (void*)&i);if(ret != 0)printf("th %d error, code = \n",i);}pthread_exit(NULL);return 0;
}
如果在Linux中使用pthread,需要使用库libpthread.a, 编译时要加-lpthread参数:
gcc test1.c -lpthread -o test1.o ./test1.
运行结果如下
create th 0
create th 1
Hello, I’m thread 1
Hello, I’m thread 2
create th 2
Hello, I’m thread 3
create th 3
create th 4
Hello, I’m thread 4
Hello, I’m thread 4
之所以main函数和printTh输出的值不同,是因为我们传入printTh的参数是一个空指针,这个空指针直接指向了用于循环的i的地址,所以printTh调用i的时候,有时i的取值刚好发生变化,有时尚未发生变化。
由于函数pthread_create接收的传输参数必须为void*,而且只有一个,故当需要传递多个参数时,需要用到结构体。
// test2.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define NUM_THREADS 5typedef struct PERSON{int id;char name;
}Person, *pPerson;void * PrintTh(void * p)
{pPerson tid = (pPerson)p;printf("I'm %c ,th %d\n", tid->name,tid->id);return 0;
}int main(void)
{pthread_t Pthread[NUM_THREADS];Person persons[NUM_THREADS];int i, ret;for (i = 0; i < NUM_THREADS; i++){printf("create th %d \n",i);persons[i].id = i;persons[i].name = 'A'+i%10;ret = pthread_create(&Pthread[i], NULL, PrintTh, (void *)&persons[i]);if (0 != ret){printf("Error: 创建线程失败!\n");exit(-1);}}pthread_exit(NULL);return 0;
}
输出结果为
create th 0
create th 1
I’m A ,th 0
I’m B ,th 1
create th 2
create th 3
I’m C ,th 2
I’m D ,th 3
create th 4
I’m E ,th 4