2017-12-13 08:23 CET

View Issue Details Jump to Notes ]
IDProjectCategoryView StatusLast Update
0000201Cheat Enginepublic2013-01-12 16:45
Reporterablonevn 
Assigned ToDark Byte 
PrioritynormalSeverityminorReproducibilityhave not tried
StatusfeedbackResolutionreopened 
Summary0000201: Lua nativethread bug
Descriptionyep, i' don't know, may be this kind of thread different abit when compare to other language :D here is my code.
i=0;
function th1()
         i=1;
         while i==1 do sleep(0);end;
         i=2;
end
createNativeThread(th1);
i=0;
while i~=2 do print("aaaaa");sleep(100);end;
as you see, after create thread th1, i'm assign i=0; in that way, the loop in thread "while i==1 do sleep(0);end; " will be finished, and print("aaaaa") run forever, is it a bug or just i'm noop to lua ?
TagsNo tags attached.
Attached Files

-Relationships
+Relationships

-Notes

~0000427

ablonevn (reporter)

sorry, the loop "while i==1 do sleep(0);end;" won't finished, and print("aaaaa") run forever

~0000428

Dark Byte (developer)

Last edited: 2013-01-12 12:12

Yup, this is one of the most common threading issues people make

example of the code each thread executes in order:
thread1 thread2
i=0
CreateNativeThread(th1)
i=0
                            i=1
                            while i==1 do sleep(0);end;
                            ...Never returns because I never gets set to 0...
while i~=2 do print("aaaaa");sleep(100);end;
...Also never returns because I never gets set to 2...
                  

As you see, variable "I" has been made 0 before thread2's while loop has started so it sets I to 1 and never returns


A working version:
function th1()
         while i==1 do sleep(0);end;
         i=2;
end
i=1;

createNativeThread(th1);
i=0;
while i~=2 do print("aaaaa");sleep(100);end;

~0000430

ablonevn (reporter)

ah, i see it, thanks, sorry for waste your time :D

~0000432

ablonevn (reporter)

here is my original file, it seem still has bug :D i'm capture screenshoot too.
file nonerr.lua can handle table with 1,000,000 element. but file err.lua cannot handle 100,000 element.

~0000433

Dark Byte (developer)

Last edited: 2013-01-12 14:48

Looks like table.getn isn't threadsafe (in 32-bit, for some reason it works fine in 64-bit)
so, get the count from inside the thread, and and then save it to a variable.

e.g:
i=0;

a={};
count=0


function th()
    i=1;
    while i==1 do
        table.insert(a,1);
                count=table.getn(a);
        --if table.getn(a)>1000000 then break;end;
        sleep(0);
    end;
    i=2;
end
createNativeThread(th);

while i~=1 do sleep(0);end; --wait thread run
--let thread run 1000000 round
while count<100000 do sleep(0);end;
i=0;
while i~=2 do print("aaaaa");sleep(100);end;

Also, for some reason putting a sleep(100) after createativeThread(th) will also fix it

~0000434

Dark Byte (developer)

Last edited: 2013-01-12 15:03

Ok, just checked it out, and lua tables are not thread safe. When you add an item to the list it sometimes relocates the buffer. So add something to make sure the table isn't touched while you're using it (critical section like thing)

table.getn reads the table by going through it till an entry is nil. If the memory it's reading gets freed it'll cause an access violation

~0000435

Dark Byte (developer)

the svn has a new lua5.1-32.dll
looks like the 32-bit build was compiled without threadsafety included

~0000436

ablonevn (reporter)

Last edited: 2013-01-12 16:12

hu ...a, i'm almost complete another issue but you change status and i'm must retype entire post :D
i'm going to test multi thread but it seem dead immediately from initialized what ever is old old dll of lua or new dll, it still not work, may be table object can not be used in nativethread ?
c={}
a={}
count=0;
function th()
    local i,j;
    i=table.getn(c)+1;
    print(string.format("start thread %d",i));
    table.insert(c,1);
    print(table.concat(c));
    print(string.format("From table: %d",table.getn(c)));
    j=0;
    count=table.getn(c);
    
    --while count==1 do --dead event replace with table.getn(c)
        j=j+1;
        print("aaa");
        table.insert(a,j);
        if table.getn(a)>=1000000 then
            --break;
        end
        sleep(100);
    --end;
    c[i]=2;
    print(string.format("finish thread %d",i));
end
th1=createNativeThread(th);
sleep(1000);
thread_freeOnTerminate(th1,true);
--print(count);
while count==0 do print(c);sleep(1000);end; --never out this loop, but if i remove this line then it work
--th2=createNativeThread(th);
--thread_freeOnTerminate(th2,true);

with this code, it dont run any thing.

i saw another project about native lua, i dont known it can help you or not, if you have time, you may want to read abit. here it is (it seem he took it from lua book):
https://github.com/hnakamur/lua-native-thread-experiment

~0000437

Dark Byte (developer)

Last edited: 2013-01-12 16:39

do not call anything that accesses the gui from a thread, it's not threadsafe. Print accesses the gui. Use thread_synchronize(thread, function) to call the specific function from the main thread

Also, gui controls need the messagehandler to run(paint, update box, etc..), but because you're in a infinite loop in the main thread, that will also never run


you might be able to use processMessages(), or better yet, use a timer that checks the count so the main thread has time to update the gui and process events

~0000438

ablonevn (reporter)

ah, it seem abit hard using lua :D saw these line second time, once in forum & once is here, perhaps i'm impatient to lua :D re-close this feed back please.

have a nice weekend :D
+Notes

-Issue History
Date Modified Username Field Change
2013-01-12 09:38 ablonevn New Issue
2013-01-12 09:40 ablonevn Note Added: 0000427
2013-01-12 12:04 Dark Byte Note Added: 0000428
2013-01-12 12:06 Dark Byte Status new => acknowledged
2013-01-12 12:12 Dark Byte Note Edited: 0000428
2013-01-12 12:42 ablonevn Note Added: 0000430
2013-01-12 13:02 Dark Byte Status acknowledged => resolved
2013-01-12 13:02 Dark Byte Resolution open => fixed
2013-01-12 13:02 Dark Byte Assigned To => Dark Byte
2013-01-12 13:45 ablonevn Note Added: 0000432
2013-01-12 13:45 ablonevn Status resolved => feedback
2013-01-12 13:45 ablonevn Resolution fixed => reopened
2013-01-12 13:45 ablonevn File Added: nonerr.lua
2013-01-12 13:45 ablonevn File Added: err.lua
2013-01-12 13:46 ablonevn File Added: err2.png
2013-01-12 14:42 Dark Byte Note Added: 0000433
2013-01-12 14:48 Dark Byte Note Edited: 0000433
2013-01-12 15:02 Dark Byte Note Added: 0000434
2013-01-12 15:03 Dark Byte Note Edited: 0000434
2013-01-12 15:36 Dark Byte Note Added: 0000435
2013-01-12 15:36 Dark Byte Status feedback => resolved
2013-01-12 15:36 Dark Byte Resolution reopened => fixed
2013-01-12 16:10 ablonevn Note Added: 0000436
2013-01-12 16:10 ablonevn Status resolved => feedback
2013-01-12 16:10 ablonevn Resolution fixed => reopened
2013-01-12 16:12 ablonevn Note Edited: 0000436
2013-01-12 16:26 Dark Byte Note Added: 0000437
2013-01-12 16:27 Dark Byte Note Edited: 0000437
2013-01-12 16:28 Dark Byte Note Edited: 0000437
2013-01-12 16:29 Dark Byte Note Edited: 0000437
2013-01-12 16:39 Dark Byte Note Edited: 0000437
2013-01-12 16:45 ablonevn Note Added: 0000438
+Issue History