Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
eecs398-search
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
vcday
eecs398-search
Commits
5d2dea33
Commit
5d2dea33
authored
7 years ago
by
benbergk
Browse files
Options
Downloads
Patches
Plain Diff
Created ProducerConsumerQueue
parent
661c6b46
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
ProducerConsumerQueue.cpp
+42
-0
42 additions, 0 deletions
ProducerConsumerQueue.cpp
ProducerConsumerQueue.h
+20
-1
20 additions, 1 deletion
ProducerConsumerQueue.h
ProducerConsumerQueue_test.cpp
+73
-1
73 additions, 1 deletion
ProducerConsumerQueue_test.cpp
with
135 additions
and
2 deletions
ProducerConsumerQueue.cpp
+
42
−
0
View file @
5d2dea33
...
...
@@ -3,3 +3,45 @@
//
#include
"ProducerConsumerQueue.h"
template
<
class
T
>
void
ProducerConsumerQueue
<
T
>::
Push
(
T
obj
)
{
pthread_mutex_lock
(
&
m
);
queue
.
push
(
obj
);
if
(
queue
.
size
()
==
1
)
{
pthread_cond_signal
(
&
consumer_cv
);
}
pthread_mutex_unlock
(
&
m
);
}
template
<
class
T
>
T
ProducerConsumerQueue
<
T
>::
Pop
()
{
pthread_mutex_lock
(
&
m
);
while
(
queue
.
empty
()
==
true
)
{
pthread_cond_wait
(
&
consumer_cv
,
&
m
);
}
T
front
=
queue
.
front
();
queue
.
pop
();
pthread_mutex_unlock
(
&
m
);
return
front
;
}
template
<
class
T
>
size_t
ProducerConsumerQueue
<
T
>::
Size
()
{
pthread_mutex_lock
(
&
m
);
size_t
size
=
queue
.
size
();
pthread_mutex_unlock
(
&
m
);
return
size
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
ProducerConsumerQueue.h
+
20
−
1
View file @
5d2dea33
...
...
@@ -5,8 +5,27 @@
#ifndef EECS398_SEARCH_PRODUCERCONSUMERQUEUE_H
#define EECS398_SEARCH_PRODUCERCONSUMERQUEUE_H
#include
<queue>
#include
<pthread.h>
class
ProducerConsumerQueue
{
//for now use STL queue, create better one later
template
<
class
T
>
class
ProducerConsumerQueue
{
private:
std
::
queue
<
T
>
queue
;
pthread_mutex_t
m
=
PTHREAD_MUTEX_INITIALIZER
;
pthread_cond_t
consumer_cv
=
PTHREAD_COND_INITIALIZER
;
public:
ProducerConsumerQueue
()
{}
void
Push
(
T
obj
);
T
Pop
();
size_t
Size
();
//Right now these pass objects by value but
// probably should pass pointers in future
};
...
...
This diff is collapsed.
Click to expand it.
ProducerConsumerQueue_test.cpp
+
73
−
1
View file @
5d2dea33
//
//
/
// Created by Ben Bergkamp on 1/25/18.
//
#include
<iostream>
#include
<stdlib.h>
#include
<pthread.h>
#include
"ProducerConsumerQueue.h"
#include
"ProducerConsumerQueue.cpp"
// needed here because class is a template
pthread_mutex_t
cout_lock
=
PTHREAD_MUTEX_INITIALIZER
;
void
*
Producer
(
void
*
p
)
{
ProducerConsumerQueue
<
int
>
*
queue
=
(
ProducerConsumerQueue
<
int
>*
)
p
;
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
if
(
queue
->
Size
()){
pthread_yield_np
();
// let the consumer thread run
}
queue
->
Push
(
i
);
pthread_mutex_lock
(
&
cout_lock
);
std
::
cout
<<
"Pushed: "
<<
i
<<
"
\n
"
;
pthread_mutex_unlock
(
&
cout_lock
);
}
return
p
;
}
void
*
Consumer
(
void
*
p
)
{
ProducerConsumerQueue
<
int
>
*
queue
=
(
ProducerConsumerQueue
<
int
>*
)
p
;
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
int
r
=
queue
->
Pop
();
pthread_mutex_lock
(
&
cout_lock
);
std
::
cout
<<
"Popped: "
<<
r
<<
"
\n
"
;
pthread_mutex_unlock
(
&
cout_lock
);
}
return
p
;
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
ProducerConsumerQueue
<
int
>*
queue
=
new
(
ProducerConsumerQueue
<
int
>
);
pthread_t
producer
,
consumer
;
pthread_mutex_lock
(
&
cout_lock
);
std
::
cout
<<
"Creating Consumer
\n
"
;
pthread_mutex_unlock
(
&
cout_lock
);
pthread_create
(
&
consumer
,
NULL
,
Consumer
,
queue
);
pthread_mutex_lock
(
&
cout_lock
);
std
::
cout
<<
"Creating Producer
\n
"
;
pthread_mutex_unlock
(
&
cout_lock
);
pthread_create
(
&
producer
,
NULL
,
Producer
,
queue
);
pthread_mutex_lock
(
&
cout_lock
);
std
::
cout
<<
"Waiting for Producer and Consumer
\n
"
;
pthread_mutex_unlock
(
&
cout_lock
);
pthread_join
(
producer
,
NULL
);
pthread_join
(
consumer
,
NULL
);
delete
queue
;
return
0
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment