Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function()
- {
- for (int k = 0; k < 10; k++)
- {
- pthread_mutex_lock (&pmutex);
- pthread_t pthreads[CPUTHRDS];
- launch_threads(pthreads);
- /* Unlock and start timer */
- pthread_mutex_unlock (&pmutex);
- tot_time = -msecond();
- /* Join taskthreads when they are done */
- join_threads(pthreads);
- tot_time += msecond();
- //<do something>
- }
- }
- void launch_threads(pthread_t *pthreads)
- {
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
- printf("#HOST# Starting Task Threads\n");
- /* Launch the CPU threads */
- for (int i = 0; i < CPUTHRDS; i++)
- {
- fprintf(stderr, "launchthread %d\n", i);
- int rc = pthread_create(&pthreads[i], &attr, cpu_thread, NULL);
- if (rc) {
- printf("ERROR; return code from pthread_create() is %d\n", rc);
- exit(-1);
- }
- }
- printf("#HOST# CPU-Threads started: %d\n", CPUTHRDS);
- pthread_attr_destroy(&attr);
- }
- void join_threads(pthread_t *pthreads)
- {
- int rc = 0;
- for(int i=0; i < CPUTHRDS; i++)
- {
- void *status;
- rc = pthread_join(pthreads[i], &status);
- if (rc) {
- printf("ERROR; return code from pthread_join() is %d\n", rc);
- exit(-1);
- }
- printf("#HOST# Thread %d joined\n", i);
- }
- printf("#HOST# All threads joined\n");
- }
- void *cpu_thread(void *arg)
- {
- unsigned long thid = pthread_self();
- fprintf(stderr, "started %lu\n", thid);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement