标签: pthreads

pthread_mutex_lock() 和 pthread_mutex_trylock() 有什么区别

我正在使用并行编程编写应用程序并希望使用同步。有什么区别pthread_mutex_lockpthread_mutex_trylock()什么时候应该使用它们?

c linux pthreads

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

c pthread传递int类型的数组

我正在传递一个 int pthread_create 类型的数组并收到错误:

  histogram.c:138:3: warning: passing argument 3 of
 ‘pthread_create’ from incompatible   pointer type [enabled by default]
  expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(int *)’

  void *output_results();
  pthread_create(&t2, NULL, output_results, (void *)bins);

  void *output_results(int *bins) {
      some code
  }
Run Code Online (Sandbox Code Playgroud)

c arrays pthreads

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

使用 C 中的互斥锁使用 2 个线程打印奇数和偶数

程序中的两个线程交替打印偶数和奇数,直到 100。我试过这个,它奏效了。有没有办法访问main内部共享数据的值,当值达到100时终止2个线程

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

pthread_t tid[2];
unsigned int shared_data = 0;
pthread_mutex_t mutex;
unsigned int rc;
//prototypes for callback functions

void* PrintEvenNos(void*);
void* PrintOddNos(void*);

void main(void)
{
    pthread_create(&tid[0],0,&PrintEvenNos,0)
    pthread_create(&tid[1],0,&PrintOddNos,0);
    sleep(3);

    pthread_join(tid[0],NULL);
    pthread_join(tid[1],NULL);
}

void* PrintEvenNos(void *ptr)
{
    pthread_mutex_lock(&mutex);
    do
    {
       if(shared_data%2 == 0)
       {
         printf("Even:%d\n",shared_data);
         shared_data++;
       } else {
          rc=pthread_mutex_unlock(&mutex);//if number is odd, do not print, release mutex
       }
    } while(shared_data <= 100);
}

void* PrintOddNos(void* ptr1)
{
    rc = pthread_mutex_lock(&mutex);
    do
    {
       if(shared_data%2 != 0)
       {
          printf("odd:%d\n",shared_data);
          shared_data++;
       } …
Run Code Online (Sandbox Code Playgroud)

linux mutex pthreads

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

C++ 错误创建 n 个 pthreads-

在应该采用 int k 并创建 k 个 pthread 的函数中出现以下错误:

cse451.cpp:95:60: 错误:从 'void*' 到 'pthread_t* {aka long unsigned int*}' [-fpermissive] 的无效转换

cse451.cpp:97:54: 错误:无效使用无效表达式

我有一种感觉,它与 foo() 函数有关(此时仅用作占位符 foo(){} )

下面列出了相关代码(第 95 行是 pthread_t *threads......,第 97 行是 err=pthread_create....)

void createThreads(int k){
int numThreads = k;
int i = 0;
int err = 0;
pthread_t *threads = malloc(sizeof(pthread_t) * numThreads);
for(i = 0;i<numThreads;i++){
    err = pthread_create(&threads[i], NULL, foo(), NULL);
    if(err != 0){
        printf("error creating thread\n");
    }
}
}

void foo(){}
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors pthreads

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

在同一个循环中集成 pthread_create() 和 pthread_join()

我是多线程编程的新手,我正在学习本教程。在本教程中,有一个简单的示例展示了如何使用pthread_create()pthread_join()。我的问题:为什么我们不能放入pthread_join()与 相同的循环中pthread_create()

参考代码:

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

#define NUM_THREADS 2

/* create thread argument struct for thr_func() */
typedef struct _thread_data_t {
   int tid;
   double stuff;
} thread_data_t;

/* thread function */
void *thr_func(void *arg) {
    thread_data_t *data = (thread_data_t *)arg;

    printf("hello from thr_func, thread id: %d\n", data->tid);

    pthread_exit(NULL);
}

int main(int argc, char **argv) {
    pthread_t thr[NUM_THREADS];
    int i, rc;
    /* create a thread_data_t argument …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading posix pthreads pthread-join

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

gcc 参数:-pthread。它有什么作用?

我开始在 Debian 8 下使用 gcc 进行多线程编程。我已经成功编写并运行了一个多线程测试应用程序 (foobar.c),但我对 Makefile 感到困惑(从示例中复制)。特别是,有效的命令是

gcc foobar.c -o foobar -pthread
Run Code Online (Sandbox Code Playgroud)

我对“-pthread”感到困惑。就是它

(a) 值为“thread”的选项“-p”,或
(b) 参数“-pthread”?

如果是任何一种情况,它实际上在做什么?包括一些图书馆?包括一些对象?设置其他选项?

顺便说一句 - 一个类似的问题15929739被问到但从未得到回答。问题20924412也没有帮助。

multithreading gcc pthreads compiler-flags

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

POSIX 线程,将多个参数传递给具有结构的函数

所以我花了最后几个小时试图谷歌并找出我的代码有什么问题,但我无法弄清楚。

我是一名学生,我刚刚开始学习线程等,所以这对我来说都是全新的,我不是很有经验。

谷歌(和这里)上的答案通常是对代码中一个特定问题的答案,我不知道如何真正让这件事起作用。

这是我的代码的一个非常简化的版本:

http://pastebin.com/wst8Yw8z

#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

using namespace std;

struct Data{
    string a;
    string b;
};

void* thread_func( void *param ){

    struct Data *input = (struct Data*)param;

    string data1 = input->a;
    string data2 = input->b;

    cout << "You said: " << data1 << " " << data2 << endl;

    return NULL;
}

int main( int argc, char *argv[] )
{
    pthread_t child;
    string arg, arg2;

    struct Data *input;

    cout << "Input …
Run Code Online (Sandbox Code Playgroud)

c++ concurrency multithreading posix pthreads

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

pthread_mutex_lock 和 EAGAIN

我已经将 pthread 用于多线程程序,并且遇到以下情况。当我在没有 sleep 命令的情况下运行代码时,它会在运行时导致错误,而当我添加 sleep 命令时,程序会按预期运行。

有睡眠:

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



pthread_mutex_t m_writer = PTHREAD_MUTEX_INITIALIZER;

void *print_str(void *args) {
  sleep(12);
  char *str = (char*) args;
  pthread_mutex_lock(&m_writer);
  printf("%s", str);
  pthread_mutex_unlock(&m_writer);
  pthread_exit(NULL);
}

int main(int argc, char **argv) {
  pthread_t t1;
  pthread_create(&t1, NULL, print_str, "Hello\n");
  pthread_mutex_lock(&m_writer);
  printf("LOL\n");
  pthread_mutex_unlock(&m_writer);
  pthread_join(t1, NULL);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

不睡觉:

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



pthread_mutex_t m_writer = PTHREAD_MUTEX_INITIALIZER;

void *print_str(void *args) {
  char *str = …
Run Code Online (Sandbox Code Playgroud)

c multithreading pthreads strace futex

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

C 中损坏的大小与 prev_size

每当我在线程中分配动态内存时,都会出现“大小损坏与 prev_size”错误。每当我在 main() 中分配内存时,它都可以正常工作。但是在线程中分配动态内存会产生错误。

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

void *fib(void *p);
struct mystruct
{
    int *array;
    int size;
};

int main(int argc, char *argv[])
{
    pthread_t tid;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct mystruct obj;

    obj.size = atoi(argv[1]);;

    pthread_create(&tid, &attr, fib, (void *)&obj);
    pthread_join(tid, NULL);
}

void *fib (void *p)
{
    struct mystruct *x = (struct mystruct *)p;
    x->array = (int *) malloc (x->size);

    for (int i=0; i<x->size; i++){
        x->array[i] = i;
        printf("The array is = %d\n", x->array[i]);
    }   
} …
Run Code Online (Sandbox Code Playgroud)

c pthreads dynamic-memory-allocation

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

如何在C++中停止线程执行

我在主程序中创建了一个线程,一旦主程序终止,线程执行就必须停止。我reader.join();用来终止线程执行。但它并没有停止执行。

我尝试使用下面提到的代码,我正在使用thread.join();函数,但它无法终止线程。在主程序之后,我的线程也继续执行。

#include <algorithm>
#include <array>
#include <atomic>
#include <mutex>
#include <queue>
#include <cstdint>
#include <thread>
#include <vector>

using namespace std;
using namespace std::chrono;

typedef pair<int, Mat> pairImage;

class PairComp {
public:
    bool operator()(const pairImage& n1, const pairImage& n2) const
    {
        if (n1.first == n2.first)
            return n1.first > n2.first;
        return n1.first > n2.first;
    }
};

int main(int argc, char* argv[])
{
    mutex mtxQueueInput;
    queue<pairImage> queueInput;
    int total = 0;
    atomic<bool> bReading(true);
    thread reader([&]() {
        int idxInputImage …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading mutex pthreads c++11

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