POSIX线程库(一)创建第一个pthread线程

对于这个简单的示例,步骤非常简单:创建线程-线程退出-同步退出。

对于POSIX线程相关的函数,无论是变量,还是属性,凡是有要使用的,一般都将会传入对象的地址,目前只有pthread_joint除外,传入的是变量的值。另外,Linux函数的返回值非常有规律,没有消息(等于0)就是好消息,如果有就是坏消息(不为0的错误代码)。这么一说其实fopen其实有点不一样,此外,pthread类相关的函数不会设置错误代码。

一、使用前的准备

  • 定义宏 __REENTRANT,开启可重入功能
  • 引用头文件 #include<pthread.h>
  • 编译链接库 -lpthread

二、创建一个线程pthread_create()

函数的原型如下:

int pthread_create(pthread_t * thread,pthread_attr_t * attr,\
void *(*start_rountine)(void *),void * arg)

输入参数可以用一句话说明:
“线程及其属性,入口及其参数”

第一个参数类似于文件指针fp,用户通过这个指针来操作线程对象。

第二个参数类似于文件的打开方式,用户通过这个来设置线程线程对象属性。在不深入的情况下,设置为NULL(使用默认参数)即可。

第三个参数是一个函数指针,函数的输入和输出都为一个空指针void *,指针名字为* start_rountine, 通常就是&函数名

第四个参数是一个参数也是一个空指针,类型解释的权利在函数内部,通常就是传入&变量or 结构体名

函数成功创建一个线程则返回0,创建失败则返回错误代码(非0)。

三、返回一个线程pthread_exit()

函数原型如下:

void pthread_exit(void * retval)

终止一个线程并返回某个对象的指针。注意不要返回一个局部变量。pthread_exit()等同于函数的return语句。

四、等待线程同步pthread_join()

int pthread_join(pthread_t th,void **thread_return);

等待指定线程结束。

第一个参数是:要等待的线程

第二个参数是:指针的指针,最后指向线程返回值。

五、程序[1]

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

char message[]="Hello Thread World";

void *thread_function(void *arg)
{
	printf("thread function is running, Argument is %s\n",(char *)arg);
	sleep(3);
	strcpy(message,"Bye!");
	pthread_exit((void *)"Thank you for the CPU time");
}

int main()
{
	int res;
	pthread_t a_thread;
	void *thread_result;

	res=pthread_create(&a_thread,NULL,thread_function,(char *)message);
	if(res!=0)
	{
		perror("thread creation failed");
		exit(EXIT_FAILURE);
	}
	printf("Waiting for thread to finish....\n");
	
	res=pthread_join(a_thread,&thread_result);
	if(res!=0)
	{
		perror("Thread join failed");
		exit(EXIT_FAILURE);
	}

	printf("Thread joined, it return %s\n",(char *)thread_result);
	printf("Messege is now %s\n",message);
	exit(EXIT_SUCCESS);

}

[1]马修, N. ), 斯通斯,等. Linux程序设计: 第4版[M]. 人民邮电出版社, 2010.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【任务1】: 在 Linux 系统下使用 pthread.h 头文件编写多线程程序,需要使用以下步骤: 1. 包含 pthread.h 头文件 2. 创建线程并指定线程处理函数 3. 等待线程结束并释放资源 下面是一个简单的例子,演示了如何使用 pthread 创建和使用多线程: ``` #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *my_thread_function(void *arg) { int *my_arg = (int *) arg; printf("Hello from thread %d\n", *my_arg); return NULL; } int main() { pthread_t my_thread; int thread_arg = 42; int ret; ret = pthread_create(&my_thread, NULL, my_thread_function, &thread_arg); if (ret != 0) { printf("Error creating thread\n"); exit(1); } printf("Hello from the main thread\n"); ret = pthread_join(my_thread, NULL); if (ret != 0) { printf("Error joining thread\n"); exit(1); } return 0; } ``` 在这个例子,我们创建一个名为 my_thread线程,并将其处理函数设置为 my_thread_function。在 main() 函数,我们打印了一条消息,然后等待 my_thread 线程结束。my_thread_function 简单地打印了一条消息,然后返回 NULL。 【任务2】: 下面是一个例子,演示了如何创建两个线程一个线程计算两个变量的加法,另一个线程计算两个变量的减法: ``` #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *add_thread_function(void *arg) { int *args = (int *) arg; int result = args[0] + args[1]; printf("Addition result: %d\n", result); return NULL; } void *sub_thread_function(void *arg) { int *args = (int *) arg; int result = args[0] - args[1]; printf("Subtraction result: %d\n", result); return NULL; } int main() { pthread_t add_thread, sub_thread; int args[2] = {5, 3}; int ret; ret = pthread_create(&add_thread, NULL, add_thread_function, args); if (ret != 0) { printf("Error creating add thread\n"); exit(1); } ret = pthread_create(&sub_thread, NULL, sub_thread_function, args); if (ret != 0) { printf("Error creating sub thread\n"); exit(1); } ret = pthread_join(add_thread, NULL); if (ret != 0) { printf("Error joining add thread\n"); exit(1); } ret = pthread_join(sub_thread, NULL); if (ret != 0) { printf("Error joining sub thread\n"); exit(1); } return 0; } ``` 在这个例子,我们创建了两个线程,分别计算加法和减法的结果。我们使用了一个名为 args 的数组来传递参数,args[0] 和 args[1] 分别表示要计算的两个数。注意,我们传递的参数是一个数组,而不是两个单独的变量。这是因为 pthread_create() 函数的第四个参数必须是一个指向 void 的指针,但我们需要传递两个 int 类型的值。 在 main() 函数,我们依次创建两个线程,并等待它们结束。注意,我们在创建线程时传递了 args 数组的地址,以便在线程函数访问它。此外,我们还检查了 pthread_create() 和 pthread_join() 函数的返回值,以确保没有错误发生。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值