c - pthread - 条件信号和等待

标签 c linux multithreading pthreads

所以我目前正在尝试使用 pthread 做一些事情,但我一直遇到在我看来应该有效的问题。在这个非常未完成的示例中,我尝试使用“y”个线程来操作“x”个数据量。所以我制作了 2 个结构,1 个用于“x”数据量,一个用于“y”线程量。线程的结构有一个指向数据结构的指针,因此我可以切换它们而无需重新创建线程或将整个数据集传递给线程。

无论如何,我在运行当前版本的程序时遇到了问题。还没有完成线程池,因为我还没有做到那么远。它应该做的是在每个线程的循环中打印出一些信息。它应该在我发出条件信号时开始。然而,条件信号似乎无法正常工作,因为并非所有线程都被激活...

我希望有人能帮助我!

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

#define NUM_THREADS     5    
#define NUM_LOOP        50



typedef struct infos {

   int real;    

} infos;



typedef struct thread_specs {

   infos          *infos;

   int thread_id;

   pthread_mutex_t mutex_condition;    
   pthread_cond_t  cond_wait;    

} thread_specs;




/* some trivial work to be done  but it should wait on the conditional wait*/
void *printtest(void *arg) {

   int i,sleep;
   thread_specs *thread_stuff= (thread_specs*)(arg);

    while (1) {
      pthread_cond_wait( &(thread_stuff->cond_wait) , &(thread_stuff->mutex_condition) );

         for (i=0;i<5;i++) {
            sleep=rand()%100000;
            printf("thread: %6i | loop: %2i\n",thread_stuff->thread_id,i); // thread_stuff->infos.real
            usleep(sleep);
         }
   }
}





int main () {

   pthread_t         threads[NUM_THREADS];

   infos             infostruct[NUM_LOOP];

   thread_specs      thread_info[NUM_THREADS];

   size_t            i;

   srand(time(0));

   for (i=0;i<NUM_THREADS;i++) {
      // setting the current loop into the thread_info struct and the info struct, later this would be more complex data but as an example it should suffice
      thread_info[i].thread_id=i;
      pthread_mutex_init(&(thread_info[i].mutex_condition), NULL);
      pthread_mutex_lock(&(thread_info[i].mutex_condition));
      infostruct[i].real=i;

      thread_info[i].infos=&infostruct[i];

   }


  // creating threads and passing a single index from the struct to it. later the infos struct pointer would be rotated to allow for different manipulation of data
   for (i=0;i<NUM_THREADS;i++) {
      if (pthread_create(&threads[i], NULL, printtest, &thread_info[i] )) {
         printf("ERROR creating thread");
      } else {
         printf("SUCCES creating thread #%i\n",i);
      }
   }

   printf("created all thread\n");

  // trying to signal all threads to start their work, make sure there mutex is unlocked so they are waiting for a the condition.
   for(i=0;i<NUM_THREADS;i++) {
      pthread_mutex_lock(&(thread_info[i].mutex_condition));
      pthread_mutex_unlock(&(thread_info[i].mutex_condition));
      pthread_cond_signal(&(thread_info[i].cond_wait));
   }

   printf("signaled condition\n");

   sleep(10);

   printf("\n!!DONE!!\n");

}  

最佳答案

您使用的 pthread_cond_wait 完全错误。它只应该在您的线程持有互斥锁时起作用。他们的全部技巧是 pthread_cond_wait 在等待期间暂时释放对互斥量的保持,并在返回时重新分配它。

此外,对 pthread_cond_signal 的调用最好放在持有锁的临界区内部,而不是之后。

关于c - pthread - 条件信号和等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22708332/

相关文章:

linux - 使用 awk/sed/cut/grep 从文本文件中提取冒号分隔的字段/值

c - undefined reference ... - 链接问题?

c - 用 ASCII 表示 UTF-8

c++ - 堆栈问题

c - 我如何将一个函数内的int指针传递给另一个函数,并在那里进行操作?

java - Kotlin上的Server-Client应用程序出现问题

linux - 使用 nfs rootfs 读取 tty 时出现问题

python - 如何使用 python 脚本在 sudo 之后执行一组命令

java - 使用信号量进行虚假唤醒

java - 销毁/停止线程