首页 \ 问答 \ 同时启动所有线程(Starting all threads at the same time)

同时启动所有线程(Starting all threads at the same time)

我无法理解像pthread_mutex_lock / unlockpthread_cond_wait / signal这样的条件变量

我试图创建九个threads ,并让它们同时运行,以确定哪个threads效率最高。

    int threadNumber = 0;
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

    #define NUM_THREADS    9

    //used to store the information of each thread
    typedef struct{
        pthread_t  threadID;
        int policy;
        struct sched_param param;
        long startTime;
        long taskStartTime;
        long endTime1;
        long endTime2;
        long endTime3;
        long runTime;
        char startDate[30];
        char endDate[30];
    }ThreadInfo;

ThreadInfo myThreadInfo[NUM_THREADS];



//main function
int main(void){

   printf("running...\n");

   pthread_mutex_lock(&mutex); //lock the mutex//////
   pthread_cond_wait(&cond, &mutex); //start waiting//////


   int fifoPri = 60;
   int rrPri = 30;

   //create the 9 threads and assign their scheduling policies
   for(int i=0; i<NUM_THREADS; i++){

      if(i%3 == SCHED_OTHER){
            myThreadInfo[i].policy = SCHED_OTHER;
            myThreadInfo[i].param.sched_priority = 0;

      }
      else if (i%3 == SCHED_RR){ 
            myThreadInfo[i].policy = SCHED_RR;
            myThreadInfo[i].param.sched_priority = rrPri++; 
      }

      else{
            myThreadInfo[i].policy = SCHED_FIFO; 
            myThreadInfo[i].param.sched_priority = fifoPri++; 

      }

      pthread_create( &myThreadInfo[i].threadID, NULL, ThreadRunner, &myThreadInfo[i]);

   }

   printf("\n\n");

   pthread_mutex_unlock(&mutex) //unlock the mutex/////////
   pthread_cond_signal(&cond); //signal the threads to start////////


   //join each thread
   for(int g = 0; g < NUM_THREADS; g++){
      pthread_join(myThreadInfo[g].threadID, NULL);
   }


   //print out the stats for each thread and perform an analysis of the data
   DisplayThreadSchdStats();


   return 0;
}

...

所以当主函数启动时,我lock互斥锁以确保线程在我告诉他们使用pthread_lock(&mutex)和pthread_cond_wait(&cond,&mutex)之前不会启动。

然后我使用各种调度策略创建所有九个线程。 完成之后,我尝试通过使用pthread_mutex_unlock(&mutex)和pthread_cond_signal(&cond)来同时启动所有线程,

但是当我运行它时,它永远不会解锁线程。 主函数的“正在运行...”打印语句熄灭,但线程永远不会启动。 (threadrunner有一个函数,它们都打印出大量不同的数字,所以我可以看到它们是否启动)。 我在做pthread互斥体和pthread cond的错误是什么?


I'm having trouble understanding condition variables like pthread_mutex_lock/unlock and pthread_cond_wait/signal

I'm trying to create nine threads, and have them all run at the same time to figure out which is the most efficient.

    int threadNumber = 0;
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

    #define NUM_THREADS    9

    //used to store the information of each thread
    typedef struct{
        pthread_t  threadID;
        int policy;
        struct sched_param param;
        long startTime;
        long taskStartTime;
        long endTime1;
        long endTime2;
        long endTime3;
        long runTime;
        char startDate[30];
        char endDate[30];
    }ThreadInfo;

ThreadInfo myThreadInfo[NUM_THREADS];



//main function
int main(void){

   printf("running...\n");

   pthread_mutex_lock(&mutex); //lock the mutex//////
   pthread_cond_wait(&cond, &mutex); //start waiting//////


   int fifoPri = 60;
   int rrPri = 30;

   //create the 9 threads and assign their scheduling policies
   for(int i=0; i<NUM_THREADS; i++){

      if(i%3 == SCHED_OTHER){
            myThreadInfo[i].policy = SCHED_OTHER;
            myThreadInfo[i].param.sched_priority = 0;

      }
      else if (i%3 == SCHED_RR){ 
            myThreadInfo[i].policy = SCHED_RR;
            myThreadInfo[i].param.sched_priority = rrPri++; 
      }

      else{
            myThreadInfo[i].policy = SCHED_FIFO; 
            myThreadInfo[i].param.sched_priority = fifoPri++; 

      }

      pthread_create( &myThreadInfo[i].threadID, NULL, ThreadRunner, &myThreadInfo[i]);

   }

   printf("\n\n");

   pthread_mutex_unlock(&mutex) //unlock the mutex/////////
   pthread_cond_signal(&cond); //signal the threads to start////////


   //join each thread
   for(int g = 0; g < NUM_THREADS; g++){
      pthread_join(myThreadInfo[g].threadID, NULL);
   }


   //print out the stats for each thread and perform an analysis of the data
   DisplayThreadSchdStats();


   return 0;
}

...

so when the main function starts, I lock the mutex to make sure the threads don't start before I tell them to with pthread_lock(&mutex) and pthread_cond_wait(&cond, &mutex)

then I create all nine threads with various scheduling policies. After that's completely done, I try to tell the threads to all start at the same time using pthread_mutex_unlock(&mutex) and pthread_cond_signal(&cond)

But when I run this, it never unlocks the threads. The main function's "running..." print statement goes off, but the threads never start. (threadrunner has a function where they all print out a ton of different numbers so I can see if they launch). What am I doing wrong with the pthread mutex and pthread cond?


原文:https://stackoverflow.com/questions/48753142
更新时间:2023-12-04 22:12

最满意答案

允许用户将Facebook帐户附加到其当前帐户,以便他们可以使用其中任何一个登录。

  • 在用户设置页面中添加“链接到Facebook帐户”的链接
  • 链接只是使用OmniauthCallbacksController驱动用户通过正常的Facebook身份验证过程,只需确保在OmniauthCallbacksController facebook方法中添加一些代码以查看用户是否已登录,如果他只是为用户添加身份验证令牌(我有一个表存储每个用户的身份验证令牌)

如果用户仅使用Facebook帐户注册,则在编辑其设置时如何隐藏(并且不需要)密码字段。

看看这个: https//github.com/plataformatec/devise/wiki/How-To : -Allow-users-to-edit-their-account-without- providing-a- password

希望这有帮助。


Let users attach a Facebook account to their current account so they can login with either.

  • in the user setting page add a link to "link to Facebook account"
  • the link just drive the user through the normal Facebook authentication processes using the OmniauthCallbacksController, just make sure in your OmniauthCallbacksController facebook method you add some code to see if the user is already logged in and if he is you just add an authentication token for the user (I have a table that stores the authentication token for each user)

If the user only signed up with a Facebook account, how do I hide (and not require) the password fields when editing their settings.

Take a look at this: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

Hope this help.

相关问答

更多
  • 没有使用过验证,但你可以随时使用自己的一些验证: validates :email, uniqueness: true validates :email, presence: true validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, message: "valid email please" Haven't used validatable, but you could always j ...
  • 您正在添加:在您注册时可以在您的帐户模型中确认它将向您发送确认邮件以确认您的电子邮件,如果您在未确认的情况下登录将会发出未经授权的错误。 You are adding :confirmable in your account model when you signup it will send you the confirmation mail to confirm your email, if you login without confirming it will throw an Unauthoriz ...
  • 如果您对用户注册不感兴趣,那么这两个选项都会给您相同的结果,但我更愿意从用户模型中删除可registerable模块以避免加载它并根本不使用它。 你可以在下一个链接中看到devise方法,所以你可以通过不加载模块来理解你所避免的: https : //github.com/plataformatec/devise/blob/master/lib/devise/models.rb# L77 另一方面,如果您有兴趣通过任何子类(比如买方或卖方,使用STI)而不是类本身(用户) registerable用户,则需 ...
  • 删除:registerable从Admin模型:registerable将删除Devise::RegistrationsController所有路由助手,例如: edit_admin_registrations_path 。 您需要在Admin模型中保留:registerable并覆盖Devise::RegistrationsController以限制对new和create操作的访问。 创建一个新的registrations_controller.rb ,其中包含: class RegistrationsC ...
  • 您是否在config / initializers / omniauth.rb中提供了凭据 Rails.application.config.middleware.use OmniAuth::Builder do provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], scope:"user:email,user:follow" end 你可以通过Link来完成 In the Devise's wiki it is said: where( ...
  • 允许用户将Facebook帐户附加到其当前帐户,以便他们可以使用其中任何一个登录。 在用户设置页面中添加“链接到Facebook帐户”的链接 链接只是使用OmniauthCallbacksController驱动用户通过正常的Facebook身份验证过程,只需确保在OmniauthCallbacksController facebook方法中添加一些代码以查看用户是否已登录,如果他只是为用户添加身份验证令牌(我有一个表存储每个用户的身份验证令牌) 如果用户仅使用Facebook帐户注册,则在编辑其设置时如何 ...
  • 当Rails服务器启动时,在add_module gem代码中, lib / add_module / modules.rb为所有可用模块调用add_module 。 lib / devise.rb调用Devise::Mapping.add_module module_name 。 lib / devise / mapping.rb然后构建像registerable?这样的方法registerable? 使用此代码: # Create magic predicates for verifying what ...
  • 答案可以在这里找到。 由于您为三个不同的模型调用devise_for ,其中一个使用omniauthable模块,因此Devise变得混乱。 或者: 删除除以下:users之外的所有devise_for方法。 或者从用户模型中删除omniauthable模块,创建自己的omniauth路由,并通过将omniauth配置移动到新文件中来停止使用devise的中间件。 所以,而不是在devise.rb中有这个: Devise.setup do |config| config.omniauth :twitte ...
  • 出于某种原因, include DeviseTokenAuth::Concerns::User删除omniauthable 。 我修复了添加它的问题: devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable include DeviseTokenAuth::Concerns::U ...
  • 我试图复制你的应用程序,对我来说这很有效。 尝试使用devise_scope ... #routes.rb resources :departments devise_for :users devise_scope :user do get 'departments/:department_id/sign_in' => 'devise/sessions#new', :as => :department_user_sign_in end 希望这有帮助,如果有任何问题,请告诉我。 I tri ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。