71 lines
1.4 KiB
C
71 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <ucore/threadqueue.h>
|
|
|
|
struct uc_threadqueue queue;
|
|
|
|
#define CNT 10000000
|
|
|
|
struct my_msg {
|
|
struct uc_threadmsg uc_msg;
|
|
int counter;
|
|
};
|
|
|
|
void *reader(void *arg)
|
|
{
|
|
int i;
|
|
int last;
|
|
for (i = 0; i < CNT/2; i++) {
|
|
struct uc_threadmsg *msg;
|
|
struct my_msg *my;
|
|
if (uc_thread_queue_get(&queue, &msg) != 0) {
|
|
printf("uc_threadqueue_get failed\n");
|
|
return NULL;
|
|
}
|
|
my = (struct my_msg*)msg;
|
|
last = my->counter;
|
|
free(my);
|
|
}
|
|
|
|
printf("reader finished, last = %d\n", last);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
pthread_t tid1,tid2;
|
|
int i;
|
|
|
|
uc_thread_queue_init(&queue, 100);
|
|
|
|
if (pthread_create(&tid1, NULL, reader, NULL) != 0) {
|
|
perror("pthread_create");
|
|
return 1;
|
|
}
|
|
|
|
if (pthread_create(&tid2, NULL, reader, NULL) != 0) {
|
|
perror("pthread_create");
|
|
return 1;
|
|
}
|
|
|
|
for (i = 0; i < CNT; i++) {
|
|
struct my_msg *msg = malloc(sizeof *msg);
|
|
if (msg == NULL) {
|
|
perror("malloc");
|
|
return 1;
|
|
}
|
|
msg->uc_msg.msgtype = 11;
|
|
msg->counter = i;
|
|
if (uc_thread_queue_add(&queue, &msg->uc_msg) != 0) {
|
|
printf("uc_threadqueue_add failed\n");
|
|
return 2;
|
|
}
|
|
}
|
|
printf("writer finished\n");
|
|
pthread_join(tid1, NULL);
|
|
pthread_join(tid2, NULL);
|
|
|
|
return 0;
|
|
}
|