vs2015使用pthread

pthread

接触了一点点多线程的开发,想参照网上教程使用pthread开发时,发现无法导入pthread.h头文件,终于让我发现了如何导入pthread.h
参考使用VS的Nuget模块获取和管理第三方库
但是使用Nuget获取到的pthread,并不能使用啊!!!!可以导入包,但是无法使用相应的函数,于是回归原始方法,下载、配置。
参考【使用pthread.h中遇到的问题
问题解决,程序正常运行。

ps.使用Nuget安装pthread后,也按照相关的步骤配置了包路径,但还是无效。。在生成自定义中勾选pthread

这个线程还是太奇怪了e,使用格式化输出总是有点问题。
看图,

void* say_hello1(void* args)
{
	pthread_t pid = pthread_self(); //获取当前线程id
	printf("[%lu]hello in thread  %d  【%d】\n", pid, *(int*)args);
	...//剩下的就不展示了
}

输出多了一个0
就这么一句格式化输出,如果使用两个%d,那就可以把函数传入的参数打印,不然由于前面多了一个0总是会把后面想要输出的变量挤掉。好像中间出了一个幽灵变量,啊难受。
这里是完整代码,这里复制的@hitwengqi改了格式化输出,因为cout无法输出pthread_t值。

#include "stdafx.h"
#include <iostream>
#include <pthread.h>
#include <stdio.h>

using namespace std;

#define BOUNDARY 5

int tasks = 10;
pthread_mutex_t tasks_mutex; //互斥锁
pthread_cond_t tasks_cond; //条件信号变量,处理两个线程间的条件关系,当task>5,hello2处理,反之hello1处理,直到task减为0

void* say_hello2(void* args)
{
	pthread_t pid = pthread_self(); //获取当前线程id
	printf("[%lu]hello in thread  【%d%d】\n", pid, *((int*)args));
	//cout << "[" << pid << "] hello in thread " << *((int*)args) << endl;

	bool is_signaled = false; //sign
	while (1)
	{
		pthread_mutex_lock(&tasks_mutex); //加锁
		if (tasks > BOUNDARY)
		{
			printf("[%lu]take task %d%d in thread  【%d】\n", pid, tasks, *((int*)args));
			//cout << "[" << pid << "] take task: " << tasks << " in thread " << *((int*)args) << endl;
			--tasks;//modify
		}
		else if (!is_signaled)
		{
			printf("[%lu]pthread_cond_signal in thread  【%d%d】\n", pid, *((int*)args));
			//cout << "[" << pid << "] pthread_cond_signal in thread " << *((int*)args) << endl;
			pthread_cond_signal(&tasks_cond); //signal:向hello1发送信号,表明已经>5
			is_signaled = true; //表明信号已发送,退出此线程
		}
		pthread_mutex_unlock(&tasks_mutex); //解锁
		if (tasks == 0)
			break;
	}
	return (void*)2;
}

void* say_hello1(void* args)
{
	pthread_t pid = pthread_self(); //获取当前线程id
	printf("[%lu]hello in thread  %d  【%d】\n", pid, *(int*)args);
	//cout << "[" << pid << "] hello in thread " << *((int*)args) << endl;
	while (1)
	{
		pthread_mutex_lock(&tasks_mutex); //加锁
		if (tasks > BOUNDARY)
		{
			printf("[%lu]pthread_cond_signal in thread  【%d】\n", pid, (int*)args);
			//cout << "[" << pid << "] pthread_cond_signal in thread " << *((int*)args) << endl;
			pthread_cond_wait(&tasks_cond, &tasks_mutex); //wait:等待信号量生效,接收到信号,向hello2发出信号,跳出wait,执行后续 
		}
		else
		{
			printf("[%lu]take task %d%d in thread 【%d】\n", pid, tasks, *((int*)args));
			//cout << "[" << pid << "] take task: " << tasks << " in thread " << *((int*)args) << endl;
			--tasks;
		}
		pthread_mutex_unlock(&tasks_mutex); //解锁
		if (tasks == 0)
			break;
	}
	return (void*)1;
}
int main()
{
	pthread_attr_t attr; //线程属性结构体,创建线程时加入的参数
	pthread_attr_init(&attr); //初始化
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); //是设置你想要指定线程属性参数,这个参数表明这个线程是可以join连接的,join功能表示主程序可以等线程结束后再去做某事,实现了主程序和线程同步功能
	pthread_cond_init(&tasks_cond, NULL); //初始化条件信号量
	pthread_mutex_init(&tasks_mutex, NULL); //初始化互斥量
	pthread_t tid1, tid2; //保存两个线程id
	tasks = 10;
	int index1 = 1;
	int ret = pthread_create(&tid1, &attr, say_hello1, (void*)&index1);
	if (ret != 0)
	{
		cout << "pthread_create error:error_code=" << ret << endl;
	}
	
	int index2 = 2;
	ret = pthread_create(&tid2, &attr, say_hello2, (void*)&index2);
	if (ret != 0)
	{
		cout << "pthread_create error:error_code=" << ret << endl;
	}
	pthread_join(tid1, NULL); //连接两个线程
	pthread_join(tid2, NULL);
	pthread_attr_destroy(&attr); //释放内存 
	pthread_mutex_destroy(&tasks_mutex); //注销锁
	pthread_cond_destroy(&tasks_cond); //正常退出
	printf("%d %d\n");
	getchar();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VS2022中,没有内置的pthread.h头文件。这是因为pthread.h是POSIX线程标准的头文件,在Windows操作系统中并不原生支持。因此,如果你在VS2022中遇到了无法打开源文件"pthread.h"的问题,你需要自己手动添加pthread.h头文件。 你可以按照以下步骤在VS2022中添加pthread.h头文件: 1. 打开你的项目并进入项目属性。 2. 在"配置属性"下,选择"C/C++"。 3. 在"C/C++"选项卡中,选择"常规"。 4. 在"附加包含目录"一栏中,添加包含pthread.h头文件的文件夹路径。 5. 确认并保存更改。 通过执行以上步骤,你就能够成功解决在VS2022中无法找到pthread.h头文件的问题。请确保你已经正确安装了pthread库,并将pthread.h头文件放置在正确的文件夹中。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [VS编译时无法打开 源 文件 "pthread.h处理方法](https://download.csdn.net/download/ma950924/10545742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Windows多线程编程缺少pthread.h文件问题](https://download.csdn.net/download/duan19920101/86508362)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值