-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_error.c
More file actions
73 lines (55 loc) · 1.74 KB
/
thread_error.c
File metadata and controls
73 lines (55 loc) · 1.74 KB
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
#include "thread_error.h"
void critical_error_exit(int error_code) {
}
#define THREAD_QUEUE_SIZE 10
typedef struct {
void (*function)(void*); // Function pointer for tasks
void* argument; // Arguments for the task
} Task;
// Task queue
thread_task taskQueue[THREAD_QUEUE_SIZE];
int queueStart = 0, queueEnd = 0, queueCount = 0;
// Synchronization
pthread_mutex_t queueMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t queueNotEmpty = PTHREAD_COND_INITIALIZER;
// Function to add a task to the queue
void addTask(void (*function)(void*), void* argument) {
pthread_mutex_lock(&queueMutex);
if (queueCount == THREAD_QUEUE_SIZE) {
usleep(100000);
} else {
taskQueue[queueEnd].function = function;
taskQueue[queueEnd].argument = argument;
queueEnd = (queueEnd + 1) % THREAD_QUEUE_SIZE;
queueCount++;
pthread_cond_signal(&queueNotEmpty); // Notify a thread
}
pthread_mutex_unlock(&queueMutex);
}
// Worker thread function
void* threadFunction(void* arg) {
char exit_thread = 1;
while (exit_thread) {
thread_task task;
pthread_mutex_lock(&queueMutex);
// Wait for a task
while (queueCount == 0) {
pthread_cond_wait(&queueNotEmpty, &queueMutex);
}
// Get the next task
task = taskQueue[queueStart];
queueStart = (queueStart + 1) % THREAD_QUEUE_SIZE;
queueCount--;
pthread_mutex_unlock(&queueMutex);
// Execute the task
task.function(task.argument);
}
return NULL;
}
// Example task function
void exampleTask(void* arg) {
int* num = (int*)arg;
printf("Processing task: %d\n", *num);
sleep(1); // Simulate work
free(num); // Free allocated memory
}