This is a followup to this question:
I guess I don't understand the Interlocked Acquire / Release APIs. I put together the small program below. As I understand it, g_val_1, g_val_2 and g_val_3 should always be updated in the same order and should end up as all the same value. But they do not (for more than one thread).
What am I missing? Thanks.
#include "windows.h"
#include "stdio.h"
#define _THREADS_ 100
#define _TICKS_ 1000
int volatile g_threads = 0;
DWORD volatile g_val_1 = 0;
DWORD volatile g_val_2 = 0;
DWORD volatile g_val_3 = 0;
BOOL g_running = TRUE;
DWORD TestThread(PVOID ignore)
{
while (g_running)
{
InterlockedIncrementAcquire(&g_val_1);
g_val_2++;
InterlockedIncrementRelease(&g_val_3);
}
InterlockedDecrement(&g_threads);
return(0);
}
int __cdecl main(int argc, char* argv[])
{
int th, duration;
int success;
DWORD ticks;
duration = _TICKS_;
g_threads = _THREADS_;
printf("Max=%d Threads=%d Entries=%d\n", duration, g_threads);
printf("Creating Threads\n");
th = g_threads;
while (th-- > 0)
{
CreateThread(NULL,
0,
TestThread,
NULL,
NORMAL_PRIORITY_CLASS,
NULL);
}
printf("Starting Threads\n");
ticks = GetTickCount();
while ((GetTickCount() - ticks) < duration);
g_running = FALSE;
while (g_threads > 0);
ticks = GetTickCount() - ticks;
success = ((g_val_1 == g_val_2) && (g_val_1 == g_val_2));
printf("Duration=%d g_val_1=%d g_val_2=%d g_val_3=%d OK=%d\n", ticks, g_val_1, g_val_2, g_val_3, success);
}
Related
I am studying the Thread Ordering Service of Windows. So, I have written the following code to test how TOS works really? But it doesn't work and it gives me an error.
#include <windows.h>
#include <process.h>
#include <iostream>
#include <avrt.h>
#include <stdio.h>
#pragma comment(lib, "Avrt.lib")
#define _100NS_IN_1MS 10000
unsigned __stdcall Thread1(void* arg_list)
{
for (size_t i = 0; i < 100; i++)
{
std::cout << "\tHello from Thread1, Repeat #" << i + 1 << std::endl;
SwitchToThread();
}
return 0;
}
unsigned __stdcall Thread2(void* arg_list)
{
for (size_t i = 0; i < 100; i++)
{
std::cout << "\tHello from Thread2, Repeat #" << i + 1 << std::endl;
}
return 0;
}
int main()
{
HANDLE handle_context = NULL;
LARGE_INTEGER period, timeout;
GUID guid = { 0 };
BOOL result;
period.QuadPart = Int32x32To64(_100NS_IN_1MS, 1000); // 1 second
timeout.QuadPart = Int32x32To64(_100NS_IN_1MS, 10000); // 10 seconds
result = AvRtCreateThreadOrderingGroup(&handle_context, &period, &guid, &timeout);
if (result != TRUE)
{
printf("Error creating group (%d)\n", GetLastError());
return 1;
}
HANDLE handle_thread1 = (HANDLE)_beginthreadex(NULL, 0, Thread1, NULL, CREATE_SUSPENDED, NULL);
HANDLE handle_thread2 = (HANDLE)_beginthreadex(NULL, 0, Thread2, NULL, CREATE_SUSPENDED, NULL);
AvRtJoinThreadOrderingGroup(&handle_thread1, &guid, FALSE);
AvRtJoinThreadOrderingGroup(&handle_thread2, &guid, FALSE);
if (handle_thread1 == NULL && handle_thread2 == NULL)
{
return -1;
}
ResumeThread(handle_thread1);
ResumeThread(handle_thread2);
int recieve1 = WaitForSingleObject(handle_thread1, INFINITE);
int recieve2 = WaitForSingleObject(handle_thread2, INFINITE);
CloseHandle(handle_thread1);
CloseHandle(handle_thread2);
}
When I run the above program, it gives me the following error:
Error creating group (1058)
1058 (0x422)
The service cannot be started, either because it is disabled or
because it has no enabled devices associated with it.
What should I do now? I am using Windows 10.
The links here and here, explain that Windows Thread Ordering Service is not on by default:
The thread ordering service is off by default and must be started by
the user. While the thread ordering service is running, it is
activated every 5 seconds to check whether there is a new request,
even if the system is idle. This prevents the system from sleeping for
longer than 5 seconds, causing the system to consume more power. If
energy efficiency is critical to the application, it is better not to
use the thread ordering service and instead allow the system scheduler
to manage execution of threads.
Use cases and other discussion are also discussed and code snippets are provided in some of the other links (see 2nd link above.) Following is an example:
#include <windows.h>
#include <avrt.h>
#include <stdio.h>
#pragma comment(lib, "Avrt.lib")
#define _100NS_IN_1MS 10000
int main( void )
{
HANDLE hContext = NULL;
LARGE_INTEGER period, timeout;
GUID guid = { 0 };
BOOL bRes;
period.QuadPart = Int32x32To64(_100NS_IN_1MS, 1000); // 1 second
timeout.QuadPart = Int32x32To64(_100NS_IN_1MS, 10000); // 10 seconds
bRes = AvRtCreateThreadOrderingGroup(
&hContext,
&period,
&guid,
&timeout );
if( bRes != TRUE )
{
printf("Error creating group (%d)\n", GetLastError());
return 1;
}
return 0;
}
I'm just beginning with directx/directinput development and I'm running some tests with some code samples I've found online. Anyway, I want to hook an application that uses dinput8 to send my own custom input to the forewindow and I'm working with this base to do it:
// dllmain.cpp : Defines the entry point for the DLL application.
#define _CRT_SECURE_NO_WARNINGS // ignore some warnings...
#define _CRT_NON_CONFORMING_SWPRINTFS // ...
#include "stdio.h"
#include <windows.h>
#include "detours.h"
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <time.h>
#include "dinput.h"
#pragma comment(lib, "detours.lib")
#pragma comment(lib, "user32.lib")
typedef HRESULT(__stdcall* GetDeviceState_t)(LPDIRECTINPUTDEVICE, DWORD, LPVOID *);
HRESULT __stdcall hkGetDeviceState(LPDIRECTINPUTDEVICE pDevice, DWORD cbData, LPVOID *lpvData);
DWORD Base = 0;
DWORD GetDeviceStateOffset = 0x7670; // This is the offset of GetDeviceState from DInput8.dll
// Open IDA and Import the DInput8.dll, then look in the Functions Table for DirectInput8Create
// There is an Address (1000XXXX or 0CXXXXX) - copy it and save it for later
// Then take a look for CDIDev_GetDeviceState and copy that address too
// Now substract the Address from CDIDev_GetDeviceState from DIrectInput8Create and u'll get your offset
HANDLE tmpHandle = NULL;
HMODULE hModDInput8 = NULL;
DWORD dwGetDeviceState = NULL;
FARPROC dwDirectInput8Create = NULL;
struct MyKeys
{
BYTE Key;
DWORD StartTime;
DWORD TTL;
BOOLEAN isDown;
};
MyKeys KeyBuffer[256];
DWORD WINAPI HookThread();
void add_log(char* format, ...);
void SendKeyDInput(byte DIK_, DWORD time);
GetDeviceState_t pGetDeviceState;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
add_log("==========LOG START==========");
add_log("DLL Attached");
add_log("Creating Thread...");
tmpHandle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&HookThread, 0, 0, 0);
if (!tmpHandle)
{
add_log("ThreadCreation Failed!");
}
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
DWORD WINAPI HookThread()
{
Base = (DWORD)GetModuleHandleA("test.exe");
add_log("Thread Created");
add_log("game.exe Base: %x", Base);
while (!hModDInput8)
{
add_log("Searching dinput8.dll...");
hModDInput8 = GetModuleHandle(L"dinput8.dll");
Sleep(100);
}
add_log("Found dinput8.dll: %x !", hModDInput8);
while (!dwDirectInput8Create)
{
add_log("Searching GetDeviceState...");
dwDirectInput8Create = GetProcAddress(hModDInput8, "DirectInput8Create");
Sleep(100);
}
add_log("Found DirectInput8Create: %x !", dwDirectInput8Create);
dwGetDeviceState = (DWORD)((DWORD)dwDirectInput8Create - GetDeviceStateOffset);
add_log("GetDevicestate is here (DirectInput8Create - %x): %x", GetDeviceStateOffset, dwGetDeviceState);
add_log("Hooking GetDeviceState...");
pGetDeviceState = (GetDeviceState_t)DetourAttach(&(PVOID&)dwGetDeviceState, (PBYTE)hkGetDeviceState);
add_log("Initiate Keyboard Buffer...");
//initiate buffer
for (int i = 0; i < 256; i++)
{
KeyBuffer[i].isDown = false;
KeyBuffer[i].Key = 0;
KeyBuffer[i].StartTime = 0;
KeyBuffer[i].TTL = 0;
}
add_log("Going into Main Loop...");
while (true)
{
if (GetAsyncKeyState(VK_F5) & 1 << 15)
{
// We check the Most Sigificant Bit from VK_F5 (F5) whilst we shifted it with 15 bits to left 1
// and then a small delay so we have enaught time to release the key
add_log("F5 pushed attempting to sendkey");
// Sleep a short time so we have time to release the F5 Key
Sleep(500);
// Now we send a A Key with 1 sec time to our Game
SendKeyDInput(DIK_A, 1000);
}
}
return 0;
}
void SendKeyDInput(byte DIK, DWORD time)
{
KeyBuffer[DIK].Key = DIK;
KeyBuffer[DIK].TTL = time;
KeyBuffer[DIK].StartTime = GetTickCount();
}
HRESULT __stdcall hkGetDeviceState(LPDIRECTINPUTDEVICE lpDevice, DWORD cbData, LPVOID *lpvData)
{
HRESULT hResult = DI_OK;
static BYTE buffer[256];
int key_count = 0;
for (int i = 0; i<256; i++)
{
if (KeyBuffer[i].Key != 0 && KeyBuffer[i].TTL>0 && KeyBuffer[i].StartTime != 0)
{
if (GetTickCount() > KeyBuffer[i].StartTime + KeyBuffer[i].TTL && KeyBuffer[i].isDown)
{
KeyBuffer[i].Key = 0;
KeyBuffer[i].StartTime = 0;
KeyBuffer[i].TTL = 0;
KeyBuffer[i].isDown = false;
buffer[KeyBuffer[i].Key] = 0;
}
else {
KeyBuffer[i].isDown = true;
buffer[KeyBuffer[i].Key] = 0x80;
key_count += 1;
add_log("Sending Key %x for %i milliseconds count: %i", KeyBuffer[i].Key, KeyBuffer[i].TTL, key_count);
}
}
}
if (key_count != 0)
{
cbData = 256;
memcpy(lpvData, buffer, cbData);
}
else {
hResult = pGetDeviceState(lpDevice, cbData, lpvData);
}
return hResult;
}
//Creates a Logfile in the Game Directory
void add_log(char* format, ...)
{
HANDLE filehandle;
DWORD dwReadBytes;
char buffer[2048];
char writebuffer[2048];
va_list args;
va_start(args, format);
vsprintf_s(buffer, format, args);
filehandle = CreateFile(L"Log.txt", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0);
SetFilePointer(filehandle, 0, 0, FILE_END);
sprintf_s(writebuffer, 2048, "Log Added: %s\r\n", buffer);
WriteFile(filehandle, writebuffer, strlen(writebuffer), &dwReadBytes, 0);
CloseHandle(filehandle);
}
The only issue in this code is when I attempt to send input, it doesn't go through. I've gotten some help and narrowed down a solution to this, which was: "Try GetDeviceState hk just memset(buffer, 0, size) or SendDeviceData". I've searched around a bit and I've been unable to find more on how to implement this solution and I'm stumped.
Could one of you kind people show me how I could use this information to fix this base? I'd be extremely grateful, thanks.
I modified the "Using Condition Variables" example in MSDN. I created several threads, which have their own producer/consumer separately.
But the program will often hang when attached to debugger. It always hangs at SleepConditionVariableCS. When I break all and continue in debugger, the program will continue to run. I found that WakeAllConditionVariable didn't wake up some thread calls SleepConditionVariableCS, for the Ptr in PExecutingTask->BufferNotFull or PExecutingTask->BufferNotEmpty was already 0x00000000(I thought that means the condition variable is wakened).
When not attached to debugger, the program will not hang.
Has anyone encountered this problem before? How to solve it?
Here is the code:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFFER_SIZE 100
DWORD WINAPI ProducerThreadProc (PVOID p);
DWORD WINAPI ConsumerThreadProc (PVOID p);
class ExecutingTask
{
public:
void Initialize()
{
InitializeConditionVariable (&BufferNotEmpty);
InitializeConditionVariable (&BufferNotFull);
InitializeCriticalSection (&BufferLock);
QueueSize = 0;
StopRequested = FALSE;
hProducer = CreateThread(NULL, 0, ProducerThreadProc, (PVOID)this, 0, NULL);
for (int i = 0; i < 20; i++)
{
PhConsumers[i] = CreateThread (NULL, 0, ConsumerThreadProc, (PVOID)this, 0, NULL);
}
}
void Stop()
{
EnterCriticalSection (&BufferLock);
StopRequested = TRUE;
LeaveCriticalSection (&BufferLock);
WakeAllConditionVariable (&BufferNotFull);
WakeAllConditionVariable (&BufferNotEmpty);
WaitForSingleObject(hProducer, INFINITE);
WaitForMultipleObjects(20, (HANDLE *)&PhConsumers, true, INFINITE);
CloseHandle(hProducer);
for (unsigned int i = 0; i < 20; i++)
{
CloseHandle(PhConsumers[i]);
}
}
public:
ULONG QueueSize;
CONDITION_VARIABLE BufferNotEmpty;
CONDITION_VARIABLE BufferNotFull;
CRITICAL_SECTION BufferLock;
BOOL StopRequested;
HANDLE PhConsumers[20];
HANDLE hProducer;
};
DWORD WINAPI ProducerThreadProc (PVOID p)
{
ExecutingTask* PExecutingTask = (ExecutingTask*)p;
while (true)
{
EnterCriticalSection (&PExecutingTask->BufferLock);
while (PExecutingTask->QueueSize >= BUFFER_SIZE && PExecutingTask->StopRequested == FALSE)
{
// Buffer is full - sleep so consumers can get items.
SleepConditionVariableCS (&PExecutingTask->BufferNotFull, &PExecutingTask->BufferLock, INFINITE);
}
if (PExecutingTask->StopRequested == TRUE)
{
LeaveCriticalSection (&PExecutingTask->BufferLock);
break;
}
// Produce an item.
PExecutingTask->QueueSize++;
LeaveCriticalSection (&PExecutingTask->BufferLock);
WakeConditionVariable (&PExecutingTask->BufferNotEmpty);
}
return 0;
}
DWORD WINAPI ConsumerThreadProc (PVOID p)
{
ExecutingTask* PExecutingTask = (ExecutingTask*)p;
while (true)
{
EnterCriticalSection (&PExecutingTask->BufferLock);
while (PExecutingTask->QueueSize == 0 && PExecutingTask->StopRequested == FALSE)
{
// Buffer is empty - sleep so producers can create items.
SleepConditionVariableCS (&PExecutingTask->BufferNotEmpty, &PExecutingTask->BufferLock, INFINITE);
}
if (PExecutingTask->StopRequested == TRUE)
{
LeaveCriticalSection (&PExecutingTask->BufferLock);
break;
}
// Consume an item.
PExecutingTask->QueueSize--;
LeaveCriticalSection (&PExecutingTask->BufferLock);
WakeConditionVariable (&PExecutingTask->BufferNotFull);
}
return 0;
}
DWORD WINAPI ThreadProc (PVOID p)
{
ExecutingTask task;
task.Initialize();
Sleep(1000);
task.Stop();
printf ("%u exit\n", &task);
return 0;
};
int main ( void )
{
HANDLE hTaskThreads[50];
for (int i = 0; i < 50; i++)
{
hTaskThreads[i] = CreateThread (NULL, 0, ThreadProc, NULL, 0, NULL);
}
WaitForMultipleObjects(50, hTaskThreads, true, INFINITE);
for (int i = 0; i < 50; i++)
{
CloseHandle(hTaskThreads[i]);
}
}
I need 2 threads: TC and TS, such that they are composed in two main sections each accessing shared data and the threads must to be synchronized. The synchronization should be like this:
The red codes are working with shared data U, but get U can be placed before the dashed rectangle on the TS thread, riht side.
The Xcurrent can be in TC or in TS the tasks, but is a shared hardware with send Ucurrent and get must be right after send was finished.
A dead lock appears and I can't figure out an elegant solution.
Thread TS:
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand */
#define _USE_MATH_DEFINES
#include <math.h>
#include <windows.h>
#include <pthread.h>
#include <time.h>
// CRLT-C var
static int stop = 0;
// TIMEING vars
__int64 frequencyT, startT, endT = 0;
double baseAngleLast;
double pendulAngleLast;
// THREADING vars
bool startedS = false, Usent = false;
double * Ucmd;
pthread_mutex_t mutexU = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutexBoard = PTHREAD_MUTEX_INITIALIZER;
unsigned int Ulenght = 6;
pthread_cond_t signal_to_read_state = PTHREAD_COND_INITIALIZER;
pthread_cond_t signal_to_read_cmd = PTHREAD_COND_INITIALIZER;
DWORD sleepTime = 30; // in milliseconds
int currentState = 3;
void *sendingCMD(void * )
{
double * U_tmp;
U_tmp = (double*)malloc(sizeof(double)*Ulenght);
startedS = true;
printf("Sin\n");
while (stop == 0)
{
printf("Smu-\n");
//get U
pthread_mutex_lock( &mutexU );
printf("Smu..\n");
pthread_cond_wait(&signal_to_read_cmd, &mutexU);
memcpy(U_tmp,Ucmd,sizeof(double)*Ulenght);
pthread_mutex_unlock( &mutexU );
printf("Smu+\n");
pthread_mutex_lock( &mutexBoard );
for (unsigned int i = 0; i<Ulenght; i++)
{
//send CMD signal
printf("%f ", U_tmp[i]);
if (i == Ulenght -1) printf("\n");
}
Sleep(sleepTime); // miliseconds
currentState = currentState + 1;
pthread_cond_signal(&signal_to_read_state);
pthread_mutex_unlock( &mutexBoard );
printf("Smb\n");
}
printf("Task S terminated. \n");
return (NULL);
}
void *computingU(void *)
{
double * U_tmp;
U_tmp = (double*)malloc(sizeof(double)*Ulenght);
int currentStateTMP =0;
bool fisrtLoop = true;
printf("Uin\n");
while (stop == 0)
{
printf("Umb- \n");
// get current state
pthread_mutex_lock( &mutexBoard );
if (!fisrtLoop)
{
printf("UmbFalse \n");
pthread_cond_wait(&signal_to_read_state, &mutexBoard);
}
else
{
printf("UmbTrue \n");
fisrtLoop=false;
}
currentStateTMP =currentState;
pthread_mutex_unlock( &mutexBoard );
printf("Umb+ \n");
pthread_mutex_lock( &mutexU );
for (unsigned int i=0;i<Ulenght;i++)
{
Ucmd[i] = Ucmd[i]+ (double)currentStateTMP/i;
}
pthread_cond_signal(&signal_to_read_cmd);
pthread_mutex_unlock( &mutexU );
printf("Umu\n");
}
return (NULL);
}
void signal_handler(int signal)
{
stop = 1;
}
int main(int argc, char* argv[])
{
//initializing output buffer to 0[V]
Ucmd= (double*)malloc(sizeof(double)*Ulenght);
for (unsigned int i=0;i<Ulenght;i++)
Ucmd[i] = 0;
//init threads
int rc1, rc2;
pthread_t threadU, threadS;
/* Create independent threads each of which will execute functionC */
if( (rc1=pthread_create( &threadU, NULL, &computingU, NULL)) ) {
printf("ThreadU creation failed: %d\n", rc1);
}
if( (rc2=pthread_create( &threadS, NULL, &sendingCMD, NULL)) )
{
printf("ThreadS creation failed: %d\n", rc2);
}
while (stop == 0);
printf("Main terminated, closing board in 10ms. \n");
Sleep(10);
return 0;
}
The blocking appears at:
TC at pthread_cond_wait(&signal_to_read_state, &mutexBoard);
TS at pthread_cond_wait(&signal_to_read_cmd, &mutexU);
btw why dose not recognize stackoverflow the code segment I pasted above in case i copy paste from a VS2010?
Hey, I'm Trying to make a programa in C++ that generate triangle, square and sine, waves. I enter the frequence, amplitude, etc, and it calculates the average of the wave. And i cah choose what wave generate by selecting a radio button.
This is a real-time system, so, if a wave is being plotted and if I choose a radio buton correspondent to another type of wave, it shall change in real time.
The error is this:
[C++ Error] FormularioPrincipal.cpp(171): E2297 'this' can only be used within a member function
It happens on the line that I'm creating the thread. Can someone help me?
Thanks!
//---------------------------------------------------------------------------
#include <vcl.h>
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <limits>
#include <complex>
#include <math.h>
#pragma hdrstop
#include "FormularioPrincipal.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE thread1;
HANDLE thread2;
HANDLE thread3;
HANDLE thread4;
HANDLE thread5;
HANDLE Mutex;
int pipe[2];
double freq;
double per;
double freqAngular;
double pi;
double taxaAmostragem;
double tempofinal;
double deslocamento;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
DWORD WINAPI geraSinalSenoide(void *parametro){
TForm1 *FormularioPrincipal = (TForm1*) parametro;
int i;
double valorGerado;
per = StrToFloat(FormularioPrincipal->Edit2->Text);
freq = 1/per;
FormularioPrincipal->Edit3->Text = freq;
pi = 3.141592654;
taxaAmostragem = 20;
tempofinal = 1000;
freqAngular = (2 * pi)/per;
double amp = StrToFloat(FormularioPrincipal->Edit1->Text);
while(true){
for (double time=0; time<=freq; time = time + (1 / taxaAmostragem)){
WaitForSingleObject(Mutex, INFINITE);
ReleaseMutex(Mutex);
deslocamento = ( sin(freqAngular * time) * amp);
write(pipe[1], &deslocamento, sizeof(int));
FormularioPrincipal->Series1->AddXY(per, deslocamento, "", clRed);
per = per + StrToFloat(FormularioPrincipal->Edit2->Text);
//FormularioPrincipal->Label7->Caption = time;
}
Sleep(1000);
return 0;
}
};
DWORD WINAPI geraSinalQuadrado(void *parametro){
TForm1 *FormularioPrincipal = (TForm1*) parametro;
int i;
int x=0;
float valorGerado;
double per = StrToFloat(FormularioPrincipal->Edit6->Text);
freq = 1/per;
FormularioPrincipal->Edit2->Text = freq;
while(true){
for(i=0; i<freq;i++){
WaitForSingleObject(Mutex, INFINITE);
ReleaseMutex(Mutex);
valorGerado = rand() % (FormularioPrincipal->Edit1->Text);
write(pipe[1], &valorGerado, sizeof(int));
FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
per = per + StrToFloat(FormularioPrincipal->Edit6->Text);
FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
} Sleep(1000);
}
}
DWORD WINAPI geraSinalTriangular(void *parametro){
TForm1 *FormularioPrincipal = (TForm1*) parametro;
int i;
int x=0;
float valorGerado;
double per = StrToFloat(FormularioPrincipal->Edit6->Text);
freq = 1/per;
FormularioPrincipal->Edit2->Text = freq;
while(true){
for(i=0; i<freq;i++){
WaitForSingleObject(Mutex, INFINITE);
ReleaseMutex(Mutex);
valorGerado = rand() % (FormularioPrincipal->Edit1->Text);
write(pipe[1], &valorGerado, sizeof(int));
FormularioPrincipal->Series1->AddXY(per, valorGerado, "", clRed);
per = per + StrToFloat(FormularioPrincipal->Edit6->Text);
} Sleep(1000);
}
};
DWORD WINAPI processaNumeros(void *parametros){
TForm1* FormularioPrincipal = (TForm1*) parametros;
int dados[10];
int qtdDadosLidosBuffer=0;
float media = 0;
float soma =0;
float dif= 0;
while(true){
soma = 0;
dif = 0;
int i;
for(i=0; i<10; i++)//ele vai pegar de 10 em 10 numeros e calcular a media
{
int qtdBytesLidos;
//ler os dados do pipe
qtdBytesLidos = read(pipe[0], &dados[i], sizeof(int));
if (qtdBytesLidos == 0)
break;
}
qtdDadosLidosBuffer = i-1;
ReleaseMutex(Mutex);
for(int i=0; i<qtdDadosLidosBuffer;i++){
soma += dados[i];
}
if(qtdDadosLidosBuffer != 0)
soma/=qtdDadosLidosBuffer;//calcula a media
for(int i=0; i<qtdDadosLidosBuffer;i++){
dif = dados[i] - soma;
dif+=dif;
}
FormularioPrincipal->Edit4->Text = soma;
FormularioPrincipal->Edit5->Text = dif;
FormularioPrincipal->Edit6->Text = sqrt(dif);
Sleep(1000);//tbm espera 1 segundo
}
};
DWORD WINAPI main(void *parametro){
Mutex = CreateMutex(NULL, false, NULL);
TForm1 *FormularioPrincipal = (TForm1*) parametro;
DWORD prioridade;
prioridade = THREAD_PRIORITY_NORMAL;
if((FormularioPrincipal->RadioButton1->Checked == true) && (FormularioPrincipal->RadioButton2->Checked == false) && (FormularioPrincipal->RadioButton3->Checked == false)){
DWORD thread1ID;
thread1 = CreateThread(NULL, 0, geraSinalSenoide, this, CREATE_SUSPENDED, &thread1ID);
DWORD thread4ID;
thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);
GetExitCodeThread(thread2, &exitCode);
TerminateThread(thread2, exitCode);
GetExitCodeThread(thread3, &exitCode);
TerminateThread(thread3, exitCode);
SetThreadPriority(&thread1ID, prioridade);
}
else if((FormularioPrincipal->RadioButton1->Checked == false) && (FormularioPrincipal->RadioButton2->Checked == true) && (FormularioPrincipal->RadioButton3->Checked == false)){
DWORD thread2ID;
thread2 = CreateThread(NULL, 0, geraSinalTriangular, this, CREATE_SUSPENDED, &thread2ID);
DWORD thread4ID;
thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);
GetExitCodeThread(thread1, &exitCode);
TerminateThread(thread1, exitCode);
GetExitCodeThread(thread3, &exitCode);
TerminateThread(thread3, exitCode);
SetThreadPriority(&thread2ID, prioridade);
}
else{
DWORD thread3ID;
thread3 = CreateThread(NULL, 0, geraSinalQuadrado, this, CREATE_SUSPENDED, &thread3ID);
DWORD thread4ID;
thread4 = CreateThread(NULL, 0, processaNumeros, this, CREATE_SUSPENDED, &thread4ID);
GetExitCodeThread(thread1, &exitCode);
TerminateThread(thread1, exitCode);
GetExitCodeThread(thread2, &exitCode);
TerminateThread(thread2, exitCode);
SetThreadPriority(&thread3ID, prioridade);
}
if(_pipe(pipe, sizeof(int)*500, O_BINARY) == -1){//cria o pipe
MessageBox(NULL, "Erro ao criar pipe", "Aviso", 0);
return;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD prioridade;
prioridade = THREAD_PRIORITY_NORMAL;
DWORD thread5ID;
thread5 = CreateThread(NULL, 0, main, this, CREATE_SUSPENDED, &thread5ID);
SetThreadPriority(&thread5ID, prioridade);
ResumeThread(thread1);
ResumeThread(thread2);
ResumeThread(thread3);
ResumeThread(thread4);
ResumeThread(thread5);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
unsigned long exitCode;
GetExitCodeThread(thread1, &exitCode);
TerminateThread(thread1, exitCode);
GetExitCodeThread(thread2, &exitCode);
TerminateThread(thread2, exitCode);
GetExitCodeThread(thread3, &exitCode);
TerminateThread(thread3, exitCode);
GetExitCodeThread(thread4, &exitCode);
TerminateThread(thread4, exitCode);
GetExitCodeThread(thread5, &exitCode);
TerminateThread(thread5, exitCode);
}
//---------------------------------------------------------------------------
In DWORD WINAPI main(void *parametro) pass parametro instead of this to CreateThread since (like the error says) you're in free function and not in a member function.