Re: spawninig of more than 8 threads problem

2016-08-10 Thread IronHawk
**@wiffel, @Araq:** Thanks a lot! I really missed that it's impossible to use spawn with closures. :( So the sample should look like below one. It's much more verbose, but it works properly. import unicode, locks, os proc lenU*(s: string): int = result = s.runeLen

Re: spawninig of more than 8 threads problem

2016-08-10 Thread IronHawk
**@euant:** > I believe the below code does the same thing as you're trying to achieve, > though I think I might be misunderstanding your aim - as it stands, there > seems to be no reason to use threads whatsoever? The idea was: check how easiy (or hard) is to write in nim the app calling pred

Re: spawninig of more than 8 threads problem

2016-08-09 Thread Araq
wiffel ist completely correct, and the compiler NEEDs to refuse this program. You cannot pass a .closure proc to spawn.

Re: spawninig of more than 8 threads problem

2016-08-09 Thread wiffel
In the original code, the **worker** proc is a _closure_. And **worker** does indeed access some of the variables in the surrounding context. I think that passing a _closure_ to **spawn** is not allowed. Since all threads are potentially running within a separate memoryspace, I'm not even sure h

Re: spawninig of more than 8 threads problem

2016-08-09 Thread euant
Hi, I believe the below code does the same thing as you're trying to achieve, though I think I might be misunderstanding your aim - as it stands, there seems to be no reason to use threads whatsoever? import unicode, threadpool, locks, os proc lenU*(s: string): int =

Re: spawninig of more than 8 threads problem

2016-08-09 Thread IronHawk
> Well, I'm done with ideas. If you want to investigate further, I guess gdb or > lldb would be able to help you see what's happening. Anyway thank you for this discussion! Yes I also have no ideas. The C code produced is quite big: 23982 lines compiled, hmm... And I'm unfamiliar with internals

Re: spawninig of more than 8 threads problem

2016-08-09 Thread flyx
Well, I'm done with ideas. If you want to investigate further, I guess gdb or lldb would be able to help you see what's happening.

Re: spawninig of more than 8 threads problem

2016-08-09 Thread IronHawk
Hi flux! Thank you for your interest to this problem. Well... All you write is almost perfect, but in practice if we execute 10-20 threads then scheduler wakes up these T1, T2, ... T19 in cycle and even if we don't sleep or sleep 1ms it would work. You are right that when scheduler tries to e

Re: spawninig of more than 8 threads problem

2016-08-09 Thread flyx
On second thought, the scheduler may be intelligent enough not to start more than one thread if it knows that they all wait for the same resource. But I wouldn't assume it.

Re: spawninig of more than 8 threads problem

2016-08-09 Thread flyx
Well it works for me with 32 characters in the string (Linux, 4 CPUs). So it seems to depend a bit on the OS - and therefore, the scheduler. > I think this shouldn't be the case. I'll try to explain what might happen. I do not know all internals involved, so it may or may not be accurate. Supp

Re: spawninig of more than 8 threads problem

2016-08-09 Thread IronHawk
> The problem is that threads go to sleep while still holding a lock. Yes, this was my mistake. Thanks! But anyway after moving sleep to the right place all still hangs with 6+ characters. > Well, the real problem is that afaik there is no guarantee that this code > will ever finish, > because

Re: spawninig of more than 8 threads problem

2016-08-09 Thread flyx
The problem is that threads _go to sleep while still holding a lock_. Well, the real problem is that afaik there is no guarantee that this code will ever finish, because the scheduler is not required to ever wake the right thread when a bunch of other threads are still waiting. But anyway, Movin

Re: spawninig of more than 8 threads problem

2016-08-09 Thread IronHawk
Interesting addition: I just tested the same in Ubuntu 16.04 under VirtualBox with **2 virtual CPUs**. And it works for string lengths up to 5. With strings having 6, 7, 8 ... characters it hangs in 100% of cases.

Re: spawninig of more than 8 threads problem

2016-08-07 Thread Stefan_Salewski
Interesting. Can you tell me what happens when proc worker executes "if nCallsCur == nCallsTotal: return": Is the lock released for this case. Sorry, no idea.

spawninig of more than 8 threads problem

2016-08-07 Thread IronHawk
Playing with threads I found strange behaviour of _spawn_. I made a simple test of multi-threading: there is the routine proc multithreadedPrint(sMsg: string, nCount: int) which prints the specified string _nCount_ number of times. And each character of the string is printed b