标签: pthreads

错误:无法使用'void'类型的左值初始化'int'类型的变量

我正在尝试pthread示例.这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

void*AsalK1(void *gelen);

int main(){

    int *i;
    i= new int;
    *i=1;

    int sonSayi;
    pthread_t th1, th2, th3, th4;
    printf("---------------------------------------------\n");
    printf("|       Threadler ile Asal Sayi Bulma       |\n");
    printf("---------------------------------------------\n");
    printf("Son sayi degeri: 1000000 \n");

    int r1=pthread_create( &th1, NULL, AsalK1, (void *)i);
    *i=3;
    int r2=pthread_create( &th2, NULL, AsalK1, (void *)i);
    *i=5;
    int r3=pthread_create( &th3, NULL, AsalK1, (void *)i);
    *i=7;
    int r4=pthread_create( &th4, NULL, AsalK1, (void *)i);

    pthread_join( th1, NULL);
    pthread_join( th2, NULL);
    pthread_join( th3, NULL);
    pthread_join( th4, NULL);
    return 0; …
Run Code Online (Sandbox Code Playgroud)

c pthreads

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

pthread_create:无法分配内存

我写了一个小的tcp服务器,它为每个传入的连接创建一个新的线程:

while (server_running)
{
  client_sock = accept(server_sock,
                   (struct sockaddr *)&client_name,
                   &client_name_len);

  if(!server_running)
    break;
  if (client_sock == -1)
    continue;
  /* accept_request(client_sock); */
  if (pthread_create(&newthread , NULL, &accept_request, &client_sock) != 0)
    perror("pthread_create");
}
Run Code Online (Sandbox Code Playgroud)

大约380个成功连接后,出现错误消息

'pthread_create:无法分配内存'

每次新的连接尝试都会发生.我真的不知道这来自哪里,因为accept_request运行正常.我还注意到TIME_WAIT在运行期间有很多与状态的连接(我用过netstat这个).那么哪里出错了?

c sockets linux pthreads

0
推荐指数
1
解决办法
1995
查看次数

我的文件可以编译,但是当我尝试运行它时,我得到"没有这样的文件或目录"

我认为问题在于我没有使用./a.out而不是./filename,但事实并非如此.

这是我编译程序的方式:

g++ -o -Wall -pthread filename.cpp
Run Code Online (Sandbox Code Playgroud)

运行:

./filename
Run Code Online (Sandbox Code Playgroud)

我认为运行pthread程序与运行标准c ++程序不同,但事实并非如此.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> 

struct argStruct {
    int arg1;
    int arg2;

};

void *sum(void *arguments) {
    struct argStruct *args = (struct argStruct *)arguments;

    int a =  args -> arg1;
    int b =  args -> arg2;
    int c = a + b;
    printf("%d + %d = %d ",a,b,c);
    pthread_exit(NULL);

}

int main() {

    pthread_t thr1, thr2;
    struct argStruct args;
    args.arg1 = 3;
    args.arg2 = 10;

    int …
Run Code Online (Sandbox Code Playgroud)

c++ pthreads

0
推荐指数
1
解决办法
597
查看次数

多线程在速度上没有任何改进 - 在C中使用pthread - 为什么?

为了在多线程中变得更加舒适,我编写了一个带有"密集"计算的小程序.它是mandelbrot集的图片,其中每个像素单独计算,然后像素缓冲到行.每个线程获得的总行数相等.因此,例如,如果选择的线程数是2,则具有1000行高度计算的图片应该以两个500行包结束.因此我建议速度种类减少两倍,但没有改善.为什么???我不明白,因为一切正常并且看似合乎逻辑.如果有人能给我一个提示,我将非常感激.下面你会看到main和一个由main调用的mandelbrot集的caluclation函数.

int main(int argc, char ** argv, char ** envp) {

if(argc != 4)
{
printf("Bitte genau 3 Argumente eingeben.\n");
 return 1;
}
//Structs und Variablen für die Stopuhr
struct timeval start, ende;
long ttlende, ttlstart;

width  = str2num(argv[1]);
height = str2num(argv[2]);

int y;
//char blueGreenRed[3];
//Ist Buffer für ganze Zeile: Breite * 3 wegen den 3 Bytes pro Pixel
//char zeile[width*3];

unsigned char info[BMPHEADER_SIZE] = {
              //size
    'B','M',  0,0,0,0, 0,0, 0,0, 54,0,0,0,
              //width  //height
    40,0,0,0, 0,0,0,0, 0,0,0,0,  1,0, 24,0, …
Run Code Online (Sandbox Code Playgroud)

c multithreading pthreads performance-testing

0
推荐指数
1
解决办法
430
查看次数

将std :: map对象传递给线程

我的代码试图传递一个std :: map作为线程的引用,但似乎有些不好并导致

error: invalid conversion from ‘void* (*)(std::map<std::basic_string<char>,
       std::vector<std::basic_string<char> > >)’ to ‘void* (*)(void*)’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)

我需要将map传递给线程并在该线程中插入map的键和值,并在成功之后.在主进程中,我需要更新或复制(线程映射)在同一映射的另一个对象,即myMapcache

int main()
{
std::map< std::pair<std::string , std::string> , std::vector<std::string> > myMap,myMapCache;

  pthread_t threads;

  //how to pass map object in thread as reference

  int rc = pthread_create(&threads, NULL, myfunction, std::ref(myMap)); 
  if (rc)
  {
     cout << "Error:unable to create thread," << rc << endl;
     exit(-1);
   }

   // if true then update to local myMapCache

   if(update)
    {
      std::copy(myMap.begin(), myMap.end(), std::inserter(MyMapCache, myMapCache.end()) );
    } 

}


void * …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading pthreads thread-safety

0
推荐指数
1
解决办法
413
查看次数

pthread_cond_timedwait立即返回ETIMEDOUT

我试图用pthread_cond_timedwait等待Java的超时等待wait(long timeout, int nanos).我知道Java wait使用相对超时,而pthread_cond_timedwait使用绝对时间阈值.尽管考虑到这一点,pthread_cond_timedwait似乎立即返回错误代码ETIMEDOUT.

下面的示例程序打印一个值<0.我希望它打印一个值> = 0.

我使用pthread_cond_timedwait不正确吗?如何重写以下程序以添加例如5秒的延迟?

#define _POSIX_C_SOURCE 199309L

#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <errno.h>

int main(void) {
    pthread_mutex_t mutex;
    pthread_cond_t cond;

    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);

    pthread_mutex_lock(&mutex);

    struct timespec t1;
    struct timespec t2;

    clock_gettime(CLOCK_MONOTONIC, &t1);

    t1.tv_sec += 5;

    while(pthread_cond_timedwait(&cond, &mutex, &t1) != ETIMEDOUT);

    clock_gettime(CLOCK_MONOTONIC, &t2);

    printf("%d", t2.tv_sec - t1.tv_sec);

    pthread_mutex_unlock(&mutex);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c c++ multithreading posix pthreads

0
推荐指数
1
解决办法
1087
查看次数

原子自由记忆

这是一个函数,我用来释放动态分配的内存

void Free(void* arg) {
    if(arg!=NULL) {
        free(arg);
        arg=NULL;
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止它工作正常,直到我开始使用pthreads.我的堆栈有时会被粉碎,我唯一能做的就是使用pthread_mutex_lock()
是否有任何原子公告来检查和释放gcc中的内存?
或者您可以提出另一种解决方案?我希望互斥体不是唯一的方法

c memory-management pthreads

0
推荐指数
1
解决办法
123
查看次数

服务器停止创建线程

我有一个非常简单的客户端 - 服务器代码.每次服务器收到数据包时,我都会创建一个处理它的线程.代码如下所示.我无法理解的是,有一段时间后我的服务器停止接收任何数据.它只是倾听并且没有收到任何东西.我无法弄清楚为什么.有谁知道原因.

我正在联想T470s上构建我的代码Fedora 29,Linux用户4.19.15-300.fc29.x86_64#1 SMP Mon 1月14日16:32:35 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

任何帮助表示赞赏.

/* 
  server.c
  cc -std=gnu11 -pedantic  -lpthread  server.c   -o server.c 
 */

#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BUF_SIZE_B 1024

static int fd;

static void *handlePacketThreadWrapper(void *arg);

int main(void)
{
    pthread_t t;
    struct pollfd pollfd;
    struct sockaddr_in addr;
    uint16_t port = 9500;

    fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    if (fd < …
Run Code Online (Sandbox Code Playgroud)

c sockets linux pthreads

0
推荐指数
1
解决办法
81
查看次数

为什么加入一个线程最终会调用多个线程?

我正在学习有关线程和Linux上C语言中的线程编程的知识。我了解的是,加入线程只是调用线程并等待其执行,就像等待子进程运行一样。但是不知道为什么在尝试加入一个线程时最终会调用两个线程!

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void *start1()
{
    printf("Hello from Thread 1\n");

}

void *start2()
{
    printf("Hello from Thread 2\n");
}

void main()
{
    pthread_t t1,t2;

    pthread_create(&t1,NULL,start1,NULL);
    pthread_create(&t2,NULL,start2,NULL);
    pthread_join(t1,NULL); 
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,输​​出如下:

[root@localhost]# ./a.out 
Hello from Thread 1
Hello from Thread 2
Run Code Online (Sandbox Code Playgroud)

我希望它仅调用start1的代码。

multithreading pthreads process

0
推荐指数
1
解决办法
55
查看次数

在信号处理程序中使用`pause()`有什么陷阱?

我想暂停线程并恢复它。有上市一些方法在这里。但是我想到了pause()从使用库函数unistd.h

在信号处理程序中使用暂停有哪些陷阱?

我注意到的一个是,当我发送0到暂停线程并0再次发送时,我的信号已排队。我需要发送1两次以恢复线程。

我猜可能还会有更多类似的情况。如果要使用信号处理程序pause()sleep()在信号处理程序中如何处理此类情况。

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>

static bool thread_ready = false;

static void cb_sig(int signal)
{
        if (signal == SIGUSR1)
                pause();
        else if (signal == SIGUSR2)
                ;
}

static void *thread_job(void *ignore)
{
        int i = 0;
        struct sigaction act;

        sigemptyset(&act.sa_mask);
        act.sa_flags = 0;
        act.sa_handler = cb_sig;

        if (sigaction(SIGUSR1, &act, NULL) == -1)
                printf("unable to handle …
Run Code Online (Sandbox Code Playgroud)

c pthreads signal-handling

0
推荐指数
1
解决办法
64
查看次数