多线程开发

线程:
线程是一种轻量级的进程,线程是操作系统调度的最小单位,可以理解为一个进程是一个或者多个线程组成的。Linux系统开发多线程程序大多使用pthread库,其操作函数基本都以pthread开头。
创建线程的函数定义如下:
int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(start_routine)(void), void *restrict arg);

thread返回创建线程的ID。
attr是一个pthread_attr_t类型的结构体,用来设置线程属性。
start_routine是一个函数指针,所指向的函数就是线程要运行的代码。
arg是start_routine指向的函数传入的参数,当执行用户的线程函数时,会把arg带的参数传入。
创建线程成功则返回0,失败返回错误号。

restrict:C语言中的一种类型限定符,用于告诉编译器,对象已经被指针所引用,不能通过除该指针外所有其他直接或间接的方式修改该对象的内容。

【pthread库创建线程】

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void* thread_func(void *arg)			//线程函数
{

        int *val = arg;
        printf("Hi I am a thread!\n");
        if(arg!=NULL)				//如果参数不为空,打印参数内容
        {
        printf("argument set: %d\n", *val);
        }
}

int main()
{
        pthread_t tid;				//线程ID
        int t_arg = 100;			//给线程传入的参数值

        if(pthread_create(&tid, NULL, thread_func, &t_arg))	//创建线程
                perror("Fail to create thread");

        sleep(1);				//睡眠1秒,等待线程执行
        printf("Main thread!\n");

        return 0;
}

【运行结果】
lwb@ubuntu:~/process$ gcc pthread.c -o pthread
/tmp/ccYhuSiD.o: In function main:
pthread.c:(.text+0x7c): undefined reference to pthread_create
collect2: error: ld returned 1 exit status
lwb@ubuntu:~/process$ gcc pthread.c -o pthread -lpthread
lwb@ubuntu:~/process$ ./pthread
Hi I am a thread!
argument set: 100
Main thread!

线程退出:
线程退出可以是代码运行结束后,自动退出,或者遇到return退出,也可以由其他线程通过pthread_cancel()函数来取消。
int pthread_cancel(pthread_t thread);

【pthread_cancel取消线程实例】

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void* thread_func(void *arg)		//线程函数
{
        int *val = arg;

        printf("Hi, I am a thread!\n");		
        if(arg!=NULL)			//如果参数不为空,打印参数内容
        {
                while(1)
                {
                printf("argument set: %d\n",*val);
                }
        }
}

int main()
{
        pthread_t tid;			//线程ID
        int t_arg = 100;		//给线程传入的参数值

        if(pthread_create(&tid, NULL, thread_func, &t_arg))	//创建线程
                perror("Fail to create thread");

        sleep(1);			//睡眠1秒,等待线程执行
        printf("Main thread!\n");
        pthread_cancel(tid);		//取消线程

        return 0;
}

【运行结果】
lwb@ubuntu:~/process$ gcc cancel.c -o cancel -lpthread
lwb@ubuntu:~/process$ ./cancel
Hi, I am a thread!
argument set: 100
{打印若干次:argument set: 100}
argument set: 100
Main thread!

主线程可以使用sleep()函数暂停自己的运行,从而等待新创建的线程结束,这种延时方式,会由于线程的运行时间不确定,导致程序的运行结果无法预测。实际使用时采用pthread_join()函数。
int pthread_join(pthread_t thread, void **value_ptr);

thread:等待线程的ID;
value_ptr:指向退出线程的返回值,被等待线程成功返回,返回值是0;

【多线程操作实例】

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void* mid_thread(void *arg);    //mid线程声明
void* term_thread(void *arg);   //term线程声明

void* mid_thread(void *arg)
{
	int times = 0;
	printf("mid thread create!\n");

	while(1)
	{
	printf("waitting term thread %d times!\n", times);
	sleep(2);
	times++;
	}
}

void* term_thread(void *arg)
{
	pthread_t *tid;
	printf("term thread created!\n");
	sleep(2);
	if(arg!=NULL)
	{
		tid=(pthread_t*)arg;
		pthread_cancel((*tid));
	}
}

int main()
{
	pthread_t mid_tid, term_tid;

	if(pthread_create(&mid_tid, NULL, mid_thread, NULL))
	{
		perror("Create mid thread error!\n");
		return 0;
	}

	if(pthread_create(&term_tid, NULL, term_thread, (void*)&mid_thread))
	{
		perror("Create term thread fail!\n");
		return 0;
	}

	if(pthread_join(mid_tid, NULL))
	{
		perror("wait mid thread error!\n");
		return 0;
	}

	if(pthread_join(term_tid, NULL))
	{
		perror("wait term thread error!\n");
		return 0;
	}

	return 0;
}

【运行结果】
lwb@ubuntu:~/process$ gcc pthread_demo.c -o pthread_demo -lpthread
lwb@ubuntu:~/process$ ./pthread_demo
term thread created!
mid thread create!
waitting term thread 0 times!
Segmentation fault (core dumped) //原因不明;

摘自:弓雷 ARM嵌入式Linux系统开发详解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值