Page 1 of 1
Threads sharing code
Posted: Mon Apr 20, 2009 2:18 am
by stanko51
Hello,
How can you implement 2 threads that will share executable code?
For example I load a first thread with a "main" and all different functions. Then I want to load a second thread with only the "main fonction" that will be able to use function defined in the first thread.
I'd like to do that using paging althought i realise it'd be easier with segmentation.
This would be some kind of "duplicating thread", but I'm not really sure where to start to implement it.
Thank you for your help.
Re: Threads sharing code
Posted: Mon Apr 20, 2009 2:46 am
by egos
Threads of one process run in same address space therefore they share code by default. Each thread must have its own stack.
Re: Threads sharing code
Posted: Mon Apr 20, 2009 3:12 am
by stanko51
Ok egos, thank you,
How to implement it, create new threads, handle switching between threads... ? Just some tips on where to start would be much appreciated ^^
Re: Threads sharing code
Posted: Mon Apr 20, 2009 3:21 am
by Hyperdrive
stanko51 wrote:How to implement it, create new threads, handle switching between threads... ? Just some tips on where to start would be much appreciated ^^
Basically it comes down to process switching. If you can do that, you can switch threads too. You can optimize it a bit: If you switch from a thread of process P to a thread of the same process P, you don't need to switch address spaces (because all threads of the same process use the same address space). That's mainly the idea behind having threads and not just plain processes.
--TS
Re: Threads sharing code
Posted: Mon Apr 20, 2009 4:17 am
by egos
stanko51 wrote:How to implement it, create new threads, handle switching between threads... ? Just some tips on where to start would be much appreciated ^^
Make double-direction ring queue where will be placed all running (not waiting) threads. And realize thread context switching on timer interrupts.
Re: Threads sharing code
Posted: Mon Apr 20, 2009 7:17 am
by jal
stanko51 wrote:How to implement it, create new threads, handle switching between threads... ?
Check the wiki, perhaps?
JAL