int main() { printf("Version %f.%f\n", cs224_project_VERSION_MAJOR, cs224_project_VERSION_MINOR); class_memory(mem,MSIZE); // give it memory allocator struct thread_pair threads[10]; std::vector<thread_pair> vThreads(10); void *exit_status; printf( "Thread Test begin...\n"); // main program continues while thread executes int i=1; for(i=1; i < 10; ++i) { threads[i].arg = i; pthread_create(&(threads[i].id), NULL, thread_func, &(threads[i].arg)); } // wait for the thread to terminate for(i=1; i < 10; ++i) { pthread_join(threads[i].id, &(threads[i].exit_status)); } printf("Thread test end...\n"); return 0; }
int main(int argc, char* argv[]) { nice(-20); int thread_num; if (argc == 1) { // default one thread if no thread num is specified thread_num = 1; } else if (argc == 2) { thread_num = atoi(argv[1]); } else return -1; printf("Num of threads to spawn is %d\n", thread_num); // tune pthread stack size to support more threads pthread_attr_t attr; set_pthread_stack_size(&attr, kPthreadStackSize); // divide the kUpper by the thread num advance = kUpper/thread_num; std::vector<pthread_t> vThreads(thread_num); for (int i = 0; i < thread_num; ++i) { pthread_t tid; // the index for the thread int* pArg = (int*)malloc(sizeof(int)); *pArg = i; if (0 != pthread_create(&tid, &attr, func, pArg)) { printf("%s\n", strerror(errno)); printf("Max num of created thread is %d\n", i); return -1; } vThreads[i] = tid; } uint64_t total = 0; for (int i = 0; i < thread_num; ++i) { void* part; assert(0 == pthread_join(vThreads[i], &part)); total += *(uint64_t*)part; free((uint64_t*)part); } printf("The reuslt is %lu\n", total); return 0; }