Acquiring thread id (tid) in glib - glib

I am using glib for creating threads:
g_thread_create()
How can I acquire thread id (number)?

You can't. The thread ID is abstracted away in GLib. What do you need it for?

If you are using vala, you can use :
var t = Thread.self<bool> () ;
message ("OTHER THREAD: %p", t) ;
The output is:
** Message: vala-thread.vala:6: OTHER THREAD: 0x165e400

Related

Darwin: Thread suspend/wakeup

Usage Cases:
Thread A: Please remove me from the active thread pool, Mr. Scheduler.
Thread B: Mr. Scheduler, please add Thread A to the active
thread pool, if he isn't there already
This is a fairly specific, and well-contained problem. Of course, I can use pthread's condition variables to do this - but they solve a more complex problem.
Was wondering if there was a more direct route available. For example, sigwait
Thread A:
thread_t thread_a = mach_thread_self();
thread_suspend(thread_a);
Thread B:
thread_resume(thread_a);

How do I find the address of an EXC_BAD_ACCESS exception?

In How do I recover from EXC_BAD_ACCESS?, I figured out how to recover from an EXC_BAD_ACCESS, but I had the badly accessed pointer stored in a global. Obviously, this won't scale. When I run the code in the iOS Simulator (i386), I can see faultvaddr register in the Exception State Registers section of the debugger when inside my catch_exception_raise function. However, its value isn't the same or close to pointer returned from vm_allocate. Is there a way to get this value dynamically?
Given the catch_exception_raise function below, how would I discover the address that caused the EXC_BAD_ACCESS?
kern_return_t
catch_exception_raise(mach_port_t exception_port,
mach_port_t thread,
mach_port_t task,
exception_type_t exception,
exception_data_t code_vector,
mach_msg_type_number_t code_count)
{
fprintf(stderr, "catch_exception_raise %d\n", exception);
return KERN_SUCCESS;
}
There is a great amount of detail on that in the OS X and iOS Internals book (http://www.newosxbook.com). Listing 11-21 (ibid) in the book actually shows sample code to do so. In a nutshell, you've two options:
A) look at the exception itself from the exception data - convert the state to an arm_thread_state, something like so:
struct arm_thread_state *atsh = &exc.old_state;
printf ("CPSR is %p, PC is %p, etc.\n", atsh->cpsr, atsh->pc);
Or
B) call thread_get_state to the thread port (since you have that right there as argument #2), and get pc (the instruction pointer) or any of the other registers
EDIT
I'm not sure how to make A) work, but the following works (found here) for B) on the 32-bit iOS Simulator. I'm not sure what the arm register equivalent is for __faultvaddr, so you'd have to figure that out before trying arm.
// types from thread_status.h
x86_exception_state32_t x86_exception_state32;
mach_msg_type_number_t sc = x86_EXCEPTION_STATE32_COUNT;
thread_get_state(thread,
x86_EXCEPTION_STATE32,
(thread_state_t)&x86_exception_state32,
&sc);

NSThread Programming issues

I am currently learning IOS Threading programming... I encountered an issue:
Here comes my code, please kindly have a look:
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSThread *t1 = [[NSThread alloc]initWithTarget:[MyThread class] selector:#selector(myMethod:) object:Nil];
[t1 start];
}
return 0;
}
#import "MyThread.h"
#implementation MyThread
+ (void)myMethod:(id)param
{
#autoreleasepool {
NSLog(#"called...");
}
}
#end
However, when I ran my program, though there was no error, no message was printed on the console. It seems like myMethod was not executed. I wonder if anyone could give me some suggestions. It has already driven me crazy.
Many thanks in advance.
The main thread of your application is exiting before your other thread has a chance to process anything.
It will work if you add in a simple sleep(1000) statement anywhere before the return 0 statement in your main method.
Your application is terminating before the thread has executed the NSLog.
NSThread creates a detached thread, see Apple's Thread Programming Guide, from which comes:
Important: At application exit time, detached threads can be terminated immediately but joinable threads cannot. Each joinable thread must be joined before the process is allowed to exit. Joinable threads may therefore be preferable in cases where the thread is doing critical work that should not be interrupted, such as saving data to disk.
To create a joinable thread, and hence be able to block your main thread until all joinable threads have finished, you use pthread - covered in the above Guide.
The Java thread model is similar, but uses slightly different terminology. By default a Java thread is joinable and the Java application will continue to execute until all such threads have terminated. A Java thread can be converted to a daemon thread, which is automatically terminated on application exit as with NSThread threads.

Does pthread resets pthread_t value after finishing execution?

first argument of pthread_create is pthread_t (which is typedef'ed to long), which I understand is thread ID.
Does it set this value to zero when thread finishes execution?
I would like to use it to check if the thread is running
No, the pthread_t isn't changed by the pthread library once it's initialized. Pass the pthread_t object to pthread_join() to wait until the thread is finished. If you want to determine without blocking, you can have the thread cooperate by setting an indicator for it's state in some shared object.

Odd behavior when creating and cancelling a thread in close succession

I'm using g++ version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) and libpthread v. 2-11-1. The following code simply creates a thread running Foo(), and immediately cancels it:
void* Foo(void*){
printf("Foo\n");
/* wait 1 second, e.g. using nanosleep() */
return NULL;
}
int main(){
pthread_t thread;
int res_create, res_cancel;
printf("creating thread\n);
res_create = pthread_create(&thread, NULL, &Foo, NULL);
res_cancel = pthread_cancel(thread);
printf("cancelled thread\n);
printf("create: %d, cancel: %d\n", res_create, res_cancel);
return 0;
}
The output I get is:
creating thread
Foo
Foo
cancelled thread
create: 0, cancel: 0
Why the second Foo output? Am I abusing the pthread API by calling pthread_cancel right after pthread_create? If so, how can I know when it's safe to touch the thread? If I so much as stick a printf() between the two, I don't have this problem.
I cannot reproduce this on a slightly newer Ubuntu. Sometimes I get one Foo and sometimes none. I had to fix a few things to get your code to compile (missing headers, missing call to some sleep function implied by a comment and string literals not closed), which indicate you did not paste the actual code which reproduced the problem.
If the problem is indeed real, it might indicate some thread cancellation problem in glibc's IO library. It looks a lot like two threads doing a flush(stdout) on the same buffer contents. Now that should never happen normally because the IO library is thread safe. But what if there is some cancellation scenario like: the thread has the mutex on stdout, and has just done a flush, but has not updated the buffer yet to clear the output. Then it is canceled before it can do that, and the main thread flushes the same data again.

Resources