同时启动所有线程(Starting all threads at the same time)
我无法理解像
pthread_mutex_lock
/unlock
和pthread_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
andpthread_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
最满意答案
允许用户将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 ...
-
使用现有设计模型的Typus获得401未授权错误(Typus with existing devise model getting 401 unauthorized error)[2023-07-28]
您正在添加:在您注册时可以在您的帐户模型中确认它将向您发送确认邮件以确认您的电子邮件,如果您在未确认的情况下登录将会发出未经授权的错误。 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( ...
-
设计:在同一个应用程序中使用可注册和omniauthable(Devise: Using registerable and omniauthable in the same app)[2023-06-27]
允许用户将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 ...