Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Differences between al_run_detached_thread() and al_start_thread()

Credits go to Arthur Kalliokoski, Elias, and Thomas Fjellstrom for helping out!
This thread is locked; no one can reply to it. rss feed Print
Differences between al_run_detached_thread() and al_start_thread()
AMCerasoli
Member #11,955
May 2010
avatar

Hi guys...

I have a question about some threading functions, yes again... :-[

  1. What is the difference from al_run_detached_thread() and al_start_thread()? The unique and obvious difference that comes to mind is the fact that with the first function you have no control over such thread, instead with the second one you have the ability to make it stop when you want. However both of them allows you you use mutexs and conds, isn't it?.


  2. When I call al_create_thread() the windows task manager shows that my game creates a new thread, and when I call al_start_thread() it creates another one, but then it disappears. But when I call al_destroy_thread() it only destroys one, but if I call again al_create_thread() and al_create_thread() again, it only creates one thread, and when I destroy it, again it only destroys one, so it doesn't look too bad, ex:

threads : 15

al_create_thread()
threads : 16
`
al_start_thread()
Threads : 17
Threads : 16

al_destroy_thread()
Threads : 16

al_create_thread()
al_start_thread()

Threads : 17
Threads : 16

So, all this crap is to know if I'm doing something bad, or if it is a normal behavior. In case I'm not doing something wrong, it's obvious that in my case would be better to use al_run_detached_thread(), since I don't need to close the thread for the main thread manually, am I right?.

Elias
Member #358
May 2000

I don't think al_create_thread and al_destroy_thread do anything but allocate/free the memory. al_start_thread will start the thread and it ends when you return from it. Would that explain the numbers you are seeing?

--
"Either help out or stop whining" - Evert

Thomas Fjellstrom
Member #476
June 2000
avatar

As far as I can tell, al_create_thread actually spawns the thread, and it sits waiting for a condition variable fired by al_start_thread, then it launches into the user proc. I'm not entirely sure why the thread api is set up that way, but it is.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

AMCerasoli
Member #11,955
May 2010
avatar

Well, probably it's the Task Manager not being precise, I thought it was a reliable tool. Here's a video showing the normal game.

video

And in this video some experiments that I can't understand.

video

PS: I'm not creating another threads besides this, in my game, so, I think it could be Allegro, or the Task Manager not being precise.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Have you tried Process Explorer?

They all watch too much MSNBC... they get ideas.

AMCerasoli
Member #11,955
May 2010
avatar

Wow, good tool.

It's showing the same information than the Task Manager, though. The game always starts with 15 threads and if I let it alone some time (maybe 5 min) drops to 12 threads, when I call al_create_thread() it creates another thread, the Process Explorer shows its Start Address is "msvcrt.dll!_endthreadedx+0x29".

But I have just realized that such thread also disappear after some minutes, so there is no problem. Thanks for the tool Arthur.

Thanks to all.

Go to: