-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "mln_stack.h" | ||
#include <stdlib.h> | ||
#include <assert.h> | ||
|
||
typedef struct { | ||
void *data1; | ||
void *data2; | ||
} data_t; | ||
|
||
static void *copy(data_t *d, void *data) | ||
{ | ||
data_t *dup; | ||
assert((dup = (data_t *)malloc(sizeof(data_t))) != NULL); | ||
*dup = *d; | ||
return dup; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int i; | ||
data_t *d; | ||
mln_stack_t *st1, *st2; | ||
|
||
assert((st1 = mln_stack_init((stack_free)free, (stack_copy)copy)) != NULL); | ||
|
||
for (i = 0; i < 3; ++i) { | ||
assert((d = (data_t *)malloc(sizeof(data_t))) != NULL); | ||
assert(mln_stack_push(st1, d) == 0); | ||
} | ||
|
||
assert((st2 = mln_stack_dup(st1, NULL)) != NULL); | ||
|
||
assert((d = mln_stack_pop(st1)) != NULL); | ||
free(d); | ||
|
||
mln_stack_destroy(st1); | ||
mln_stack_destroy(st2); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include "mln_thread_pool.h" | ||
|
||
static int main_process_handler(void *data); | ||
static int child_process_handler(void *data); | ||
static void free_handler(void *data); | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
struct mln_thread_pool_attr tpattr; | ||
|
||
tpattr.main_data = NULL; | ||
tpattr.child_process_handler = child_process_handler; | ||
tpattr.main_process_handler = main_process_handler; | ||
tpattr.free_handler = free_handler; | ||
tpattr.cond_timeout = 1; | ||
tpattr.max = 1; | ||
tpattr.concurrency = 1; | ||
return mln_thread_pool_run(&tpattr); | ||
} | ||
|
||
static int child_process_handler(void *data) | ||
{ | ||
printf("%s\n", (char *)data); | ||
free(data); | ||
return 0; | ||
} | ||
|
||
static int main_process_handler(void *data) | ||
{ | ||
int n, i = 0; | ||
char *text; | ||
|
||
while (1) { | ||
if ((text = (char *)malloc(16)) == NULL) { | ||
return -1; | ||
} | ||
n = snprintf(text, 15, "hello world"); | ||
text[n] = 0; | ||
mln_thread_pool_resource_add(text); | ||
usleep(1000); | ||
|
||
if (++i >= 20) break; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static void free_handler(void *data) | ||
{ | ||
free(data); | ||
} | ||
|