- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在尝试创建嵌套线程时,helgrind 报告了几种不同类型的数据争用。
==4429== Possible data race during write of size 8 at 0x5673830 by thread #13
==4429== Locks held: none
==4429== at 0x4C379EF: memset (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x5060C85: get_cached_stack (allocatestack.c:250)
==4429== by 0x5060C85: allocate_stack (allocatestack.c:501)
==4429== by 0x5060C85: pthread_create@@GLIBC_2.2.5 (pthread_create.c:537)
==4429== by 0x4C32BF7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x4022D7: read_group (c_esp.c:318)
==4429== by 0x4C32DF6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x50606A9: start_thread (pthread_create.c:333)
==4429== Address 0x5673830 is 16 bytes inside a block of size 560 alloc'd
==4429== at 0x4C2EFB5: calloc (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x40134C4: allocate_dtv (dl-tls.c:322)
==4429== by 0x40134C4: _dl_allocate_tls (dl-tls.c:544)
==4429== by 0x50610D2: allocate_stack (allocatestack.c:588)
==4429== by 0x50610D2: pthread_create@@GLIBC_2.2.5 (pthread_create.c:537)
==4429== by 0x4C32BF7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x4022D7: read_group (c_esp.c:318)
==4429== by 0x4C32DF6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x50606A9: start_thread (pthread_create.c:333)
==4429== Block was alloc'd by thread #3
==4429== Possible data race during write of size 1 at 0x724368F by thread #13
==4429== Locks held: none
==4429== at 0x4C3856C: mempcpy (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x40132F6: _dl_allocate_tls_init (dl-tls.c:520)
==4429== by 0x5060C8D: get_cached_stack (allocatestack.c:253)
==4429== by 0x5060C8D: allocate_stack (allocatestack.c:501)
==4429== by 0x5060C8D: pthread_create@@GLIBC_2.2.5 (pthread_create.c:537)
==4429== by 0x4C32BF7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x4022D7: read_group (c_esp.c:318)
==4429== by 0x4C32DF6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x50606A9: start_thread (pthread_create.c:333)
==4429==
==4429== This conflicts with a previous write of size 1 by thread #4
==4429== Locks held: none
==4429== at 0x5060612: start_thread (pthread_create.c:265)
==4429== Address 0x724368f is in a rw- anonymous segment
==4429== Possible data race during read of size 8 at 0x7243728 by thread #14
==4429== Locks held: none
==4429== at 0x40178C: read_record (c_esp.c:171)
==4429== by 0x4C32DF6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x50606A9: start_thread (pthread_create.c:333)
==4429==
==4429== This conflicts with a previous write of size 8 by thread #13
==4429== Locks held: none
==4429== at 0x5060DB7: pthread_create@@GLIBC_2.2.5 (pthread_create.c:589)
==4429== by 0x4C32BF7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x4022D7: read_group (c_esp.c:318)
==4429== by 0x4C32DF6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==4429== by 0x50606A9: start_thread (pthread_create.c:333)
==4429== Address 0x7243728 is in a rw- anonymous segment
提到的行是:
318: pthread_create(pth_array + k, &attrs, read_record, args_array +k);
171: void *read_record(void *_args){
所以看起来在创建线程时存在数据竞争?为“不同分支”中的线程分配的堆栈是否可能重叠?还是我搞砸了什么地方?
我已经尝试减少每个线程的堆栈大小,但没有成功。
根据要求,一个最小的可重现示例:
#include <pthread.h>
void *func_b(void *args){
return args;
}
void *func_a(void *args){
pthread_t pth[5];
int i;
for(i = 0; i < 5; i++){
pthread_create(&pth[i], NULL, func_b, NULL);
}
for(i = 0; i < 5; i++){
pthread_join(pth[i], NULL);
}
return args;
}
int main(void){
pthread_t pth[100];
int i;
for(i = 0; i < 100; i++){
pthread_create(&pth[i], NULL, func_a, NULL);
}
for(i = 0; i < 100; i++){
pthread_join(pth[i], NULL);
}
}
helgrind 的完整输出是 here接下来是所有相关代码。此片段从第 171 行到第 477 行。
void *read_record(void *_args){
struct _thread_args *args = (struct _thread_args *)_args;
uint32_t data_size = 0;
args->item->record->type = calloc(5, sizeof *args->item->record->type);
memcpy(args->item->record->type, *args->map, 4);
*args->map += 4;
memcpy(&data_size, *args->map, 4);
*args->map += 4;
memcpy(&args->item->record->flags, *args->map, 4);
*args->map += 4;
memcpy(&args->item->record->id, *args->map, 4);
*args->map += 4;
memcpy(&args->item->record->revision, *args->map, 4);
*args->map += 4;
memcpy(&args->item->record->version, *args->map, 2);
*args->map += 2;
memcpy(&args->item->record->unknown, *args->map, 2);
*args->map += 2;
args->item->record->misc_data = NULL;
args->item->record->children = NULL;
args->item->record->last = NULL;
// args->item->record->next = NULL;
// args->item->record->previous = NULL;
args->item->record->_proxy = NULL;
args->item->record->compression_level = 0x7f; // some invalid number, doesn't matter
if(args->parse_records){
args->item->record->is_parsed = TRUE;
if(args->item->record->flags & 0x00040000){
uLongf uncompressed_size = 0;
memcpy(&uncompressed_size, *args->map, 4);
*args->map += 4;
if(*(*args->map + 1) & (1 << 6)){
if(*(*args->map + 1) & (1 << 7)){
args->item->record->compression_level = Z_BEST_COMPRESSION; // both hight bits are set 11
} else{
args->item->record->compression_level = Z_BEST_SPEED; // 01
}
} else{
if(*(*args->map + 1) & (1 << 7)){
args->item->record->compression_level = Z_DEFAULT_COMPRESSION; // 10
} else{
args->item->record->compression_level = Z_NO_COMPRESSION; // 00
}
}
uint8_t *uncompressed_data = malloc(uncompressed_size * sizeof *uncompressed_data);
uint8_t *start = uncompressed_data;
uncompress(uncompressed_data, &uncompressed_size, *args->map, data_size - 4);
parse_record_data((uint8_t **)&uncompressed_data, uncompressed_size, &args->item->record->children, &args->item->record->last);
*args->map += data_size - 4;
free(start);
} else{
parse_record_data(args->map, data_size, &args->item->record->children, &args->item->record->last);
}
} else{
args->item->record->is_parsed = FALSE;
args->item->record->misc_data = malloc(sizeof *args->item->record->misc_data);
args->item->record->misc_data->data = malloc(data_size * sizeof *args->item->record->misc_data->data);
args->item->record->misc_data->data_size = data_size;
memcpy(args->item->record->misc_data->data, *args->map, data_size);
*args->map += data_size;
}
return NULL;
}
void *read_group(void *_args){
struct _thread_args *args = (struct _thread_args *)_args;
uint32_t data_size = 0;
args->item->group->type = "GRUP";
*args->map += 4;
memcpy(&data_size, *args->map, 4);
*args->map += 4;
memset(args->item->group->label, 0, 5);
memcpy(args->item->group->label, *args->map, 4);
*args->map += 4;
memcpy(&args->item->group->group_type, *args->map, 4);
*args->map += 4;
memcpy(&args->item->group->stamp, *args->map, 2);
*args->map += 2;
memcpy(&args->item->group->unknown1, *args->map, 2);
*args->map += 2;
memcpy(&args->item->group->version, *args->map, 2);
*args->map += 2;
memcpy(&args->item->group->unknown2, *args->map, 2);
*args->map += 2;
args->item->group->children = NULL;
args->item->group->last = NULL;
args->item->group->_proxy = NULL;
uint8_t *data_start = *args->map;
int item_count = 0;
while(*args->map < (data_start + data_size - 24)){
item_count++;
char next_type[5] = {0};
memcpy(next_type, *args->map, 4);
uint32_t next_data_size = 0;
memcpy(&next_data_size, *args->map + 4, 4);
*args->map += next_data_size;
if(strcmp(next_type, "GRUP") != 0){
*args->map += 24;
}
}
*args->map = data_start;
pthread_attr_t attrs;
pthread_attr_init(&attrs);
pthread_attr_setstacksize(&attrs, 1000);
struct _group_item *current = NULL;
bool current_is_record = FALSE;
pthread_t *pth_array = malloc(item_count * sizeof *pth_array);
uint8_t **map_array = malloc(item_count * sizeof *map_array);
struct _thread_args *args_array = malloc(item_count * sizeof *args_array);
int k;
for(k = 0; k < item_count; k++){
char next_type[5] = {0};
memcpy(next_type, *args->map, 4);
uint32_t new_data_size = 0;
memcpy(&new_data_size, *args->map + 4, 4);
*(map_array + k) = *args->map;
*args->map += new_data_size;
(*(args_array + k)).map = map_array + k;
(*(args_array + k)).parse_records = args->parse_records;
(*(args_array + k)).item = malloc(sizeof *(*(args_array + k)).item);
if(strcmp(next_type, "GRUP") == 0){
(*(args_array + k)).is_record = FALSE;
(*(args_array + k)).item->group = malloc(sizeof *(*(args_array + k)).item->group);
pthread_create(pth_array + k, &attrs, read_group, args_array + k);
//read_group(args_array + k);
} else{
*args->map += 24;
(*(args_array + k)).is_record = TRUE;
(*(args_array + k)).item->record = malloc(sizeof *(*(args_array + k)).item->record);
pthread_create(pth_array + k, &attrs, read_record, args_array + k);
//read_record(args_array + k);
}
if(args->item->group->children == NULL){
args->item->group->children = current = (*(args_array + k)).item;
args->item->group->children_is_record = current_is_record = (*(args_array + k)).is_record;
if((*(args_array + k)).is_record){
current->record->previous = NULL;
} else{
current->group->previous = NULL;
}
continue;
}
if((*(args_array + k)).is_record){
if(current_is_record){
current->record->next = (*(args_array + k)).item;
current->record->next_is_record = (*(args_array + k)).is_record;
(*(args_array + k)).item->record->previous = current;
(*(args_array + k)).item->record->previous_is_record = current_is_record;
} else{
current->group->next = (*(args_array + k)).item;
current->group->next_is_record = (*(args_array + k)).is_record;
(*(args_array + k)).item->record->previous = current;
(*(args_array + k)).item->record->previous_is_record = current_is_record;
}
} else{
if(current_is_record){
current->record->next = (*(args_array + k)).item;
current->record->next_is_record = (*(args_array + k)).is_record;
(*(args_array + k)).item->group->previous = current;
(*(args_array + k)).item->group->previous_is_record = current_is_record;
} else{
current->group->next = (*(args_array + k)).item;
current->group->next_is_record = (*(args_array + k)).is_record;
(*(args_array + k)).item->group->previous = current;
(*(args_array + k)).item->group->previous_is_record = current_is_record;
}
}
current = (*(args_array + k)).item;
current_is_record = (*(args_array + k)).is_record;
}
args->item->group->last = current;
args->item->group->last_is_record = current_is_record;
if(current != NULL){
if(current_is_record){
current->record->next = NULL;
} else{
current->group->next = NULL;
}
}
int i;
for(i = 0; i < item_count; i++){
pthread_join(*(pth_array + i), NULL);
}
free(pth_array);
free(map_array);
free(args_array);
assert(*args->map == (data_start + data_size - 24));
return NULL;
}
Plugin *plugin_read(const char *filename, const bool parse_records){
Plugin *plugin = malloc(sizeof *plugin);
plugin->children = NULL;
plugin->last = NULL;
plugin->_proxy = NULL;
FILE *fileobject = fopen(filename, "rb");
if(fileobject == NULL){
printf("No file found.\n");
return NULL;
}
fseek(fileobject, 0, SEEK_END);
long int filesize = ftell(fileobject);
rewind(fileobject);
uint8_t *map = malloc(filesize * sizeof *map);
fread(map, filesize, 1, fileobject);
fclose(fileobject);
uint8_t *start = map;
pthread_attr_t attrs;
pthread_attr_init(&attrs);
pthread_attr_setstacksize(&attrs, 1000);
pthread_t header_pth;
plugin->header = malloc(sizeof *plugin->header);
struct _group_item header_item;
header_item.record = plugin->header;
uint8_t *header_map = map;
struct _thread_args header_args;
header_args.item = &header_item;
header_args.map = &header_map;
header_args.parse_records = parse_records;
pthread_create(&header_pth, &attrs, read_record, &header_args);
uint32_t header_data_size = 0;
memcpy(&header_data_size, map + 4, 4);
map += header_data_size + 24;
uint8_t *after_header = map;
int group_count = 0;
while(map < (start + filesize)){
group_count++;
uint32_t next_data_size = 0;
memcpy(&next_data_size, map + 4, 4);
map += next_data_size;
}
map = after_header;
pthread_t *pth = malloc(group_count * sizeof *pth);
uint8_t **map_array = malloc(group_count * sizeof *map_array);
struct _thread_args *args = malloc(group_count * sizeof *args);
struct _group_item *current = NULL;
int k;
for(k = 0; k < group_count; k++){
uint32_t new_data_size = 0;
memcpy(&new_data_size, map + 4, 4);
*(map_array + k) = map;
map += new_data_size;
(*(args + k)).item = malloc(sizeof *(*(args + k)).item);
(*(args + k)).item->group = malloc(sizeof *(*(args + k)).item->group);
(*(args + k)).map = map_array + k;
(*(args + k)).parse_records = parse_records;
pthread_create(pth + k, &attrs, read_group, args + k);
if(plugin->children == NULL){
plugin->children = (*(args + k)).item;
current = (*(args + k)).item;
current->group->previous = NULL;
continue;
}
current->group->next = (*(args + k)).item;
current->group->next_is_record = FALSE;
(*(args + k)).item->group->previous = current;
(*(args + k)).item->group->previous_is_record = FALSE;
current = (*(args + k)).item;
}
plugin->last = current;
plugin->last->group->next = NULL;
pthread_join(header_pth, NULL);
int i;
for(i = 0; i < group_count; i++){
pthread_join(*(pth + i), NULL);
}
assert(map == (start + filesize));
free(start);
free(pth);
free(args);
free(map_array);
return plugin;
}
谢谢!
最佳答案
尝试 --sim-hints=no-nptl-pthread-stackcache。
参见 http://www.valgrind.org/docs/manual/manual-core.html#manual-core.rareopts获取更多信息。
关于c - 嵌套线程创建期间的数据竞争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40113375/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!