A
A
Andrey Smirnov2016-12-18 19:48:02
C++ / C#
Andrey Smirnov, 2016-12-18 19:48:02

What is wrong with multithreading programming?

Good day forum users! I am writing with such a question: the task was set to write a simple program with multithreading. Those. I have two tasks and they are executed one after another, replacing each other after a certain time interval.
And the program works for a while, and then just stops and that's it. There are no errors or crashes. When debugging, the arrow disappears on one of the steps, I saw this for the first time.
I would be very grateful if you poke my nose into my jambs and generally explain what's what.
Thanks in advance.

Файл MultiTask.h
#pragma once
void InitManager();
int AddTask(void *Task);
int Task1(char*);
int Task2(char*);
void Manager();

Файл Test.cpp
#include "MultiTask.h"

void main() {
  InitManager();
  AddTask(Task1);
  AddTask(Task2);
  //AddTask(Task2);
  //AddTask(Task1);
  Manager();
}

Файл MultiTask.cpp
#include "MultiTask.h"

#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#define SIZE_OF_BUFFER 1000
#define TIME 200

int N = 0;

typedef struct Task {
  HANDLE hThread;
  int NumberFunction;
  void(*nameOfTask)(int);
  char buffer[SIZE_OF_BUFFER];
}Task;

Task* tasks[10];

void InitManager() {
}

int AddTask(void *task) {
  tasks[N] = (Task*)malloc(sizeof(Task));
  tasks[N]->NumberFunction = N;
  tasks[N]->nameOfTask = *(void(*)(int))task;
  tasks[N]->hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)tasks[N]->nameOfTask,
    tasks[N]->buffer, CREATE_SUSPENDED, NULL);
  for (int i = 0; i < SIZE_OF_BUFFER; i++)
  {
    tasks[N]->buffer[i] = 0;
  }
  N++;
  return 0;
}

int Task1(char* buffer) {
  int i = 0;
  for (i = *(int*)buffer; i < 200; i++) {
    printf("%d ", i);
    *(int*)buffer = i + 1;
    Sleep(50);
  }
  if (i == 200) buffer[4] = 1;
  return 1;
}

int Task2(char* buffer) {
  int i = 100;
  for (i -= *(int*)buffer; i > -1; i--) {
    printf("%d ", i);
    *(int*)buffer = 100 - (i - 1);
    Sleep(50);
  }
  if (i == -1) buffer[4] = 1;
  return 1;
}


void Manager() {
  int FLAG = N;
  int currentTask = 0;

  while (FLAG)
  {
    printf("\n Task: %d \n", tasks[currentTask]->NumberFunction);
    ResumeThread(tasks[currentTask]->hThread);
    Sleep(500);
    SuspendThread(tasks[currentTask]->hThread);

    if (currentTask == N - 1) currentTask = 0;
    else currentTask++;

    int tmp = 0;
    for (int i = 0; i < N; i++) {
      if (tasks[i]->buffer[4] == 0) {
        break;
      }
      if (i == N - 1) FLAG = 0;
    }
  }
  _getch();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Rulev, 2016-12-19
@megoduxa

The documentation for SuspendThread says that it is best used only by debuggers, not in real life.
In addition, SuspendThread/ResumeThread increment/decrement the counter, depending on whether the thread is running or not. Check what functions return, because if, for example, ResumeThread fails, you'll end up with a counter out of sync and never be able to wake it up again.
Perhaps the cycle in one of the tasks has ended and then the methods also do something absurd.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question