Thread naming & priority

Linux specific questions.
Post Reply
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Thread naming & priority

Post by Dinosaur »

Hi All

In order to use Linux thread tools it is desirable to have the threads named something other then
the name of the main program.

ps -T -p "program PID number" shows me 4 threads all with the same name.

Does anyone have experience with this and how to name the threads created.?

Code: Select all

    HandlePtr = ThreadCreate(@usbThread,CPtr(Any Ptr,NULL))               ''Start usb I/O in another thread.
This statement creates the thread usbThread WITHIN my application, but that name does not show up
anywhere in the Linux Thread tools.

Regards
Last edited by Dinosaur on Oct 17, 2018 19:27, edited 1 time in total.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Naming Threads

Post by caseih »

Using linux kernel calls, you can set the name of a thread in Linux by using prctl(). However this is not in the C runtime, so I'm not totally sure how to access it from FB. For a C example, see this: https://stackoverflow.com/questions/304 ... -or-thread . Hopefully someone can chime in with how to use this from FB. Maybe it's just a matter of the right declare function line.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Naming Threads

Post by badidea »

There is a "crt/pthread.bi" with "pthread_setname_np(...)" but I have not figured out how to use it.

I think this works:

Code: Select all

'https://manpages.courier-mta.org/htmlman3/pthread_setname_np.3.html
#include "crt/pthread.bi"

Sub print_dots(ByRef char As String)
    For i As Integer = 0 To 2999
        Print char;
        Sleep CInt(Rnd() * 100), 1
    Next
End Sub

Sub mythread1(param As Any Ptr)
    print_dots("*")
End Sub

Sub mythread2(param As Any Ptr)
    print_dots("-")
End Sub

Randomize(Timer())
Dim As pthread_t Ptr pThread1 = ThreadCreate(@mythread1, 0)
Dim As pthread_t Ptr pThread2 = ThreadCreate(@mythread2, 0)
dim as zstring * 16 threadName = "" + chr(0)

print pthread_getname_np(*pThread1, @threadName, 16);
print threadName
print pthread_setname_np(*pThread1, !"THREADFOO\0");
print pthread_getname_np(*pThread1, @threadName, 16);
print threadName

print_dots(".")
ThreadWait(pThread1)
ThreadWait(pThread2)
Print
Sleep
ps -T -p <pid>

Code: Select all

 PID  SPID TTY          TIME CMD
11196 11196 pts/3    00:00:00 test
11196 11197 pts/3    00:00:00 THREADFOO
11196 11198 pts/3    00:00:00 test
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Naming Threads

Post by Dinosaur »

Hi All

Thanks for that badidea, and it does seem to work.
So I will try to implement that into my project and see the result.

Regards
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Naming Threads

Post by Dinosaur »

Hi All

Had a chuckle when I re-read my last post;
Thanks for that badidea,
With the test example from badidea I get only one instance of the thread renamed to THREADFOO.

When I implement it into my application I get 2 instances of the renamed thread but with consecutive PID numbers.
So it looks like another thread with the same name. The Test thread also has 2 instances with the same name.
The second instance of the thread does not seem to accumulate time, it stays at 0:00:0

I guess creating multiple threads that don't run (or consume time) doesn't affect performance, but it just puzzles me.

Regards
EDIT: The ultimate aim is to set a Thread's priority using PTHREAD_SETSCHEDPARAM(3)

EDIT2:
Have modified your test as follows.

Code: Select all

'https://manpages.courier-mta.org/htmlman3/pthread_setname_np.3.html
#include "crt/pthread.bi"
Declare sub mythread1(param as any ptr)
Declare sub mythread2(param as any ptr)
Declare sub print_dots(ByRef char as string)
Dim Shared As pthread_t Ptr This_Thread
Dim Shared As Sched_param Params
Dim As pthread_t Ptr pThread1 = ThreadCreate(@mythread1, 0)
Dim As pthread_t Ptr pThread2 = ThreadCreate(@mythread2, 0)
dim As zstring * 16 threadName = "" + chr(0)


Sub print_dots(ByRef char As String)
    For i As Integer = 0 To 2999
        Print char;
        Sleep CInt(Rnd() * 100), 1
    Next
End Sub

Sub mythread1(param As Any Ptr)
    Dim as Long ret, policy
    print_dots("*")
End Sub

Sub mythread2(param As Any Ptr)
    print_dots("-")
End Sub

Randomize(Timer())
print "1;";pthread_getname_np(*pThread1, @threadName, 16)
print "2;";threadName
print "3;";pthread_setname_np(*pThread1, !"THREADFOO\0")
print "4;";pthread_getname_np(*pThread1, @threadName, 16)
print "5;";threadName
params.sched_priority = sched_get_priority_max(SCHED_FIFO)
Print "6;";params.sched_priority
''Now you have to be Root otherwise next line fails.
Print "7;";pthread_setschedparam(*pthread1, SCHED_FIFO, @params)

print_dots(".")
ThreadWait(pThread1)
ThreadWait(pThread2)
Print
Sleep
Setting a Thread to RT works, but it is showing up on htop as Test being RT and not THREADFOO ????

EDIT3: It seems like the ps command is a bit reluctant to show detail unless you specifically request it.
top -H -p "PID #" shows the new name AND the RT priority.
Post Reply