Hi all , I am having problem about killing multiple treads in my program at the same time, here is my code:

int main()
{
while(1)
{
i++;
HANDLE thread =(HANDLE)_beginthread(TheThread, 0, NULL);
if(i==10)
break;
}

Sleep(3000);

TerminateThread(thread,0);

return 0;
}

now "TerminateThread(thread,0);" will only terminate the 10th thread , but how i will be able to terminate the 10 threads at once, also i don't want to create a new variable for new threads and terminate them like:

HANDLE a,b,c;
a =(HANDLE)_beginthread(TheThread, 0, NULL);
b =(HANDLE)_beginthread(TheThread2, 0, NULL);
c =(HANDLE)_beginthread(TheThread3, 0, NULL);

Sleep(3000);

TerminateThread(a,0);
TerminateThread(b,0);
TerminateThread(c,0);

as it will be hard to write the program if i want to create 300 threads at once, so please help me :(


Thanks in advance

Dani AI

Generated

If the goal is to stop a whole batch of worker threads cleanly, two separate issues in the thread above are the likely culprits: an out‑of‑range index when you store/terminate handles (the array size vs loop bounds), and sharing a single buffer for per‑thread data so threads race on the same memory. was right to suggest keeping the handles in a collection, and s crash is most commonly caused by those two mistakes rather than by some magic bug in the OS.

Avoid TerminateThread. It is inherently unsafe (it can leak locks, skip DLL/thread cleanup, corrupt global state) — use a cooperative shutdown instead. See the Microsoft notes on TerminateThread for details: TerminateThread documentation.

Two practical approaches:

  • Modern C++: use std::thread + std::vector and a shared stop flag (std::atomic<bool>) or condition variable. Each thread must receive its own copy of the work item (don’t reuse a single global char buffer). Signal the flag and join all threads. Example pattern:

    std::atomic<bool> stop{false};
    std::vector<std::thread> threads;
    for (auto item : workList) {
    threads.emplace_back([item, &stop](){
      while (!stop) { /* do work, check stop periodically */ }
    });
    }
    stop = true;
    for (auto &t : threads) if (t.joinable()) t.join();
  • Win32 handles: if you must use CRT thread APIs, prefer _beginthreadex (gives a waitable HANDLE) and close each handle when done; wait on handles with WaitForMultipleObjects (the API limit is MAXIMUM_WAIT_OBJECTS — see WaitForMultipleObjects). For >64 handles, wait in batches or use threadpool/register-wait helpers.

Final checklist: use < (not <=) when indexing arrays, give every thread its own copy of input data, prefer cooperative shutdown and join/wait, and close handles when you’re done. These changes will stop the crashes and avoid unsafe TerminateThread behavior.

Recommended Answers

All 4 Replies

create an array of HANDLE objects so that you can put the code in a loop.

const int MaxThreads = 20;
HANDLE hThreads[MaxThreads];

for(int i = 0; i < MaxThreads; i++)
   hThreads[i] = (HANDLE)_beginthread(TheThread, 0, NULL);

...

for(int i = 0; i < MaxThreads; i++)
   TerminateThread(hThreads[i],0);

create an array of HANDLE objects so that you can put the code in a loop.

const int MaxThreads = 20;
HANDLE hThreads[MaxThreads];

for(int i = 0; i < MaxThreads; i++)
   hThreads[i] = (HANDLE)_beginthread(TheThread, 0, NULL);

...

for(int i = 0; i < MaxThreads; i++)
   TerminateThread(hThreads[i],0);

THANKS!!!!! :twisted:

I have slightly modify the terminate thread program and used it in my proxy checking program, but its crashing after creating and terminating 1st set of threads , I cant figure out whats the problem :( , here is my code:

char zz[4000];

int main()
{
int i=0;
const int MaxThreads = 40;
HANDLE hThreads[MaxThreads];
string line;

   ifstream infile("proxy.txt");
   while ( getline(infile, line) )
   {
	   i++;

	   strcpy(zz,line.c_str());

	   hThreads[i] =(HANDLE)_beginthread(TheThread, 0, NULL);
	   cout<<hThreads[i]<<endl;

	   if (i==MaxThreads)
	   {
		   cout<<i<<endl;
		   i=0;
		   Sleep(15000);
		 
		   for(int x = 0; x <=MaxThreads; x++)
   TerminateThread(hThreads[x],0);


	   }
	   
	   	 
	   
	   cout<<zz<<endl;
Sleep(10);

   }

    Sleep(15000);
   		   for(int x = 0; x <=MaxThreads; x++)
   TerminateThread(hThreads[x],0);
   MessageBoxA(NULL,"complete","complete",NULL);
   return 0;
}

please help me to figure out the problem :)

Thanks in advance

line 13: you are incrementing i too soon which is causing theThreads[0] to be an uninitialized variable. Move that line down to line 19.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.