View difference between Paste ID: 5iUsy8L2 and 88dYb1Wn
SHOW: | | - or go back to the newest paste.
1
function()
2
{
3
	for (int k = 0; k < 10; k++)
4
	{
5
		pthread_mutex_lock (&pmutex);
6
		
7
		pthread_t pthreads[CPUTHRDS];
8
		launch_threads(pthreads);
9
10
		/* Unlock and start timer */
11
		pthread_mutex_unlock (&pmutex);
12
		tot_time = -msecond();
13
14
		/* Join taskthreads when they are done */
15
		join_threads(pthreads);
16
		tot_time += msecond();
17
18
		//<do something>
19
	}
20
}
21
22
void launch_threads(pthread_t *pthreads)
23
{
24
    pthread_attr_t attr;
25
    pthread_attr_init(&attr);
26
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
27
    
28
    printf("#HOST# Starting Task Threads\n");    
29
30
    /* Launch the CPU threads */
31
    for (int i = 0; i < CPUTHRDS; i++)
32
    {
33
        fprintf(stderr, "launchthread %d\n", i);
34
        int rc = pthread_create(&pthreads[i], &attr, cpu_thread, NULL);
35
        if (rc) {
36
            printf("ERROR; return code from pthread_create() is %d\n", rc);
37
            exit(-1);
38
        }
39
    }
40
    printf("#HOST# CPU-Threads started: %d\n", CPUTHRDS);
41
42
    pthread_attr_destroy(&attr);
43
}
44
45
void join_threads(pthread_t *pthreads)
46
{
47
    int rc = 0;
48
    for(int i=0; i < CPUTHRDS; i++)
49
        {
50
            void *status;
51
            rc = pthread_join(pthreads[i], &status);
52
            if (rc) {
53
                printf("ERROR; return code from pthread_join() is %d\n", rc);
54
                exit(-1);
55
            }
56
            printf("#HOST# Thread %d joined\n", i);
57
        }
58
        printf("#HOST# All threads joined\n");
59
}
60
61
void *cpu_thread(void *arg)
62
{
63
    unsigned long thid = pthread_self();
64
    fprintf(stderr, "started %lu\n", thid);
65
}