pthread
接触了一点点多线程的开发,想参照网上教程使用pthread开发时,发现无法导入pthread.h头文件,终于让我发现了如何导入pthread.h
参考使用VS的Nuget模块获取和管理第三方库。
但是使用Nuget获取到的pthread,并不能使用啊!!!!可以导入包,但是无法使用相应的函数,于是回归原始方法,下载、配置。
参考【使用pthread.h中遇到的问题】
问题解决,程序正常运行。
ps.使用Nuget安装pthread后,也按照相关的步骤配置了包路径,但还是无效。。
续
这个线程还是太奇怪了e,使用格式化输出总是有点问题。
看图,
void* say_hello1(void* args)
{
pthread_t pid = pthread_self(); //获取当前线程id
printf("[%lu]hello in thread %d 【%d】\n", pid, *(int*)args);
...//剩下的就不展示了
}
就这么一句格式化输出,如果使用两个%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;
}