testthreadpool.cpp
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Copyright 2007 Stephen Liu
* For license terms, see the file COPYING along with this library.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include "spthread.hpp"
#include "spporting.hpp"
#include "spthreadpool.hpp"
extern int errno;
void threadFunc( void *arg )
{
int seconds = (int) arg;
fprintf( stdout, " in threadFunc %d\n", seconds );
fprintf( stdout, " thread#%ld\n", sp_thread_self() );
sleep( seconds );
fprintf( stdout, " done threadFunc %d\n", seconds);
}
int main( int argc, char ** argv )
{
SP_ThreadPool pool( 2 );
fprintf( stdout, "**main** dispatch 3\n" );
pool.dispatch( threadFunc, (void*)3 );
fprintf( stdout, "**main** dispatch 6\n");
pool.dispatch( threadFunc, (void*)6 );
fprintf( stdout, "**main** dispatch 7\n");
pool.dispatch( threadFunc, (void*)7 );
fprintf( stdout, "**main** done first\n" );
sleep( 20 );
fprintf( stdout, "\n\n" );
fprintf( stdout, "**main** dispatch 3\n" );
pool.dispatch( threadFunc, (void *) 3 );
fprintf( stdout, "**main** dispatch 6\n" );
pool.dispatch( threadFunc, (void *) 6 );
fprintf( stdout, "**main** dispatch 7\n");
pool.dispatch( threadFunc, (void *) 7 );
fprintf( stdout, "**main done second\n" );
sleep(20);
return 0;
}