-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy paththreads.c
101 lines (85 loc) · 2.83 KB
/
threads.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/***********************************************************
* This file is part of glyr
* + a command-line tool and library to download various sort of music related metadata.
* + Copyright (C) [2011-2016] [Christopher Pahl]
* + Hosted at: https://github.com/sahib/glyr
*
* glyr is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* glyr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with glyr. If not, see <http://www.gnu.org/licenses/>.
**************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include "../../lib/glyr.h"
#include "../../lib/cache.h"
/*
* Example program showing how to use libglyr with multiple threads.
* libglyr is fully thread-compatible as long every thread uses it's own query.
*/
static void * call_get (void * p)
{
GlyrMemCache * r = glyr_get (p,NULL,NULL);
puts ("Thread finished");
return r;
}
static void configure (GlyrQuery * s, GLYR_GET_TYPE type)
{
glyr_query_init (s);
glyr_opt_artist (s,"Die Apokalyptischen Reiter");
glyr_opt_album (s,"Moral & Wahnsinn");
glyr_opt_title (s,"Die Boten");
glyr_opt_verbosity (s,1);
glyr_opt_type (s,type);
glyr_opt_from (s,NULL);
}
static void printme (GlyrMemCache * start)
{
GlyrMemCache * i = start;
while (i != NULL)
{
glyr_cache_write (i,"stdout");
i = i->next;
}
glyr_free_list (start);
}
int main (void)
{
/* Init */
glyr_init();
atexit (glyr_cleanup);
/* Fill some silly values */
GlyrQuery query_long, query_longer;
configure (&query_long,GLYR_GET_SIMILAR_ARTISTS);
configure (&query_longer,GLYR_GET_LYRICS);
GlyrDatabase * db = glyr_db_init ("/tmp");
glyr_opt_lookup_db (&query_long,db);
glyr_opt_lookup_db (&query_longer,db);
GlyrMemCache * a, * b;
/* Create two threads */
GThread * thread_a = g_thread_new ("call_get", call_get, (gpointer) &query_long);
GThread * thread_b = g_thread_new ("call_get", call_get, (gpointer) &query_longer);
puts ("Running!");
/* Wait for threads to join, get results */
a = g_thread_join (thread_a);
b = g_thread_join (thread_b);
puts ("Joined!\n---> Data: ");
if (a && b)
{
printme (a);
puts ("-------------");
printme (b);
}
glyr_query_destroy (&query_long);
glyr_query_destroy (&query_longer);
return EXIT_SUCCESS;
}