标签: pthreads

C 多线程 | for 循环中的线程创建使用上次迭代中的参数

我是多线程的新手,而且总体来说并不是 C 语言中最好的,所以对我来说就这样了。

我有一个 for 循环,它创建了许多线程,我将参数传递给这些线程:

  for(int i = 0; i < NO_OF_THREADS; i++) {

    int ordered_product = (rand() % NO_OF_PRODUCTS);
    int ordered_quantity = (rand() % 10) + 1;
    int customer = (rand() % NO_OF_CUSTOMERS);

    printf("%d %d %d\n", customer+1, ordered_quantity, ordered_product+1);

    ThreadArgs myargs = {customer, ordered_product, ordered_quantity};

    int rc = pthread_create(&mythreads[i], NULL, thread_function, &myargs);
    if(rc != 0) {
      perror("Pthread create");
      exit(1);
    }

  }
Run Code Online (Sandbox Code Playgroud)

我有一个函数“thread_function”,它是这样写的:

void* thread_function(void* arg) {

  ThreadArgs* args = (ThreadArgs*) arg;
  ThreadArgs myargs = *args;
  int customer_id = …
Run Code Online (Sandbox Code Playgroud)

c multithreading operating-system mutex pthreads

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

有时我的二进制信号量没有等待正确的时间

有时它等待的时间不够长。我可能错过了一些简单的东西 - 但我找不到它。为什么等待函数有时会过早返回

#define SEMAPHORE_MAXWAIT   -1
#define SEMAPHORE_NOWAIT    0

// Type your code here, or load an example.
typedef struct binary_semaphore {
    pthread_mutex_t mutex;
    pthread_cond_t condvar;
    bool variable;
}binary_semaphore_t;


static struct timespec *timespec_addms(struct timespec *ts, unsigned ms)
{
    uint64_t nsec;
    if(ts)
    {
        ts -> tv_sec += ms / 1000;
        nsec = ts -> tv_nsec + (ms % 1000) * 1000000ULL;
        ts -> tv_sec += nsec / 1000000000ULL;
        ts -> tv_nsec = nsec % 1000000000ULL;
    }
    return ts;
}

static int …
Run Code Online (Sandbox Code Playgroud)

c mutex timeout semaphore pthreads

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

pthread_mutex_lock并解锁

我有两个线程,它们运行非常快,我的pthread_mutex_lock使用和调用pthread_mutex_unlock进入全球(externed变量)数据

问题是我的应用程序需要大约15-20%的CPU在Ubuntu Linux上运行,

相同的代码,但与EnterCriticalSection的和LeaveCriticalSection和运行在Windows上使用的CPU的1-2%

c++ mutex pthreads

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

用户定义的信号1终止我的POSIX程序!

我有一个程序运行一些pthreads,并在每个线程中有一个connect(),recv()send().问题是,有时它会突然关闭显示消息的整个程序"User defined signal 1".它在Linux中使用POSIX线程在C中运行.代码是这样的:

pthread_mutex_t cur_lock;
int stop = 0;

 void SocketsFunction(){
 //..
connect();
while(recv()<0)
{
  //do stuff
send();
}
close();
return NULL;
}

void job()
{
 //..
while (!stop)
{
    if (something)
               //..
    else
    {
        stop = 1;
        break;
    }

    pthread_mutex_unlock(&cur_lock);

    SocketsFunction();

    pthread_mutex_lock(&cur_lock);
}

pthread_mutex_unlock(&cur_lock);

return NULL;
}

main(){
//..
 pthread_mutex_init(&cur_lock, NULL);
//..
 for(i = 0; i < 30; ++i)
    pthread_create(&pID, NULL, job, NULL);

 //..
}
Run Code Online (Sandbox Code Playgroud)

c linux posix pthreads

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

C++中使用posix线程的一些奇怪的东西

我在C++中遇到了pthreads的奇怪错误,我尝试运行这段代码:

typedef struct
{
    struct sockaddr_in clienAddr;
    int clientLength;
    string message;
}param;

pthread_t clientThread;

param sentParam ;
sentParam.clienAddr = clientAddress;
sentParam.clientLength= client_info;
sentParam.message=buffString;

cout <<"sentParam: "<<sentParam.message<<endl;
// it prints well.

int i = pthread_create(&clientThread, NULL, handleClientRequestRead,&sentParam );
cout <<"i: "<<i<<endl;
the function which be called

void* handleClientRequestRead(void* params)
{
    // cout<<"params: "<< ;
    string msg = (( param *)(params))->message;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试打印msg时它是空的.任何帮助将不胜感激

c++ linux pthreads

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

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

如何在C中使用函数指针?

我正在编写包含下面给出的函数的pthread程序,我想在一个函数之间调用另一个函数.

void *fun1(void *arg1)
{
fun2(NULL);
}
void *fun2(void *arg)
{
fun1(NULL);
}
Run Code Online (Sandbox Code Playgroud)

当我调用上面显示的另一个函数时,我收到错误,如下所示

错误:'fun2'的冲突类型

注意:先前隐含的'fun2'声明就在这里

如何在fun1之间调用fun2

c gcc pthreads

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

如何避免互斥变量被同一个线程锁定两次?

queueLIFOQList

//  This function is run by the thread `Producer`.
void *threadProducerFunction (void *arg)
{
    Q_UNUSED (arg);

    while (1)
    {
        if (queueLIFO.length () < 10)
        {
            pthread_mutex_lock (&mutexVariable);
            queueLIFO.push_back (1);
            pthread_mutex_unlock (&mutexVariable);
        }
        else
        {
            pthread_mutex_lock (&mutexVariable);
            pthread_cond_wait (&conditionVariable, &mutexVariable);
        }
    }
    return NULL;
}
Run Code Online (Sandbox Code Playgroud)

现在,考虑以下链接中的信息:https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal

pthread_cond_wait() - 应该在锁定互斥锁时调用此例程,并在等待时自动释放互斥锁.

收到信号并唤醒线程后,互斥锁将自动锁定以供线程使用.

然后程序员负责在线程完成时解锁互斥锁.

当从另一个线程接收到信号时,pthread_cond_wait将锁定该线程的使用的互斥锁,这意味着在我的情况下,控件将进入if语句,其中互斥锁已被锁定pthread_cond_wait(来自else条件)并且我们将其锁定现在又来了.

我是否以错误的方式编写了代码逻辑?怎么样?

c++ qt multithreading pthreads

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

如何用C中的线程对数组元素求和?

我致力于此,但我没有找到我的问题的解决方案。这是我的代码。此代码在 pthread_create 和 pthread_join 行上给出错误。我尝试了一切来解决这个问题,但我不能这样做。

  #include <stdio.h>
  #include <pthread.h>
  #define array_size 1000
  #define no_threads 10

 float a[array_size];
 int global_index = 0;
 int sum = 0;
 pthread_t mutex_t ,mutex1;

 void *slave(void *ignored)
{
 int local_index, partial_sum = 0;
 do {
    pthread_t mutex_t ,lock(mutex1);
      local_index = global_index;
      global_index++;
    pthread_t mutex ,unlock(mutex1);

    if (local_index < array_size)
        partial_sum += *(a + local_index);
}
while
 (local_index < array_size);

pthread_t mutex , lock(mutex1);
sum += partial_sum;
pthread_t mutex_t , unlock(mutex1);

 return 0;
}

 main()
 {
  int …
Run Code Online (Sandbox Code Playgroud)

c pthreads

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

循环创建线程

像这样在循环中创建线程是否不好?

线程函数示例:

void *thread_func(void* args)
{
  const char *str = (const char *)args;

  printf("Threading %s\n", str);
  usleep(1000);
}
Run Code Online (Sandbox Code Playgroud)

主循环:

pthread_t thread;
while (1) {
    char *str = "test str";
    if (pthread_create(&thread, NULL, thread_func, str)) {
            fprintf(stderr, "Failed to create thread\n");
            return -1;
    }
    usleep(3000);
  /* Thread guaranteed finished here */
  }
Run Code Online (Sandbox Code Playgroud)

或者我必须创建一次并循环使用

c multithreading gcc pthreads

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