相关疑难解决方法(0)

将int转换为void*或反之亦然意味着什么?

void*从内存的角度来看,将整数值转换为或者反之亦然?我的理解是void*一个未指定长度的内存块的地址.
这似乎就像比较苹果和橙子.

int myval = 5;
void* ptr = (void*)myval;
printf("%d",(int)ptr);
Run Code Online (Sandbox Code Playgroud)

我意识到我应该给出使用它的确切上下文.

int main(int argc, char* argv[]) {
long       thread;  /* Use long in case of a 64-bit system */
pthread_t* thread_handles; 

/* Get number of threads from command line */
if (argc != 2) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10);  
if (thread_count <= 0 || thread_count > MAX_THREADS) Usage(argv[0]);

thread_handles = malloc (thread_count*sizeof(pthread_t)); 

for (thread = 0; thread < thread_count; thread++)  
  pthread_create(&thread_handles[thread], NULL, Hello, (void*) thread); …
Run Code Online (Sandbox Code Playgroud)

c void

23
推荐指数
1
解决办法
2万
查看次数

在同时运行的线程中调用printf是否可以安全线程?

可能重复:
Linux上的C中的stdout线程安全吗?

假设thread1和thread2相似,并且在它们的作业结束时它们都是printf.它是线程安全还是必须以某种方式锁定printf?

它与stdout有关吗?如果在每个printf之后执行fflush(stdout)怎么办?它有什么改变吗?

c multithreading pthreads thread-safety

11
推荐指数
1
解决办法
2万
查看次数

pthread 和 printf 的 C 性能都很差

我正在使用大型数组测试 Linux 的 ac 代码以测量线程性能,当线程增加到最大内核(Intel 4770 为 8 个)时,应用程序可以很好地扩展,但这仅适用于我的代码的纯数学部分。

如果我为结果数组添加 printf 部分,那么即使重定向到文件,时间也会变得太大,从几秒到几分钟,而当 printf 这些数组应该只添加几秒时。

代码:

(gcc 7.5.0-Ubuntu 18.04)

没有 printf 循环:

gcc -O3 -m64 exp_multi.c -pthread -lm 
Run Code Online (Sandbox Code Playgroud)

使用 printf 循环:

gcc -DPRINT_ARRAY -O3 -m64 exp_multi.c -pthread -lm
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#define MAXSIZE 1000000
#define REIT 100000
#define XXX -5
#define num_threads 8
static double xv[MAXSIZE];
static double yv[MAXSIZE];

/*    gcc -O3 -m64 exp_multi.c -pthread -lm    */


void* run(void *received_Val){
    int single_val = *((int *) received_Val); …
Run Code Online (Sandbox Code Playgroud)

c performance multithreading pthreads

2
推荐指数
1
解决办法
790
查看次数

标签 统计

c ×3

multithreading ×2

pthreads ×2

performance ×1

thread-safety ×1

void ×1