c++ - Effective way of signaling and keeping a pthread open? -


i have code trying run intense matrix processing, thought faster if multithreaded it. however, intention is keep thread alive can used in future more processing. here problem, multithreaded version of code runs slower single thread, , believe problem lies way signal/keep threads alive.

i using pthreads on windows , c++. here code thread, runtest() function matrix calculations happen:

void* playqueue(void* arg) {     while(true)     {         pthread_mutex_lock(&queuelock);         if(testqueue.empty())             break;         else             testqueue.pop();         pthread_mutex_unlock(&queuelock);         runtest();     }     pthread_exit(null);  } 

the playqueue() function 1 passed pthread, , have of now, there queue (testqueue) of lets 1000 items, , there 100 threads. each thread continue run until queue empty (hence stuff inside mutex).

i believe reason multithread runs slow because of called false sharing (i think?) , method of signaling thread call runtest() , keeping thread alive poor.

what effective way of doing multithreaded version run faster (or @ least equally fast) iterative version?

here full version of code (minus matrix stuff)

# include <cstdlib> # include <iostream> # include <cmath> # include <complex> # include <string> # include <pthread.h> # include <queue>  using namespace std;  # include "matrix_exponential.hpp" # include "test_matrix_exponential.hpp" # include "c8lib.hpp" # include "r8lib.hpp"  # define num_threads 3  int main ( ); int counter; queue<int> testqueue; queue<int> anotherqueue; void *playqueue(void* arg); void runtest(); void matrix_exponential_test01 ( ); void matrix_exponential_test02 ( ); pthread_mutex_t anotherlock; pthread_mutex_t queuelock; pthread_cond_t queue_cv;  int main ()  {     counter = 0;     /* (int i=0;i<1; i++)         for(int j=0; j<1000; j++)         {             runtest();           cout << counter << endl;         }*/      pthread_t threads[num_threads];     pthread_mutex_init(&queuelock, null);     pthread_mutex_init(&anotherlock, null);     pthread_cond_init (&queue_cv, null);     for(int z=0; z<1000; z++)     {         testqueue.push(1);     }     for( int i=0; < num_threads; i++ )     {        pthread_create(&threads[i], null, playqueue, (void*)null);     }     while(anotherqueue.size()<num_threads)     {      }     cout << counter;     pthread_mutex_destroy(&queuelock);     pthread_cond_destroy(&queue_cv);     pthread_cancel(null);     cout << counter;     return 0; }  void* playqueue(void* arg) {     while(true)     {         cout<<counter<<endl;         pthread_mutex_lock(&queuelock);         if(testqueue.empty()){                 pthread_mutex_unlock(&queuelock);             break;         }         else             testqueue.pop();         pthread_mutex_unlock(&queuelock);         runtest();     }     pthread_mutex_lock(&anotherlock);     anotherqueue.push(1);     pthread_mutex_unlock(&anotherlock);     pthread_exit(null); }  void runtest() {       counter++;       matrix_exponential_test01 ( );       matrix_exponential_test02 ( ); } 

so in here "matrix_exponential_tests" taken this website permission , of matrix math occurs. counter used debug , make sure instances running.

doesn't stuck ?

while(true) {     pthread_mutex_lock(&queuelock);     if(testqueue.empty())         break; //<----------------you break without unlock mutex...     else         testqueue.pop();     pthread_mutex_unlock(&queuelock);     runtest(); } 

the section between lock , unlock run slower if in single thread.

mutexes slowing down. should lock critical section, , if want speed up, try not use mutex @ all.

you can supplying test via function argument rather use queue.

one way avoid using mutex use vector without deleting , std::atomic_int (c++11) index (or lock getting current index , increment)

or use iterator this:

vector<test> testvector; vector<test>::iterator it; //when initialized to: = testvector.begin(); 

now loop can this:

while(true) {     vector<test>::iterator it1;     pthread_mutex_lock(&queuelock);     it1 = (it==testvector.end())? : it++;      pthread_mutex_unlock(&queuelock);      //now outside critical section:      if(it==testvector.end())         break;      //you don't delete or change vector     //so can use it1 iterator freely     runtest(); } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -