#include #include #include #include #include #define NUM_THREADS 4 #define SORT_SIZE 1000 #define NSAMPLES 5 const char* samples[NSAMPLES] = { "this is just a test 1", "THIS IS JUST A TEST 1", "this IS JUST A TEST 1", "THIS IS JUST A TEST 2", "this is just a test 2" }; extern const std::locale& get_locale() { static const std::locale* l = 0; #ifdef DO_LOCK static std::_STL_mutex_lock __lock __STL_MUTEX_INITIALIZER; std::_STL_auto_lock __auto(__lock); if( !l ) { l = &std::locale::classic(); } #else if( !l ) { static std::_STL_mutex_lock __lock __STL_MUTEX_INITIALIZER; std::_STL_auto_lock __auto(__lock); l = &std::locale::classic(); } #endif return *l; } template bool lowercase_less( T c1, T c2 ) { return std::tolower(c1, get_locale()) < std::tolower(c2, get_locale()); } template bool str_case_less(const std::basic_string& s1, const std::basic_string& s2) { return std::lexicographical_compare( s1.begin(), s1.end(), s2.begin(), s2.end(), std::ptr_fun( lowercase_less ) ); } pthread_mutex_t count_mutex; pthread_cond_t ready_sort; int ready_count = 0; typedef std::list test_list; void* thread_main( void* ) { test_list tl; for( int i=0; i ); t2 = clock(); printf( "Sorting time: %7.2f\n", (float)(t2-t1)/CLOCKS_PER_SEC); return 0; } int main() { pthread_t thread[NUM_THREADS]; pthread_attr_t attr; int t; /* Initialize mutex and condition variable objects */ pthread_mutex_init(&count_mutex, NULL); pthread_cond_init(&ready_sort, NULL); /* Initialize and set thread detached attribute */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); for(t=0;t < NUM_THREADS;t++) { printf("Creating thread %d\n", t); pthread_create(&thread[t], &attr, thread_main, NULL); } /* Free attribute and wait for the other threads */ for(t=0;t < NUM_THREADS;t++) { pthread_join(thread[t], NULL); } pthread_attr_destroy(&attr); pthread_mutex_destroy(&count_mutex); pthread_cond_destroy(&ready_sort); return 0; }