C语言多线程

文章目录

创建线程

在C语言中创建线程方式如下

#include <pthread.h>
pthread_create (thread, attr, start_routine, arg)

其中,thread为线程指针;attr为线程属性,默认NULLstart_routine为线程运行函数起始地址;arg为运行函数的参数。无参数时使用NULL

如果线程成功创建,函数返回0,否则说明创建失败。

当线程完成工作后,若无需继续存在,则会调用pthread_exit函数。也就是说,这个函数可以显式地退出一个线程。

pthread_exit (status)

实战

下面做一个简单的示例,通过调用多线程来打印数字

//test1.c
#include<stdio.h>
#include<pthread.h>
#define NUM_THREADS 5

void *PrintTh(void *th){
	int i = *((int*)th);
	printf("Hello, I'm thread %d
", i);
	return 0;
}

int main(){
	int i,ret;
	pthread_t p;
	for(i=0; i<NUM_THREADS; i++){
		printf("create th %d
",i);
		ret = pthread_create(&p,NULL, PrintTh, (void*)&i);
		if(ret != 0)
			printf("th %d error, code = 
",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 5
 
typedef struct PERSON{
    int id;
    char name;
}Person, *pPerson;

void * PrintTh(void * p)
{
    pPerson tid = (pPerson)p;
    printf("I'm %c ,th %d
", 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 
",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: 创建线程失败!
");
            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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值