标签: pthreads

如何使用用户输入创建全局变量

我希望创建以下内容:

int amount[i];
Run Code Online (Sandbox Code Playgroud)

作为全局变量(练习使用线程和互斥体),但该变量i是在程序启动时定义的:

./a.out 10
Run Code Online (Sandbox Code Playgroud)

我如何通过 main ( argv[1]) 获取值并相应地创建全局?

c global pthreads

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

互斥锁上的随机错误

互斥锁会随机成功或失败,失败时会出现以下情况:

Invalid argument
Run Code Online (Sandbox Code Playgroud)

或者

tpp.c:62: __pthread_tpp_change_priority: Assertion `new_prio == -1 || (new_prio >= __sched_fifo_min_prio && new_prio <= __sched_fifo_max_prio)' failed.
Run Code Online (Sandbox Code Playgroud)

该代码非常基本,您可以在此处看到:

pthread_mutex_t mutex;
main() {
  int ret;
  pthread_mutexattr_t attr;
  pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
  ret = pthread_mutex_init(&mutex, &attr);
  if (ret != 0) {
    printf("pthread_mutex_init\n");
    return 1;
  }
  ret = pthread_mutex_lock(&mutex);
  if (ret != 0) {
    printf("mutex_lock failed %s\n", strerror(ret));
    return 1;
  }
  ret = pthread_mutex_unlock(&mutex);
  if (ret != 0) {
    printf("mutex_unlock failed %s\n", strerror(ret));
    return -1;
  }
Run Code Online (Sandbox Code Playgroud)

这是为什么 ?

c mutex pthreads

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

传递给 pthread_create 的例程何时开始?

给出以下代码

#include <pthread.h>

void *pt_routine(void *arg)
{
    pthread_t *tid;
    tid = (pthread_t *) arg;
    /* do something with tid , say printf?*/
    /*
    printf("The thread ID is %lu\n", *tid);
    */
    return NULL;
}

int main(int argc, char **argv)
{
    int rc;
    pthread_t tid;
    rc = pthread_create(&tid, NULL, pt_routine, &tid);
    if (rc)
    {
        return 1;
    }
    printf("The new thread is %lu\n", tid);
    pthread_join(tid, NULL);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

例行公事能永远正确吗tid

当然,我可以使用 pthread 来获取自身 ID,但我只是想知道例程何时运行。

c posix pthreads

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

pthread_create 没有单独的函数

我有一个 s 数组pthread_t,它们是通过 的 for 循环启动的pthread_create

我有大量预先声明的变量,它们对于线程的内部工作很重要。我想要一个匿名内部函数作为启动例程,如下所示pthread_create

pthread_create(threads[i], NULL, 

     { inner function here }

, NULL);
Run Code Online (Sandbox Code Playgroud)

我知道 C++ 没有这个特别的功能,所以我想也许 lambda 可能会有所帮助,或者也许有人有另一个想法,这样我就不必创建一个单独的方法并移交之前的所有变量pthread_create

c++ pthreads

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

使用system()创建独立的子进程

我编写了一个程序,在主程序中创建一个线程,并用于system()从该线程启动另一个进程。我也使用主函数中的 来启动相同的过程system()。即使父进程死亡,从线程启动的进程似乎仍保持活动状态。但是从 main 函数调用的函数会随着父函数一起死亡。任何想法为什么会发生这种情况。

请找到下面的代码结构:

void *thread_func(void *arg)
{
     system(command.c_str());        
}

int main()
{
    pthread_create(&thread_id, NULL, thread_func, NULL);
    .... 
    system(command.c_str());
    while (true)
    {
        ....
    }
    pthread_join(thread_id, NULL);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading fork pthreads system-calls

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

为什么 Helgrind 显示“违反锁定顺序”错误消息?

请看下面的代码

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

    pthread_mutex_t g = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;

    void* worker(void* arg) 
    {
        pthread_mutex_lock(&g);

        if ((long long) arg == 0) {
        pthread_mutex_lock(&m1);
        pthread_mutex_lock(&m2);
        } else {
        pthread_mutex_lock(&m2);
        pthread_mutex_lock(&m1);
        }
        pthread_mutex_unlock(&m1);
        pthread_mutex_unlock(&m2);

        pthread_mutex_unlock(&g);
        return NULL;
    }

    int main(int argc, char *argv[]) {
        pthread_t p1, p2;
        pthread_create(&p1, NULL, worker, (void *) (long long) 0);
        pthread_create(&p2, NULL, worker, (void *) (long long) 1);
        pthread_join(p1, NULL);
        pthread_join(p2, NULL);
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

Helgrind …

c multithreading valgrind deadlock pthreads

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

在 Swift 中移动互斥锁安全吗?

我正在对互斥锁进行一些研究,并发现了以下 Swift 代码:

class Lock {

    private var mutex: pthread_mutex_t = {
        var mutex = pthread_mutex_t()
        pthread_mutex_init(&mutex, nil)
        return mutex
    }()

    func someFunc() {
        pthread_mutex_lock(&mutex)
        defer { pthread_mutex_unlock(&mutex) }
        ...
    }

}
Run Code Online (Sandbox Code Playgroud)

该代码在闭包内定义并初始化 pthread_mutex_t,然后将返回值分配给类属性。然后,它会在多个功能内进行锁定和解锁,如图所示。

由于还应该调用 pthread_mutex_destroy,这意味着互斥体中正在发生某种分配,该互斥体可能会或可能不会引用原始值的地址。

实际上,互斥量在一个位置初始化并存储在另一个位置。

问题是这样做是否安全或正确?

如果互斥体初始值设定项需要参数怎么办?

    private var mutex: pthread_mutex_t = {
        var recursiveMutex = pthread_mutex_t()
        var recursiveMutexAttr = pthread_mutexattr_t()
        pthread_mutexattr_init(&recursiveMutexAttr)
        pthread_mutexattr_settype(&recursiveMutexAttr, PTHREAD_MUTEX_RECURSIVE)
        pthread_mutex_init(&recursiveMutex, &recursiveMutexAttr)
        return recursiveMutex
     }()
Run Code Online (Sandbox Code Playgroud)

后者让我觉得绝对是不正确的,因为当闭包崩溃时,其地址传递到互斥体的属性存储将消失。

concurrency pthreads swift

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

pthread_create() 似乎泄漏内存

我写了一个简单的wrapper.soovercalloc()free()来监视内存调用,并且出现pthreads_create()内存泄漏。

在使用 进行初始分配后calloc(17, 16)(大多数情况下calloc(18, 16)),似乎正在尝试释放内存,但 anullptr被传递给了free()

这里发生了什么事?

// test.cpp

#include <pthread.h>
#include <cassert>
#include <cstdlib>

void* p_dummy(void*)
{
    return nullptr;
}

int main(void)
{
    void* ptr = calloc(11, 11);
    free(ptr);

    pthread_t thread;
    assert(pthread_create(&thread, nullptr, p_dummy, nullptr) == 0); 
    assert(pthread_join(thread, nullptr) == 0);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)
// wrapper.cpp

#ifndef _GNU_SOURCE
    #define _GNU_SOURCE
#endif

#include <dlfcn.h>
#include <cstdio>

static void  (*real_free)(void* ptr)                   = NULL;
static void* …
Run Code Online (Sandbox Code Playgroud)

c++ linux pthreads

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

如何修复 pthread_create 调用中的分段错误

我当前的代码有问题。我正在开发一个项目,在该项目中,我使用线程从终端读取一组文件,并了解单个文件组和总文件组中有多少行。我的问题是,当我运行代码时,我得到一个核心转储,当我通过 gdb 运行代码时,我在 pthread_create 调用中得到分段错误。是因为我的实现还是因为我的代码中的其他内容?

#define NUM_THREADS 12
struct thread_data{
    char *thread_id;
    int count;
};

struct thread_data thread_data_array[NUM_THREADS];

void* filecount(void * thread_arg){
    char thread_id;
    int count;
    struct thread_data *thread;

    thread = (struct thread_data *) thread_arg;
    thread_id = *thread->thread_id;
    count = thread->count;

    FILE *fp = fopen(&thread_id, "r");
    if (fp == NULL) {
        fprintf(stderr, "Cannot open %s\n", thread_id);
        exit(-1);
    }

    for (char c = getc(fp); c != EOF; c = getc(fp))
        if (c == '\n')
            count++;

    fclose(fp);
    pthread_exit(NULL);
}

int main(int argc, …
Run Code Online (Sandbox Code Playgroud)

c pthreads

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

g++ 和 gcc 在 pthread_cleanup_push/pop 上的行为不同

这是我的代码

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

void cleanup(void *arg) {
    printf("cleanup: %s\n", (const char*)arg);
}

void *thr_fn1(void *arg) {
    printf("thread 1 strat\n");
    pthread_cleanup_push(cleanup, (void*)"thread 1 first handler");
    pthread_cleanup_push(cleanup, (void*)"thread 1 first handler");

    if(arg)
        return (void*)1;

    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);

    return (void*)1;
}

void *thr_fn2(void *arg) {
    printf("thread 2 strat\n");
    pthread_cleanup_push(cleanup, (void*)"thread 2 first handler");
    pthread_cleanup_push(cleanup, (void*)"thread 2 first handler");

    if(arg)
        return (void*)2;

    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);

    return (void*)2;
}

int main() {
    int err;
    pthread_t tid1, tid2;
    void *tret;

    pthread_create(&tid1, NULL, thr_fn1, (void*)1);
    pthread_create(&tid2, NULL, …
Run Code Online (Sandbox Code Playgroud)

c c++ gcc g++ pthreads

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

标签 统计

pthreads ×10

c ×6

c++ ×4

multithreading ×2

concurrency ×1

deadlock ×1

fork ×1

g++ ×1

gcc ×1

global ×1

linux ×1

mutex ×1

posix ×1

swift ×1

system-calls ×1

valgrind ×1