26
Начал я тут разбираться с потоками. Нужно мне запускать кучу коротких потоков, и не дожидаясь их окончания дальше продолжать программу, запуская по ходу другие потоки. И тут я наткнулся на то, что потоки вроде и завершаются, но не выгружаются из памяти. В результате течет память, а где-то на ~380-м создаваемом потоке функция pthread_create() возвращает ошибку 11. Вот минимальный пример. Компилирую командой g++ -L/usr/lib -x c++ -o sample sample.cpp -lm -lpthread Новый исправленный код, чтоб самому не забыть: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define SLEEP1 25000 #define SLEEP2 60000 static void *threadFunc(void *arg); int main (int argc, char *argv[]) { // Настройка атрибутов потока pthread_attr_t threadAttr; pthread_attr_init(&threadAttr); pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED); for(int iteration=1; iteration<1000000; iteration++) { pthread_t thread;

Начал я Тут Разбираться с Потоками

Embed Size (px)

Citation preview

. , , .

, , . , - ~380- pthread_create() 11.

.

g++ -L/usr/lib -x c++ -o sample sample.cpp -lm -lpthread

, :

#include

#include

#include

#include

#define SLEEP1 25000

#define SLEEP2 60000

static void *threadFunc(void *arg);

int main (int argc, char *argv[])

{

// pthread_attr_t threadAttr;

pthread_attr_init(&threadAttr);

pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);

for(int iteration=1; iteration { while (true);});

Console.WriteLine("Started task " + t.Id.ToString());

}

Task.Factory.StartNew(T, i + 1 + 5, TaskCreationOptions.AttachedToParent); //scheduled Task.Factory.StartNew(T, i + 2 + 5, TaskCreationOptions.AttachedToParent); //scheduled Task.Factory.StartNew(T, i + 3 + 5, TaskCreationOptions.AttachedToParent); //scheduled Task.Factory.StartNew(T, i + 4 + 5, TaskCreationOptions.AttachedToParent); //scheduled Task.Factory.StartNew(T, (i + 5 + 5).ToString(), TaskCreationOptions.AttachedToParent); //scheduled //BP4 - 1 in M, 2 in R, 3 in J, 4 in R, 5 died Debugger.Break();

}

else {

Debug.Assert((int)o == 4);

t3.Wait();

}

}

static void T(object o)

{

Console.WriteLine("Scheduled run " + Task.CurrentId.ToString());

}

static Task t1, t2, t3, t4;

static int aa = 0;

static int bb = 0;

static int cc = 0;

static bool waitFor1 = true;

static bool waitFor5 = true;

static int pcount;

static S mylock = new S();

}

1. .2. . , Debugger.Break(DebugBreak C++). , . 4 . " ". "" . .

1. . Visual Studio.2. . Visual Studio.3. , . . . " "

., . . .

, . , :A,BC. , , (4 ) , .

. , .

, ( , , , , ), . , . . "", , .

, . .

S.A, S.B S.C. , . S.D, S.E. S.F, S.G S.H. S.I S.J S.K, , . , , 1 2 . , . , . , . S.B, . . , . ., S.J, .

S.C . , . ( ) ( ). .

, 1 . . , 1 , , . . . , , . . , .

, . , . , .

1. , . , , . S.L, . S.L.

, S.L . , S.L, , , . .

, , . . , . . . . . , . , ( , ). , . . , . , , , .

2. , .

1. , . , . , . . . , , .

. : . , CTRL, . : "" "".

, .2. , . " " "" " " , .

1. .2. . Visual Studio.3. . Visual Studio.4. , . . . " ".5. . .

, , , , ( , ). , , , , . . , . , . . , . , , , . , - , .

, . , . . , ., 3 4.

4, , , , 2.

, . , . . , . , , . "".

, , "" . , , , , , . . , .

., AppDomain , .. .

, . 5, 4 . . , 4 5. "-", . , .

, 4 5 . , , . , ., . 4 5, . . , 4 5 .

S.P . "" . .

"" .

, . , . , .

5 , . , , F11. 3 4 .5 2 . , , . . . , . "" . , , , .

. . , .

. . . , , .

, .

. :, , , , , , ,, . , , . , .