c - Pthread lost signals / slow conditions? -


I am writing a simple thread for some small jobs (100 to 700 micronsense). I am working with only two threads (because there are only two jobs and there are only two cores in the processor). My problem is that most of the time both jobs are executed by the same thread. There is no problem with large jobs (some milliseconds)

Expected behaviors (in this case, speeds are expected as follows:

  • Thread 1 after cod_wait
  • Job executed by: 1
  • Threads 0
  • Worked by Job: 0
  • Thread 1 before cod_wit
  • / Li>
  • The formula after the quote_white
  • before the thread 0 cond_wait
  • Thread 1 before cod_wight

Or worse (Hint to lose another thread?):

  • Thread out after cod_white
  • < Li> Job executed by: 0
  • for pthread_mutex_lock (& ​​amp; pl-> mutex); (;;); / / Wait on the notification that a new job is available / / (Pl- & quot; Queue_head == faucet) {// printf ("Thread% d before condom \ n", thread ID); Pthread_cond_wait (& amp; Pl- & gt; Worksite, & amp; PL- & gt; Mutes); // printf ("thread% d after condomite \ n", thread ID); } / * Get the first job * / job = PL- & gt; Q_Hade; If (Job! = Null) {/ * Quote * / pl-> queue_head = job-> remove the next job; If (Job == pl- & quot; queue_tail) {pl- & gt; Queue_tail = NULL; } Pthread_mutex_unlock (& ​​amp; pl-> Mutex); / * Get Job Parameters * / func = job- & gt; Fank; Arg = jobs & gt; Arg; / * Execute / / printf ("Executed job:% d \ n", threadID); Funk (ARG, ThreadID); / * Acquisition Lock * / pthread_mutex_lock (& ​​amp; pl-> Mutex); }}

    Before the submission of jobs, both threads wait for the loop in the event of a tenure. The code is presented by the following code lines (in both code snippets I have removed the code used to wait for both jobs to be completed):

      pthread_mutex_lock (& Amp; pl-> muttex); / * Attach job to queue * / if (pl-> queue_head == null) {pl- & gt; Queue_head = job [numJobs-1]; } Else {pl-> queue_tail-> Next = Job [numJobs-1]; } Pl-> Queue_tail = job [0]; / * A waste * / pthread_cond_broadcast (& amp; pl-> workscv) if the thread is awake; Pthread_mutex_unlock (& ​​amp; pl-> muttex);  

    The default attributes are used for mutes, threads, and situations. Environment: GCC 4.2.1, Mac OSX Snow Leopard

    Am I doing wrong?

    Thank you! Instead of

    Code> should be used. Additionally, if the thread has taken a job, then it should indicate the condition variable:

      / * queue * / pl- & gt; Queue_head = job-> gt; If (Job == pl- & quot; queue_tail) {pl- & gt; Queue_tail = NULL; } If (pl-> queue_head! = NULL) pthread_cond_signal (& amp; pl-> workscv); Pthread_mutex_unlock (& ​​amp; pl-> muttex);  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -