threads written

This commit is contained in:
2016-03-05 09:23:42 +01:00
parent b5d649564d
commit ccc5014cb8
6 changed files with 298 additions and 222 deletions

View File

@ -111,6 +111,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.c" />
<ClCompile Include="threads.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pipe.h" />

View File

@ -18,6 +18,9 @@
<ClCompile Include="main.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="threads.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pipe.h">

View File

@ -97,90 +97,20 @@ void log(pipe_t * const source, pipe_t * const target, uint32_t element)
printf("%s -> %d -> %s(%d)\n", source->name, element, target->name, *((uint32_t*)target->state));
}
#define BUF_SIZE 255
void DisplayMessage(HANDLE hScreen,
char *ThreadName, int Data, int Count)
{
TCHAR msgBuf[BUF_SIZE];
size_t cchStringSize;
DWORD dwChars;
// Print message using thread-safe functions.
StringCchPrintf(msgBuf, BUF_SIZE,
TEXT("Executing iteration %02d of %s"
" having data = %02d \n"),
Count, ThreadName, Data);
StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
WriteConsole(hScreen, msgBuf, cchStringSize,
&dwChars, NULL);
}
DWORD WINAPI Thread_no_2(LPVOID lpParam)
{
int Data = 0;
int count = 0;
HANDLE hStdout = NULL;
// Get Handle To screen.
// Else how will we print?
if ((hStdout =
GetStdHandle(STD_OUTPUT_HANDLE))
== INVALID_HANDLE_VALUE)
return 1;
// Cast the parameter to the correct
// data type passed by callee i.e main() in our case.
Data = *((int*)lpParam);
for (count = 0; count <= 4; count++)
{
DisplayMessage(hStdout, "Thread_no_1", Data, count);
}
return 0;
}
DWORD WINAPI Thread_no_1(LPVOID lpParam)
{
int Data = 0;
int count = 0;
HANDLE hStdout = NULL;
// Get Handle To screen.
// Else how will we print?
if ((hStdout =
GetStdHandle(STD_OUTPUT_HANDLE))
== INVALID_HANDLE_VALUE)
return 1;
// Cast the parameter to the correct
// data type passed by callee i.e main() in our case.
Data = *((int*)lpParam);
for (count = 0; count <= 4; count++)
{
DisplayMessage(hStdout, "Thread_no_2", Data, count);
}
return 0;
}
int main(void)
extern void threads(void);
int main(int argc, char *argv[])
{
uint32_t counter = 0;
if (argc == 1)
{
/* Create pipes and connect them */
Pipe_Create(increment_pipe, 4, 1, NULL, NULL);
Pipe_Create(square_pipe, 4, 1, NULL, NULL);
Pipe_Create(increment_pipe, 4, 1, NULL, log);
Pipe_Create(square_pipe, 4, 1, NULL, log);
Pipe_Create(integrate_pipe, 8, 2, &counter, log);
Pipe_Create(sum_pipe, 8, 1, NULL, log);
Pipe_Create(average_pipe, 8, 1, NULL, log);
Pipe_Create(print_pipe, 4, 1, NULL, log);
Pipe_Create(sum_pipe, 8, 2, NULL, log);
Pipe_Create(average_pipe, 8, 2, NULL, log);
Pipe_Create(print_pipe, 4, 2, NULL, log);
Pipe_Connect(&increment_pipe, &integrate_pipe);
Pipe_Connect(&square_pipe, &integrate_pipe);
@ -189,27 +119,7 @@ int main(void)
Pipe_Connect(&sum_pipe, &print_pipe);
Pipe_Connect(&average_pipe, &print_pipe);
/* Create Threads */
int Data_Of_Thread_1 = 1;
int Data_Of_Thread_2 = 2;
HANDLE Handle_Of_Thread_1 = 0;
HANDLE Handle_Of_Thread_2 = 0;
Handle_Of_Thread_1 = CreateThread(NULL, 0, Thread_no_1, &Data_Of_Thread_1, 0, NULL);
if (Handle_Of_Thread_1 == NULL)
ExitProcess(Data_Of_Thread_1);
Handle_Of_Thread_2 = CreateThread(NULL, 0, Thread_no_2, &Data_Of_Thread_2, 0, NULL);
if (Handle_Of_Thread_2 == NULL)
ExitProcess(Data_Of_Thread_2);
HANDLE Array_Of_Thread_Handles[] = { Handle_Of_Thread_1, Handle_Of_Thread_1 };
WaitForMultipleObjects(3, Array_Of_Thread_Handles, TRUE, INFINITE);
CloseHandle(Handle_Of_Thread_1);
CloseHandle(Handle_Of_Thread_2);
/* Generate input */
/* Create Input */
Pipe_Insert(&increment_pipe, 1);
Pipe_Insert(&increment_pipe, 3);
Pipe_Insert(&increment_pipe, 5);
@ -227,6 +137,11 @@ int main(void)
print(&print_pipe);
getchar();
}
else
{
threads();
}
return 0;
}

View File

@ -9,9 +9,6 @@
#define inline __inline
#endif
/* number of connections */
#define PIPE_NUMBER_OF_CONNECTIONS 4
typedef struct pipe_tt
{
ringbuffer_t * input;

View File

@ -66,6 +66,7 @@ static inline uint32_t RingBuffer_Read(ringbuffer_t * const ring_buffer)
if (!RingBuffer_IsEmpty(ring_buffer))
{
element = *(ring_buffer->reader);
*(ring_buffer->reader) = 5;
ring_buffer->reader = RingBuffer_NextAddress(ring_buffer, ring_buffer->reader);
}

159
Pipe/threads.c Normal file
View File

@ -0,0 +1,159 @@
#include <stdio.h>
#include <stdint.h>
#include <strsafe.h>
#include <windows.h>
#include "ringbuffer.h"
#include "pipe.h"
/* Integrate every element of the signal. */
static void increment(pipe_t * const p)
{
while (Pipe_isFilled(p))
{
uint32_t item = Pipe_Read(p);
item++;
Pipe_Write(p, item);
}
}
/* Print the signal. */
static void write_to_file1(pipe_t * const pipe)
{
HANDLE hFile;
char DataBuffer[128];
DWORD dwBytesWritten = 0;
hFile = CreateFile("thread1.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
while (1) {
while (Pipe_isFilled(pipe))
{
sprintf_s(DataBuffer, 128, "%d\r\n", Pipe_Read(pipe));
WriteFile(hFile, DataBuffer, (DWORD)strlen(DataBuffer), &dwBytesWritten, NULL);
}
}
}
static void write_to_file2(pipe_t * const pipe)
{
HANDLE hFile;
char DataBuffer[128];
DWORD dwBytesWritten = 0;
hFile = CreateFile("thread2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
while (1) {
while (Pipe_isFilled(pipe))
{
sprintf_s(DataBuffer, 128, "%d\r\n", Pipe_Read(pipe));
WriteFile(hFile, DataBuffer, (DWORD)strlen(DataBuffer), &dwBytesWritten, NULL);
}
}
}
DWORD WINAPI incrementThread1(LPVOID lpParam)
{
pipe_t * pipe = (pipe_t *)lpParam;
LARGE_INTEGER time;
while (1)
{
/* Generate input */
QueryPerformanceCounter(&time);
Pipe_Insert(pipe, (uint32_t)time.u.LowPart);
QueryPerformanceCounter(&time);
Pipe_Insert(pipe, (uint32_t)time.u.LowPart);
QueryPerformanceCounter(&time);
Pipe_Insert(pipe, (uint32_t)time.u.LowPart);
increment(pipe);
}
return 0;
}
DWORD WINAPI incrementThread2(LPVOID lpParam)
{
pipe_t * pipe = (pipe_t *)lpParam;
LARGE_INTEGER time;
while (1)
{
/* Generate input */
QueryPerformanceCounter(&time);
Pipe_Insert(pipe, (uint32_t)time.u.LowPart);
QueryPerformanceCounter(&time);
Pipe_Insert(pipe, (uint32_t)time.u.LowPart);
QueryPerformanceCounter(&time);
Pipe_Insert(pipe, (uint32_t)time.u.LowPart);
increment(pipe);
}
return 0;
}
DWORD WINAPI incrementThread3(LPVOID lpParam)
{
pipe_t * pipe = (pipe_t *)lpParam;
while (1)
{
increment(pipe);
}
return 0;
}
DWORD WINAPI Thread2(LPVOID lpParam)
{
pipe_t * pipe = (pipe_t *)lpParam;
write_to_file2(pipe);
return 0;
}
DWORD WINAPI Thread1(LPVOID lpParam)
{
pipe_t * pipe = (pipe_t *)lpParam;
write_to_file1(pipe);
return 0;
}
static void log(pipe_t * const source, pipe_t * const target, uint32_t element) {}
void threads(void)
{
/* Create pipes and connect them */
Pipe_Create(increment_pipe1, 4, 1, NULL, log);
Pipe_Create(increment_pipe2, 4, 1, NULL, log);
Pipe_Create(increment_pipe3, 8, 2, NULL, log);
Pipe_Create(write_to_file1_pipe, 8, 1, NULL, log);
Pipe_Create(write_to_file2_pipe, 8, 1, NULL, log);
Pipe_Connect(&increment_pipe1, &increment_pipe3);
Pipe_Connect(&increment_pipe2, &increment_pipe3);
Pipe_Connect(&increment_pipe3, &write_to_file1_pipe);
Pipe_Connect(&increment_pipe3, &write_to_file2_pipe);
/* Create Threads */
CreateThread(NULL, 0, incrementThread1, &increment_pipe1, 0, NULL);
CreateThread(NULL, 0, incrementThread2, &increment_pipe2, 0, NULL);
CreateThread(NULL, 0, incrementThread3, &increment_pipe3, 0, NULL);
HANDLE thread1Handle, thread2Handle;
thread1Handle = CreateThread(NULL, 0, Thread1, &write_to_file1_pipe, 0, NULL);
thread2Handle = CreateThread(NULL, 0, Thread2, &write_to_file2_pipe, 0, NULL);
HANDLE threadHandles[] = { thread1Handle, thread2Handle };
WaitForMultipleObjects(2, threadHandles, TRUE, INFINITE);
CloseHandle(thread1Handle);
CloseHandle(thread2Handle);
return 0;
}