- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用硬件模拟器,它使用 PIN 工具来执行工作负载。作为工作负载,我使用以下代码。虽然它可以在带有 -lpthread 标志的 Ubuntu 上运行,但在加入线程时它会在模拟器上卡住。
我认为 native 操作系统可以容忍但模拟器不能容忍的代码中存在一些不安全的地方。最合适的编码方式是什么?
main.h:
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <pthread.h>
#include <stdint.h>
#include <getopt.h>
#include <set>
#include <vector>
#include <algorithm>
#include <iterator>
#define NUM_OF_VERTICES 4
#define NUM_OF_PTHREADS (NUM_OF_VERTICES*(NUM_OF_VERTICES-1)/2)
std::string payload_texts[NUM_OF_VERTICES];
void payload_text_initialize();
double indices [NUM_OF_VERTICES][NUM_OF_VERTICES];
class thread_args {
public:
uint index1, index2;
unsigned long value = 0;
unsigned long * valuePointer = &value;
};
main.cc:
#include "main.h"
extern "C" {
extern void mcsim_skip_instrs_begin();
extern void mcsim_skip_instrs_end();
extern void mcsim_spinning_begin();
extern void mcsim_spinning_end();
int32_t log_2(uint64_t);
}
using namespace std;
set<char> find_uniques(string str){
set<char> unique_chars;
for (int i = 0 ; i < str.length() ; i++ ){
char c = str.at(i);
if (unique_chars.find(c) == unique_chars.end())
unique_chars.insert(c);
}
return unique_chars;
}
void * jaccard_visit(void *arg){
thread_args * args = (thread_args *) arg;
set<char> setunion;
set<char> intersect;
set<char> set1 = find_uniques(payload_texts[args->index1]);
set<char> set2 = find_uniques(payload_texts[args->index2]);
std::set_intersection(set1.begin(),set1.end(),set2.begin(),set2.end(),std::inserter(intersect,intersect.begin()));
std::set_union(set1.begin(),set1.end(),set2.begin(),set2.end(),std::inserter(setunion,setunion.begin()));
double similarity = ((double) intersect.size()) / ((double) setunion.size());
indices[args->index1][args->index2] = similarity;
indices[args->index2][args->index1] = similarity;
unsigned long a = 1;
unsigned long b = 1;
unsigned long c = a + b;
for (int i = 3 ; i < 100000 * (similarity - 0.9) ; i++){
a = b;
b = c;
c = a + b;
}
*(args->valuePointer) = c;
return NULL;
}
void execute_parallel(){
pthread_t threads[NUM_OF_PTHREADS]; //array to hold thread information
thread_args *th_args = (thread_args*) malloc(NUM_OF_PTHREADS * sizeof(thread_args));
cout << "NUM_OF_PTHREADS is " << NUM_OF_PTHREADS << endl;
uint k = 0 ;
for (int i = 0 ; i < NUM_OF_VERTICES ; i++){
for (int j = i+1 ; j < NUM_OF_VERTICES ; j++){
th_args[k].index1 = i;
th_args[k].index2 = j;
th_args[k].value = i+j;
th_args[k].valuePointer = &(th_args[k].value);
pthread_create(&threads[k], NULL, jaccard_visit, (void*) &th_args[k]);
cout << "Thread " << k << " is started" << endl;
k++;
}
}
cout << "k is " << k << endl;
for(int i = 0; i < NUM_OF_PTHREADS; i++){
cout << "Thread " << i << " is joined" << endl;
pthread_join(threads[i], NULL);
}
cout << "Free threads" << endl ;
free(th_args);
}
void manual_schedule(){
pthread_t th0, th1, th2, th3, th4, th5;
thread_args arg0, arg1, arg2, arg3, arg4, arg5;
arg0.index1 = 0; arg0.index2 = 1; arg0.value = 0; arg0.valuePointer = &arg0.value;
arg1.index1 = 0; arg1.index2 = 2; arg1.value = 1; arg1.valuePointer = &arg1.value;
arg2.index1 = 0; arg2.index2 = 3; arg2.value = 2; arg2.valuePointer = &arg2.value;
arg3.index1 = 1; arg3.index2 = 2; arg3.value = 3; arg3.valuePointer = &arg3.value;
arg4.index1 = 1; arg4.index2 = 3; arg4.value = 4; arg4.valuePointer = &arg4.value;
arg5.index1 = 2; arg5.index2 = 3; arg5.value = 5; arg5.valuePointer = &arg5.value;
cout << "Arguments are done ";
pthread_create(&th0, NULL, jaccard_visit, (void*) &arg0);
pthread_create(&th1, NULL, jaccard_visit, (void*) &arg1);
pthread_create(&th2, NULL, jaccard_visit, (void*) &arg2);
pthread_create(&th3, NULL, jaccard_visit, (void*) &arg3);
pthread_create(&th4, NULL, jaccard_visit, (void*) &arg4);
pthread_create(&th5, NULL, jaccard_visit, (void*) &arg5);
cout << "Threads are created" << endl;
cout << "Join starts here" << endl;
pthread_join(th0, NULL);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
pthread_join(th3, NULL);
pthread_join(th4, NULL);
pthread_join(th5, NULL);
cout << "Fibonaccis: " <<endl;
cout << *(arg0.valuePointer) << endl;
cout << *(arg1.valuePointer) << endl;
cout << *(arg2.valuePointer) << endl;
cout << *(arg3.valuePointer) << endl;
cout << *(arg4.valuePointer) << endl;
cout << *(arg5.valuePointer) << endl;
}
int main(int argc, const char * argv[]){
cout << "Jaccard process is started"<<endl;
mcsim_skip_instrs_begin();
payload_text_initialize();
mcsim_skip_instrs_end();
cout << "Parallel part begins"<< endl;
manual_schedule();
cout << "Calculated results are being logged"<<endl;
for (int i = 0 ; i < NUM_OF_VERTICES ; i++){
for (int j = 0 ; j < NUM_OF_VERTICES ; j++){
cout << indices[i][j] << " ";
}
cout << endl;
}
}
void payload_text_initialize(){
payload_texts[0] = "l5IC5uC9AzcROkE3YkDJ2lEzLts8XP8a9WqDgDLWjg1M7HysAUfDFwzLWjc7875PnZVUHLzi6nQaUMQDNUeG4Wn2UkiOB79tOlE1t6LaKYbYiCJwJ34CAOFZCIbFSmcLTAAoB1rvPfeA6oM3kV3C8BDvraGvXjUORLGFAcBRQCerb3WD0qhrrM0MVW0t93bBqlTsrkxg";
payload_texts[1] = "tILKwAhbUkoqouKZ1G1VrZRmKwQnwzBgQirLkdedsYIAplKdEfk8oSmqdJmCJd5g0Q3VcJ8RYoxtIwA7jL1L01DcagIOuld0whcyM0yvSP0pMWO2yVTwOQPGkW2k7AHqzSEvb5BWkKsTexBsCUepjbG50T6vKsEHXGJ9aZwn2274Ekhnu1hlvuTqsS8jgwr0kQwhbwxN";
payload_texts[2] = "LNyQgx3mox3szmRNn1tSB4ibVuLsTr7MfANlj41Y0hKStx3NJx1O52XxNiqTMDCu4eGwWYcBvFMEC5tl1E7Rsm0Q9NZsPAJIwuiPYQuXeUyhMmbFiwRk6PlziXne0QaFJ3TrncsHsL3LxIDyaDPScSRdEvX72IJmi2gQTHgASi0KkKH4Sr6VJV3FjdNjKwY2ncT5oSXZ";
payload_texts[3] = "UxynTAvEWF4CcY9wUJRFnrX7sgrvvubcXUqH5DXK12UjSHDUME397S3BdB38FeMQJq8r7P7RILAY0qkw7OxUhGsZHRPmuY7VwKULqb6fx0Oy2McW2u07yqdAEMCN6AkQ1jTn2sXB4uWH21uLbjCf9i2V7W9tyw3cx6piE7XJb3vfbLI34OG5LKQXmVAGT0D6nbibaN8M";
}
execute_parallel()
包含 2 个用于创建和加入 pthread 的 for 循环。 manual_schedule()
具有相同代码的展开版本。在 PIN 上执行时,两者都运行良好,直到第一个线程的 join 函数。当 join 到来时,它会卡住并永远保持这样,没有任何信号或错误。在带有 -lpthread 标志的 Ubuntu 上执行时,它运行完美并生成结果。
在这种情况下,实现 pthread 最安全、最合适的方法是什么?
提前致谢
编辑
我注意到程序在读取 payload_texts[args->index1]
之前卡住。添加互斥量有助于在这一点上进行。它也正常工作了一次。它现在是不确定的,在同一二进制文件的多次执行中,它很少能正确完成。我认为 jaccard_visit 函数内的死锁应该是有原因的。我将其更改如下:
void * jaccard_visit(void *arg){
thread_args * args = (thread_args *) arg;
set<char> setunion;
set<char> intersect;
int id = args->index1 * 10 + args->index2;
pthread_mutex_lock(&cout_mutex); cout << "Thread "<< id << " started with indices: " << args->index1 << " " << args->index2 << endl; pthread_mutex_unlock(&cout_mutex);
pthread_mutex_lock(&payload_mutex);
set<char> set1 = find_uniques(payload_texts[args->index1]);
set<char> set2 = find_uniques(payload_texts[args->index2]);
pthread_mutex_unlock(&payload_mutex);
pthread_mutex_lock(&cout_mutex); cout << id << " : payload_texts were read" << endl; pthread_mutex_unlock(&cout_mutex);
pthread_mutex_lock(&cout_mutex); cout << id << " : intersect was created, scan begins" << endl; pthread_mutex_unlock(&cout_mutex);
for (set<char>::iterator i = set1.begin(); i != set1.end(); i++) {
char c1 = *i;
for (set<char>::iterator j = set2.begin(); j != set2.end(); j++) {
char c2 = *j;
if (c1 == c2){
intersect.insert(c1);
pthread_mutex_lock(&cout_mutex); cout << id << " : char" << c1 << " was inserted to intersection" << endl; pthread_mutex_unlock(&cout_mutex);
break;
}
}
}
pthread_mutex_lock(&cout_mutex); cout << id << " : intersection is calculated" << endl; pthread_mutex_unlock(&cout_mutex);
for (set<char>::iterator i = set1.begin(); i != set1.end(); i++) {
setunion.insert(*i);
}
for (set<char>::iterator i = set2.begin(); i != set2.end(); i++) {
char c = *i;
bool exists = false;
for (set<char>::iterator j = set1.begin(); j != set1.end(); j++) {
if (c == *j)
exists = true;
}
if (exists == false)
setunion.insert(c);
}
pthread_mutex_lock(&cout_mutex); cout << id << " : union is calculated" << endl; pthread_mutex_unlock(&cout_mutex);
double similarity = ((double) intersect.size()) / ((double) setunion.size());
pthread_mutex_lock(&cout_mutex);
cout << id << " : similarity is calculated as " << similarity << endl;
pthread_mutex_unlock(&cout_mutex);
indices[args->index1][args->index2] = similarity;
indices[args->index2][args->index1] = similarity;
unsigned long a = 1;
unsigned long b = 1;
unsigned long c = a + b;
pthread_mutex_lock(&cout_mutex);
cout << id << " : fibonacci starts" << endl;
pthread_mutex_unlock(&cout_mutex);
for (int i = 3 ; i < 100000 * (similarity - 0.9) ; i++){
a = b;
b = c;
c = a + b;
}
*(args->valuePointer) = c;
//pthread_exit(args->valuePointer);
return NULL;
}
最佳答案
最后,我通过对每个线程执行的函数 (jaccard_visit) 进行以下修改使其工作:
以下代码运行良好:
void * jaccard_visit(void *arg){
thread_args * args = (thread_args *) arg;
int id = args->id;
pthread_mutex_lock(&cout_mutex); cout << id << " : thread started" << endl; pthread_mutex_unlock(&cout_mutex);
pthread_mutex_lock(&payload_mutex);
string str1 = my_graph[args->index1].payload_text;
string str2 = my_graph[args->index2].payload_text;
pthread_mutex_unlock(&payload_mutex);
pthread_mutex_lock(&cout_mutex); cout << id << " : payload texts are read" << endl; pthread_mutex_unlock(&cout_mutex);
int stringLength = str1.length() - 1;
for (int i = 0; i < stringLength; i++) {
for (int j = i + 1; j < stringLength;) {
if (str1[i] == str1[j])
str1[j] = str1[--stringLength];
else
j++;
}
}
string set1 = str1.substr(0, stringLength);
pthread_mutex_lock(&cout_mutex); cout << id << " : unique chars of first node were extracted" << endl; pthread_mutex_unlock(&cout_mutex);
stringLength = str2.length() - 1;
for (int i = 0; i < stringLength; i++) {
for (int j = i + 1; j < stringLength;) {
if (str2[i] == str2[j])
str2[j] = str2[--stringLength];
else
j++;
}
}
string set2 = str2.substr(0, stringLength);
pthread_mutex_lock(&cout_mutex); cout << id << " : unique chars of second node were extracted" << endl; pthread_mutex_unlock(&cout_mutex);
int intersection_index = 0;
int union_index = 0;
for (int i = 0 ; i < set1.length() ; i++){
bool exists_in_set2 = false;
for (int j = 0 ; j < set2.length() && exists_in_set2 == false; j++){
if (set1[i] == set2[j]) {
intersection_index ++;
exists_in_set2 = true;
}
}
if (!exists_in_set2) {
union_index ++;
}
}
union_index += set2.length();
pthread_mutex_lock(&cout_mutex); cout << id << " : set1={" << set1 << "}, set2={" << set2 << "}" << endl; pthread_mutex_unlock(&cout_mutex);
pthread_mutex_lock(&cout_mutex); cout << id << " : |n|=" << intersection_index << ", |u|=" << union_index << endl; pthread_mutex_unlock(&cout_mutex);
double similarity = ((double) intersection_index / union_index);
pthread_mutex_lock(&cout_mutex); cout<<id<<" : similarity is: " << similarity << endl; pthread_mutex_unlock(&cout_mutex);
pthread_mutex_lock(&indices_mutex);
my_graph[args->index1].jaccardList[args->index2] = similarity;
my_graph[args->index2].jaccardList[args->index1] = similarity;
pthread_mutex_unlock(&indices_mutex);
return NULL;
}
关于c++ - 在使用 PIN 的模拟器上对 Pthread 进行安全编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32056735/
我正在尝试打印 timeval 类型的值。实际上我可以打印它,但我收到以下警告: 该行有多个标记 格式“%ld”需要“long int”类型,但参数 2 的类型为“struct timeval” 程序
我正在编写自己的 unix 终端,但在执行命令时遇到问题: 首先,我获取用户输入并将其存储到缓冲区中,然后我将单词分开并将它们存储到我的 argv[] 数组中。IE命令是“firefox”以启动存储在
我是 CUDA 的新手。我有一个关于一个简单程序的问题,希望有人能注意到我的错误。 __global__ void ADD(float* A, float* B, float* C) { con
我有一个关于 C 语言 CGI 编程的一般性问题。 我使用嵌入式 Web 服务器来处理 Web 界面。为此,我在服务器中存储了一个 HTML 文件。在此 HTML 文件中包含 JavaScript 和
**摘要:**在代码的世界中,是存在很多艺术般的写法,这可能也是部分程序员追求编程这项事业的内在动力。 本文分享自华为云社区《【云驻共创】用4种代码中的艺术试图唤回你对编程的兴趣》,作者: break
我有一个函数,它的任务是在父对象中创建一个变量。我想要的是让函数在调用它的级别创建变量。 createVariable testFunc() [1] "test" > testFunc2() [1]
以下代码用于将多个连续的空格替换为1个空格。虽然我设法做到了,但我对花括号的使用感到困惑。 这个实际上运行良好: #include #include int main() { int ch, la
我正在尝试将文件写入磁盘,然后自动重新编译。不幸的是,某事似乎不起作用,我收到一条我还不明白的错误消息(我是 C 初学者 :-)。如果我手动编译生成的 hello.c,一切正常吗?! #include
如何将指针值传递给结构数组; 例如,在 txt 上我有这个: John Doe;xxxx@hotmail.com;214425532; 我的代码: typedef struct Person{
我尝试编写一些代码来检索 objectID,结果是 2B-06-01-04-01-82-31-01-03-01-01 . 这个值不正确吗? // Send a SysObjectId SNMP req
您好,提前感谢您的帮助, (请注意评论部分以获得更多见解:即,以下示例中的成本列已添加到此问题中;西蒙提供了一个很好的答案,但成本列本身并未出现在他的数据响应中,尽管他提供的功能与成本列一起使用) 我
我想知道是否有人能够提出一些解决非线性优化问题的软件包的方法,而非线性优化问题可以为优化解决方案提供整数变量?问题是使具有相等约束的函数最小化,该函数受某些上下边界约束的约束。 我已经在R中使用了'n
我是 R 编程的初学者,正在尝试向具有 50 列的矩阵添加一个额外的列。这个新列将是该行中前 10 个值的平均值。 randomMatrix <- generateMatrix(1,5000,100,
我在《K&R II C 编程 ANSI C》一书中读到,“>>”和“0; nwords--) sum += *buf++; sum = (sum >>
当下拉列表的选择发生变化时,我想: 1) 通过 div 在整个网站上显示一些 GUI 阻止覆盖 2)然后处理一些代码 3) 然后隐藏叠加层。 问题是,当我在事件监听器函数中编写此逻辑时,将执行 onC
我正在使用 Clojure 和 RESTEasy 设计 JAX-RS REST 服务器. 据我了解,用 Lisp 系列语言编写的应用程序比用“传统”命令式语言编写的应用程序更多地构建为“特定于领域的语
我目前正在研究一种替代出勤监控系统作为一项举措。目前,我设计的用户表单如下所示: Time Stamp Userform 它的工作原理如下: 员工将选择他/她将使用的时间戳类型:开始时间、超时、第一次
我是一名学生,试图自学编程,从在线资源和像您这样的人那里获得帮助。我在网上找到了一个练习来创建一个小程序来执行此操作: 编写一个程序,读取数字 a 和 b(长整型)并列出 a 和 b 之间有多少个数字
我正在尝试编写一个 shell 程序,给定一个参数,打印程序的名称和参数中的每个奇数词(即,不是偶数词)。但是,我没有得到预期的结果。在跟踪我的程序时,我注意到,尽管奇数词(例如,第 5 个词,5 %
只是想知道是否有任何 Java API 可以让您控制台式机/笔记本电脑外壳上的 LED? 或者,如果不可能,是否有可能? 最佳答案 如果你说的是前面的 LED 指示电源状态和 HDD 繁忙状态,恐怕没
我是一名优秀的程序员,十分优秀!