Skip to content

Commit 50aeba9

Browse files
author
Christian Howe
committed
Initial commit
0 parents  commit 50aeba9

File tree

7 files changed

+515
-0
lines changed

7 files changed

+515
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build:
2+
gcc -g -lpthread -o test main.c message-queue.c
3+
4+
memcheck:
5+
valgrind --leak-check=full ./test
6+
7+
clean:
8+
rm test

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Multithreaded Test
2+
3+
This is my testbed for multithreaded classes. I've written a MessageQueue class
4+
that uses locking already. I use pthreads for everything, as any respectable
5+
unix programmer would. To see some examples for my method of writing C code that
6+
has object orientation, see
7+
[object-oriented-test](https://github.com/coderarity/object-oriented-test).
8+
9+
Examples are provided in main.c. I may expand this testbed as I add more classes
10+
for multithreaded communications.
11+
12+
## Build it
13+
14+
```
15+
make
16+
```
17+
18+
## Try it
19+
20+
```
21+
./test
22+
```
23+
24+
## Clean it
25+
26+
```
27+
make clean
28+
```
29+
30+
## Check it for memory leaks (requires valgrind)
31+
32+
```
33+
make memcheck
34+
```
35+
36+
# License
37+
38+
All code and documentation in this repository is released under the MIT license.
39+
See [license text](http://coderarity.mit-license.org/).
40+

error.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef ERROR_H
2+
#define ERROR_H
3+
4+
#include <errno.h>
5+
6+
#define ERROR_VAL 2 // 1 is used by bool, bool can't be negative, next is 2
7+
#define SUCCESS_VAL 0
8+
9+
#endif // ERROR_H

main.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <errno.h>
4+
5+
#include <pthread.h>
6+
7+
#include "message-queue.h"
8+
9+
messagequeue_t* threadQueue;
10+
11+
void* threadFunc (void* arg) {
12+
int i;
13+
message_t msg;
14+
// 1 extra loop to give a bit of time for main to catch up
15+
for (i = 0; i < 6; ++i) {
16+
sleep(1);
17+
if (MessageQueue.pop(threadQueue, &msg) == ERROR_VAL) {
18+
if (errno != ENOMSG) {
19+
perror("Popping from threadQueue failed");
20+
return (void*)EXIT_FAILURE;
21+
}
22+
else {
23+
continue;
24+
}
25+
}
26+
printf("value: %s\n", (const char*)msg.data);
27+
free(msg.data); // we malloc'd earlier, remember? always free your mallocs!
28+
}
29+
return (void*)EXIT_SUCCESS;
30+
}
31+
32+
int main (int argc, char* argv[]) {
33+
threadQueue = malloc(sizeof(messagequeue_t));
34+
35+
if (threadQueue == NULL) {
36+
errno = ENOMEM;
37+
perror("Allocating threadQueue failed");
38+
return EXIT_FAILURE;
39+
}
40+
41+
if (MessageQueue.new(threadQueue) == ERROR_VAL) {
42+
perror("Intializing threadQueue failed");
43+
return EXIT_FAILURE;
44+
}
45+
46+
pthread_t thread;
47+
if (pthread_create(&thread, NULL, threadFunc, NULL) != 0) {
48+
perror("Creating thread failed");
49+
return EXIT_FAILURE;
50+
}
51+
52+
int i;
53+
const char* text = "hello!";
54+
message_t msg;
55+
msg.type = 1;
56+
for (i = 0; i < 5; ++i) {
57+
sleep(1);
58+
msg.data = malloc(strlen(text)+1); // we malloc this! don't forget to free!
59+
60+
if (msg.data == NULL) {
61+
errno = ENOMEM;
62+
perror("Allocating message data failed");
63+
return EXIT_FAILURE;
64+
}
65+
66+
// Copy the data to prevent race conditions
67+
strcpy(msg.data, text);
68+
69+
if (MessageQueue.push(threadQueue, &msg) == ERROR_VAL) {
70+
perror("Pushing to threadQueue failed");
71+
return EXIT_FAILURE;
72+
}
73+
}
74+
75+
void* retValue;
76+
if ((errno = pthread_join(thread, &retValue)) != 0) {
77+
perror("Joining thread failed");
78+
return EXIT_FAILURE;
79+
}
80+
81+
if (MessageQueue.delete(threadQueue) == ERROR_VAL) {
82+
perror("Deleting threadQueue failed");
83+
return EXIT_FAILURE;
84+
}
85+
free(threadQueue);
86+
threadQueue = NULL;
87+
88+
if (retValue == (void*)EXIT_FAILURE)
89+
return EXIT_FAILURE;
90+
91+
return EXIT_SUCCESS;
92+
}

0 commit comments

Comments
 (0)